Java Swing GUI在鼠标上改变颜色

时间:2013-05-26 02:57:58

标签: java swing jbutton

我对Java Swing库比较新,我试图用一个3乘3的JButton网格编写一个tic tac toe程序。当用户选择一个按钮时,我正在更改包含所选按钮的行和列的背景颜色,以添加突出显示的感觉(通过将每个JButton的button.setBackground()更改为不同的颜色)。

但是,当鼠标拖过其中一个突出显示的按钮时,我目前遇到了删除新背景颜色(并更改回旧背景颜色)的问题。

当鼠标进入按钮时,似乎有一个鼠标事件正在重新绘制按钮,但是我已经尝试过但未能关闭此事件。

我非常感谢任何帮助!如果我需要澄清任何事情,请告诉我。感谢

2 个答案:

答案 0 :(得分:2)

如果要将按钮更改为默认值,请将背景设置为NULL

button.setBackground(inBounds ? new Color(0xFFFF00) : null);

这是我掀起的一个例子。您可以将其用作参考。

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) { }
}

答案 1 :(得分:0)

当鼠标退出时鼠标在按钮上输入时,您可以更改按钮的颜色,使用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();
  }

}