Java Swing GUI 在鼠标悬停时更改颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16755831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing GUI changing colour on mouse over
提问by Mr. Polywhirl
I am relatively new to the Java Swing library and I am attempting to write a tic tac toe program with a 3 by 3 grid of JButtons. When a user selects a button, I am changing the background colour of the row and column that contains the selected button to add a highlighted feel (by changing the button.setBackground() of each JButton to a different colour).
我对 Java Swing 库比较陌生,我正在尝试使用 3 x 3 的 JButton 网格编写井字游戏程序。当用户选择一个按钮时,我正在更改包含所选按钮的行和列的背景颜色以添加突出显示的感觉(通过将每个 JButton 的 button.setBackground() 更改为不同的颜色)。
However, I am currently having an issue where the new background colour is removed (and changed back to the old background colour) when the mouse is dragged over one of the highlighted buttons.
但是,我目前遇到了一个问题,即当将鼠标拖动到突出显示的按钮之一上时,新的背景颜色会被删除(并更改回旧的背景颜色)。
There appears to be a mouse event that is repainting the button when the mouse enters the button, however I have tried and failed to turn this event off.
当鼠标进入按钮时,似乎有一个鼠标事件正在重新绘制按钮,但是我已经尝试并未能关闭此事件。
I would greatly appreciate any help! If I need to clarify anything please let me know. Thanks
我将不胜感激任何帮助!如果我需要澄清任何事情,请告诉我。谢谢
回答by Mr. Polywhirl
Set the background to NULL
if you want to change the button back to it's default:
NULL
如果要将按钮更改回默认值,请将背景设置为:
button.setBackground(inBounds ? new Color(0xFFFF00) : null);
Here is an example I whipped up. You can use it as a reference.
这是我提出的一个例子。您可以将其用作参考。
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridRollOver extends JFrame implements MouseListener {
private static final long serialVersionUID = -7134685459910610342L;
public JButton[] buttons = new JButton[9];
public GridRollOver() {
this.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
JButton b = new JButton();
b.setRolloverEnabled(true);
b.addMouseListener(this);
this.add(b);
buttons[i] = b;
}
this.setVisible(true);
this.setSize(500, 500);
this.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new GridRollOver();
}
public void highlightButtons(Point cursor) {
for (int i = 0; i < buttons.length; i++) {
JButton button = buttons[i];
Point buttonLocation = button.getLocationOnScreen();
double west = buttonLocation.getX();
double east = buttonLocation.getX() + button.getWidth();
double north = buttonLocation.getY();
double south = buttonLocation.getY() + button.getHeight();
boolean inRow = cursor.getX() > west && cursor.getX() < east;
boolean inCol = cursor.getY() > north && cursor.getY() < south;
boolean inBounds = inRow || inCol;
button.setBackground(inBounds ? new Color(0xFFFF00) : null);
}
}
@Override
public void mouseEntered(MouseEvent event) {
highlightButtons(event.getLocationOnScreen());
}
@Override
public void mouseExited(MouseEvent e) { }
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
}
回答by Ganesh Patel
In which you can change the color of button when mouse entered on the button when mouse exit it change to it default color by using MouseListener
method mouseEntered(MouseEvent e)
and mouseExited(MouseEvent e)
.
其中,您可以更改当鼠标进入按钮时按钮的颜色,当鼠标退出时,使用MouseListener
方法mouseEntered(MouseEvent e)
和更改为默认颜色mouseExited(MouseEvent e)
。
package listener;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* This class is used to show when mouse arrow on the button its color change when exited it again on it same color
* @author Ganesh Patel
*
*/
public class ButtonColorChanger implements MouseListener{
JFrame frame;
JButton buttonArray[];
JPanel contentPane;
public ButtonColorChanger() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = createContentPane();
frame.setContentPane(contentPane);
frame.setVisible(true);
frame.pack();
}
/**
* This method is used to create content pane and add 9 button and call the MouseListener on every button
* @return panel content pane of the frame
*/
public JPanel createContentPane() {
JPanel panel = new JPanel(new GridLayout(3,3));
buttonArray = new JButton[9];
//add 9 button on the panel and call MouseListener on every button
for(int i = 0; i<buttonArray.length; i++) {
buttonArray[i] = new JButton(" O ");
buttonArray[i].addMouseListener(this);
panel.add(buttonArray[i]);
}
return panel;
}
@Override
public void mouseClicked(MouseEvent e) {
}
/**
*This method is used for change the color of button when mouse on it.
*/
@Override
public void mouseEntered(MouseEvent e) {
JButton button = (JButton)e.getSource();
button.setBackground(Color.RED);
}
/**
* This method is used to change the color of button when mouse is not on it.
*/
@Override
public void mouseExited(MouseEvent e) {
JButton button = (JButton)e.getSource();
button.setBackground(null);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
public static void main(String args[]) {
new ButtonColorChanger();
}
}