MouseListener帮助Java

我正在尝试用Java Swing编写一个程序,它输出一个10 x 10网格的几何矩形,里面装满了randoms颜色。 但是,当用户单击显示窗口中的一个矩形时,矩形应重新绘制()并更改为另一种颜色。

到目前为止,我已经运行了基本程序,但我无法弄清楚如何为其实现mouseListener,以便在用户单击内部时更改矩形的颜色。 此时,矩形仅在显示窗口展开和最小化时重新绘制。 任何建议/帮助将不胜感激! 谢谢!

这是我到目前为止所拥有的……

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; public class ColorGrid extends JPanel { int w, x, y, z; Color c = new Color((int)(Math.random() * 0xFFFFFF)); public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; setLayout(new GridLayout(10,10)); int w = x = y = z = 0; for(int i=0;i<100;i++){ Color c = new Color((int)(Math.random() * 0xFFFFFF)); w+=10; x+=10; y+=50; z+=15; g2.drawRect(w+10,x+30,y,z); g2.drawRect(w+10,x+30,y,z); g2.fillRect(w+10,x+30,y,z); g2.setPaint(c); } } public static void main(String[] args) { JFrame f= new JFrame(); f.setTitle("ColorGrid Display Window"); f.setSize(200,200); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = f.getContentPane(); contentPane.add(new ColorGrid()); f.show(); } } 

任何Component都可以拥有MouseListenerJLabel很适合彩色矩形,只要你让它变得不透明。

附录:在其他地方推荐了MouseAdapter ,我应该提到一个实例就足够了。

附录:此更新在ColorLabel构造函数中添加了鼠标侦听器。

ColorLabel图片

 import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.JFrame; import javax.swing.JLabel; /** @see http://stackoverflow.com/questions/5136859 */ public class ColorLabel extends JLabel { private static final int N = 10; private static final Random random = new Random(); private static final MouseAdapter listener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { ColorLabel label = (ColorLabel) e.getSource(); label.setBackground(new Color(random.nextInt())); } }; public ColorLabel() { this.setOpaque(true); this.setBackground(new Color(random.nextInt())); this.setPreferredSize(new Dimension(32, 32)); this.addMouseListener(listener); } private void displayGrid() { JFrame f = new JFrame("ColorGrid"); f.setLayout(new GridLayout(N, N)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = 0; i < N * N; i++) { final ColorLabel label = new ColorLabel(); f.add(label); } f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ColorLabel().displayGrid(); } }); } } 

你没有自己绘制颜色网格的JPanel ,而是拥有一个按钮网格。 您可以覆盖按钮的绘图机制,以便它只是呈现为当前颜色。 然后,您具有内置的function,以侦听在网格的特定部分中发生的点击。

这就是我提出的。 注意:我还在大学学习Java,所以这可能不是确切的方法,但是当我这样做时它起作用了。

 public class ColorGrid extends JPanel implements MouseListener { this.addMouseListener(this); addMouseListener(this); 

这是第一部分,第二部分是在代码中使用这些方法。

 public void mouseClicked(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } 

然后,根据您的需要,(即单击鼠标或按下鼠标),只需输入:

 repaint(); 

希望这有帮助。

假设你有一个2d颜色数组,你可以简单地使用x和y,当你点击计算该矩形的索引时,mouselistener会给你。 只需使用整数除法将x和y除以矩形的大小。 更改颜色后使用repaint()来显示它。