MouseListener 帮助 Java [英] MouseListener Help Java

查看:29
本文介绍了MouseListener 帮助 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Java Swing 编写一个程序,该程序输出一个 10 x 10 的几何矩形网格,其中填充了随机颜色.但是,当用户单击显示窗口中的一个矩形时,该矩形应该 repaint() 并更改为另一种颜色.

I am trying to write a program in Java Swing that outputs a 10 x 10 grid of geometric rectangles filled with randoms colors. However, when the user clicks on one of the rectangles within the display window the rectangle should repaint() and change to another color.

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

Thus far I have the rudimentary program running, but I can't figure out how to implement a mouseListener to it in order to have the rectangles' color change when a user clicks inside. At this point, the rectangles only repaint when the display window is expanded and minimized. Any advice/help would be greatly appreciated! Thanks!

这是我目前所拥有的...

Here is what I have so far...

   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 都可以有一个 MouseListener.JLabel 非常适合彩色矩形,只要您使它不透明即可.

Any Component can have a MouseListener. JLabel is nice for a colored rectangle, as long as you make it opaque.

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

Addendum: Having recommended MouseAdapter elsewhere, I should mention that one instance is enough.

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

Addendum: This update adds the mouse listener in the ColorLabel constructor.

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

这篇关于MouseListener 帮助 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆