将ActionListeners添加到JPanel

时间:2015-02-16 08:50:57

标签: java swing

这个问题已被问过几次,但我的情况有点不同。我创建了一个小应用程序,在视图中我向JFrame添加了一些JPanel。然后我尝试在控制器中添加actionListeners,这是发生问题的地方。

以下代码给出了以下错误:

The method addActionListener(new ActionListener(){})
is undefined for the type JPanel

视图类

public class MainMenuGUI {

JTabbedPane tabbedPane = new JTabbedPane();

JPanel findUserPanel;
JPanel deleteUserPanel;
JPanel addUserPanel;

JFrame frame = new JFrame();
JPanel tabbedPanel = new JPanel();

//Controller class for tabbedPanel
ControllerTabbedPane listen = new ControllerTabbedPane(this);
//Controller class for findUserPanel
FindUserPanelController findUserController = new FindUserPanelController(findUserPanel);

public MainMenuGUI() {

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    findUserPanel = createFindUserPanel();
    deleteUserPanel = createDeleteUserPanel();
    addUserPanel = createAddUserPanel();

    tabbedPane.addTab("Find User", findUserPanel);
    tabbedPane.addTab("Delete User", deleteUserPanel);
    tabbedPane.addTab("Add User", addUserPanel);

    tabbedPanel.add(tabbedPane);
    frame.add(tabbedPanel);

    frame.pack();
    // opens frame in the center of the screen
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

JPanel createFindUserPanel() {
    findUserPanel = new JPanel();
    findUserPanel.setPreferredSize(new Dimension(300, 300));
    findUserPanel.setLayout(new GridLayout(5, 7));

    JLabel firstlbl = new JLabel("First Name");
    JLabel lastlbl = new JLabel("Last Name");
    JLabel addresslbl = new JLabel("Address");
    JLabel agelbl = new JLabel("Age");

    JTextField firstNametxt = new JTextField(15);
    JTextField lastNametxt = new JTextField(15);
    JTextField addresstxt = new JTextField(30);
    JTextField age = new JTextField(3);
    JButton btn = new JButton("Submit");
    JScrollPane window = new JScrollPane();
    window.setViewportBorder(new LineBorder(Color.RED));
    window.setPreferredSize(new Dimension(150, 150));

    findUserPanel.add(firstlbl);
    findUserPanel.add(firstNametxt);
    findUserPanel.add(lastlbl);
    findUserPanel.add(lastNametxt);
    findUserPanel.add(addresslbl);
    findUserPanel.add(addresstxt);
    findUserPanel.add(agelbl);
    findUserPanel.add(age);
    findUserPanel.add(window, BorderLayout.CENTER);
    findUserPanel.add(btn);

    return findUserPanel;
}

控制器类

 public class ControllerTabbedPane {

    MainMenuGUI mainMenuGUI;

    int currentTabbedIndex = 0;

    ControllerTabbedPane(MainMenuGUI mainMenuGUI){
        this.mainMenuGUI = mainMenuGUI;
        addTabbedPaneListeners();

    }

    private void addTabbedPaneListeners() {

        mainMenuGUI.tabbedPane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {

                currentTabbedIndex = mainMenuGUI.tabbedPane.getSelectedIndex();
                System.out.println("Current tab is:" + currentTabbedIndex);

            }
        });
    }

    /*ERROR saying The method addActionListener(new ActionListener(){})
    is undefined for the type JPanel*/
    private void findPanelListeners() {

        mainMenuGUI.findUserPanel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }


        });

    }

2 个答案:

答案 0 :(得分:0)

这是实现您的请求的方式:

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton bt1 = new JButton();
JButton bt2 = new JButton();
panel1.add(bt1);
panel2.add(bt2);

bt1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Bt1 on panel1 pressed");
    }
});

bt2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Bt2 on panel2 pressed");
    }
});

您可以将变量或其他对象修改为侦听器,以存储“按下”哪个面板。

答案 1 :(得分:0)

我认为无法将addActionListner()添加到JPanel

相反

你可以使用,

 JPanel p1=new JPanel();
 p1.addMouseListener(this);

并覆盖

public void mouseClicked(MouseEvent me)
{
  int x=me.getX();
  int y=me.getY();
  System.out.println(x+","+y);
    //By using x AND y you can identify the panel 

}

注意:extends MouseAdapter