简体   繁体   中英

Get clicked components in panel

I have a panel in which some components are added. They are classes that extend JPanel and JLabels In my example 3+3 are placed but let's say there can be many of them.

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

What I want is, each time the user clicks on a component in the container, get the class of what is clicked. Then, depending on what is clicked, get some of its properties (JLabel text or myarea areaname and change the MyTitle caption. How can it be done?

  1. Use Java naming convention. Class names begin with capital letters using CamelCasing ie MyArea

  2. Don't do this JPanel addpanel = new myarea(..) . Why? Because JPanel does not have the methods myarea does. You should be doing this ( after correct naming convention ).

     MyArea addPanel = new MyArea(...); 
  3. Use private fields a getters

     public class MyArea extends JPanel { private String areaName; ... public String getAreaName() { return areaName; } } 
  4. Add a MouseListener to the panel.

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

Add a MouseListener to the panel.

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

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