简体   繁体   中英

Java Swing GUI changing colour on mouse over

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).

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

Set the background to NULL if you want to change the button back to it's default:

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

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) .

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();
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM