1

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

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

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

我将不胜感激任何帮助!如果我需要澄清任何事情,请告诉我。谢谢

4

2 回答 2

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) { }
}
于 2013-05-26T03:54:35.103 回答
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();
  }

}
于 2017-07-17T04:15:28.513 回答