JPanel动作监听器

时间:2015-02-15 09:32:42

标签: java swing oop actionlistener

我创建了一个JTabbedPane和一些JPanel个对象。由于每个面板几乎看起来相同,因此字段也称为相同。我认为没关系,因为所有字段都是局部变量。现在我在控制器类中,我遇到了一个问题:

  1. 向每个JPanel
  2. 中的所有字段添加动作侦听器的最佳方法
  3. 确定选择了哪个标签窗格。
  4. 这是GUI:     包mainGUI;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;
    
    public class MainMenuGUI {
    
    JTabbedPane tabbedPane = new JTabbedPane();
    
    JPanel findUserPanel;
    JPanel deleteUserPanel;
    JPanel addUserPanel;
    
    JFrame frame = new JFrame();
    JPanel tabbedPanel = new JPanel();
    
    //Controller class for MainMenuGUI
    ControllerMainMenuGUI listen = new ControllerMainMenuGUI(this);
    
    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;
    }
    
    JPanel createDeleteUserPanel() {
        deleteUserPanel = new JPanel();
        deleteUserPanel.setPreferredSize(new Dimension(300, 300));
        deleteUserPanel.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));
    
        deleteUserPanel.add(firstlbl);
        deleteUserPanel.add(firstNametxt);
        deleteUserPanel.add(lastlbl);
        deleteUserPanel.add(lastNametxt);
        deleteUserPanel.add(addresslbl);
        deleteUserPanel.add(addresstxt);
        deleteUserPanel.add(agelbl);
        deleteUserPanel.add(age);
        deleteUserPanel.add(window, BorderLayout.CENTER);
        deleteUserPanel.add(btn);
        return deleteUserPanel;
    }
    
    JPanel createAddUserPanel() {
        addUserPanel = new JPanel();
        addUserPanel.setPreferredSize(new Dimension(300, 300));
        addUserPanel.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);
        // nametxt.setPreferredSize(new Dimension(1,10));
        JButton btn = new JButton("Submit");
        JScrollPane window = new JScrollPane();
        window.setViewportBorder(new LineBorder(Color.RED));
        window.setPreferredSize(new Dimension(150, 150));
    
        addUserPanel.add(firstlbl);
        addUserPanel.add(firstNametxt);
        addUserPanel.add(lastlbl);
        addUserPanel.add(lastNametxt);
        addUserPanel.add(addresslbl);
        addUserPanel.add(addresstxt);
        addUserPanel.add(agelbl);
        addUserPanel.add(age);
        addUserPanel.add(window, BorderLayout.CENTER);
        addUserPanel.add(btn);
        return addUserPanel;
    }
    }
    

    控制器类:

    package mainGUI;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class ControllerMainMenuGUI {
    
    MainMenuGUI mainMenuGUI;
    
    int currentTabbedIndex = 0;
    
    ControllerMainMenuGUI(MainMenuGUI mainMenuGUI){
        this.mainMenuGUI = mainMenuGUI;
    }
    
    //inner class will handle MainMenuGUI actions
    class Actions implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
    
        }
    
        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);
    
                }
            });
        }
    
    
    
    }
    
    }
    

0 个答案:

没有答案