将最终变量传递给MouseListener方法并返回结果

时间:2014-01-20 01:55:52

标签: java swing jpanel mouselistener

我正在努力使用这段代码,我想点击网格的一个单元格,由JPanel个对象制作,并在该单元格中显示一个带有该单元格索引的标签。我做了一个方法来添加最终变量并返回带有该标签的JPanel。它不起作用。我怎么能这样做?

 public MyTest01(int width, int length) { //constructor

    frame.setLayout(new GridLayout(width, length)); //set layout
    JPanel temp = null;
    JLabel l;

    for (int y = 0; y < length; y++) {
        for (int x = 0; x < width; x++) {
            temp = new JPanel();
            temp.setBorder(new LineBorder(Color.black, 1));
            temp=doStuff(temp,x,y);
            frame.add(temp);
        }
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack(); //sets appropriate size for frame
    frame.setVisible(true); //makes frame visible
}

public static JPanel doStuff( final JPanel temp,final int x, final int y) {

MouseListener mouseListener = new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent mouseEvent) {
       JLabel  l = new JLabel("("+x+" - "+y+")");
     temp.add(l);

    }
};
return temp;
}

1 个答案:

答案 0 :(得分:1)

  1. 您永远不会将监听器添加到JPanel
  2. 添加组件(revalidate)后,您需要repaintJPanel JLabel;

    MouseListener mouseListener = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent mouseEvent) {
            JLabel l = new JLabel("(" + x + " - " + y + ")");
            temp.add(l);
            temp.revalidate();;             <-------- revalidate
            temp.repaint();                 <-------- repaint
        }
    };
    temp.addMouseListener(mouseListener);   <-------- add listener
    return temp;
    

  3. 这是工作代码

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    
    public class MyTest {
    
        JFrame frame = new JFrame();
    
        public MyTest(int width, int length) { //constructor
    
            frame.setLayout(new GridLayout(width, length)); //set layout
            JPanel temp = null;
            JLabel l;
    
            for (int y = 0; y < length; y++) {
                for (int x = 0; x < width; x++) {
                    temp = new JPanel();
                    temp.setBorder(new LineBorder(Color.black, 1));
                    temp = doStuff(temp, x, y);
                    frame.add(temp);
                }
            }
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack(); //sets appropriate size for frame
            frame.setVisible(true); //makes frame visible
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    new MyTest(3, 3);
                }
            });
        }
    
        public static JPanel doStuff(final JPanel temp, final int x, final int y) {
    
            MouseListener mouseListener = new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent mouseEvent) {
                    JLabel l = new JLabel("(" + x + " - " + y + ")");
                    temp.add(l);
                    temp.revalidate();;
                    temp.repaint();
                }
            };
            temp.addMouseListener(mouseListener);
            return temp;
        }
    }