在面板中获取单击的组件

时间:2014-02-03 18:22:31

标签: java swing jpanel mouselistener

我有一个面板,其中添加了一些组件。它们是扩展JPanel和JLabel的类 在我的例子中放置了3 + 3,但是假设可以有很多。

 import java.awt.*;
 import java.util.ArrayList;
 import javax.swing.*;

 public class MyPane {
 JFrame frame;
 JPanel panel;
 JPanel addpanel;

 public void createUI()
 {
    frame = new JFrame("Test clicks");
    panel = new JPanel();

    frame.setPreferredSize(new Dimension(300, 300));
    frame.setLayout(new BorderLayout());
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    //ADD SMALLPANELS
    ArrayList<Color> colorList = new ArrayList<Color>();
    colorList.add(Color.red);
    colorList.add(Color.yellow);
    colorList.add(Color.blue);


    for (int i=0; i<3; i++){
    String mytext="no"+ i;
    MyArea addpanel = new MyArea(mytext);
    addpanel.setBackground(colorList.get(i));

    panel.add(addpanel);
    }

    //ADD LABELS
    for (int i=0; i<4 ; i++){
    JLabel myLabel= new  JLabel();
    myLabel.setText("label"+ i);
    panel.add(myLabel);
    }

    //ADD INFORMATION LABEL
    JLabel MyTitle= new  JLabel();
    MyTitle.setText("You just clicked on:");
    MyTitle.setBackground(Color.yellow);
    MyTitle.setOpaque(true);
    panel.add(MyTitle);

 }  

 public static void main(String[] args) {
    MyPane overlapPane = new MyPane();
    overlapPane.createUI();
 }

 public class MyArea extends JPanel{
    public String areaname;

    public MyArea(String myname) {
        areaname=myname;
    }

 public String getAreaName() {
     return areaname;
 }
    }  
 }

我想要的是,每次用户点击容器中的组件时,都会获得所点击的类。然后,根据点击的内容,获取一些属性(JLabel文本或myarea areaname并更改MyTitle标题。如何完成?

2 个答案:

答案 0 :(得分:5)

  1. 使用Java命名约定。类名以CamelCasing开头的大写字母开头,即MyArea

  2. 不要执行此操作JPanel addpanel = new myarea(..)。为什么?因为JPanel没有方法myarea。 你应该这样做(在正确的命名约定之后)。

    MyArea addPanel = new MyArea(...);
    
  3. private字段用作getter

    public class MyArea extends JPanel {
        private String areaName;
        ...
        public String getAreaName() {
            return areaName;
        }
    }
    
  4. 在面板中添加MouseListener

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            MyArea source = (MyArea)e.getSource();
            String areaName = source.getAreaName();
            System.out.println(areaName);
        }
    }
    ....
    MyMouseListener listener = new MyMouseListener();
    for (int i=0; i<3; i++){
       String mytext="no"+ i;
       addpanel = new myarea(mytext);
       addPanel.addMouseListener(new MyMouseListener());
       addpanel.setBackground(colorList.get(i));  
       panel.add(addpanel);
    }
    

答案 1 :(得分:1)

向面板添加MouseListener

    addpanel.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent e) {
        }
        @Override
        public void mousePressed(MouseEvent e) {
        }
        @Override
        public void mouseExited(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            myarea panel = (myarea)e.getSource();
            //you can access the properties of the panel here
        }
    });