如何使用 JMenuItem 和 ActionListener 创建 JButton

How to create JButtons using JMenuItem and ActionListener

我正在尝试通过使用 JMenuItem 创建 JButton。我有一个新员工 JMenuItem,单击它会打开一个包含四个 JTextField 的框架:员工姓名、员工 IDNumber、员工工资率和员工工作时数,以及一个 JButton Apply。在用户将数据输入 JTextFields 并点击 Apply JButton 后,main window 应该添加一个带有输入数据的 JButton。

这是程序显示的代码

 public ProgramDisplay() {

        setTitle("Fluid Accounts");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new GridLayout(5, 5));
        account = new JButton("<html>" + "Samuel Tayman" + "<br>" + "Wages: " + findPay(hours, payRate));
        add(account);
        account = new JButton("<html>" + "Janice" + "<br>" + "Wages: " + findPay(hours, payRate));
        add(account);

        buildMenuBar();
        pack();
        setVisible(true);

    }

这里是新员工 JMenuItem 的代码 ActionListener:

private void buildEmployeeMenu()

{

    newEmployee = new JMenuItem("New Employee");
    newEmployee.addActionListener(new NewListener());
    employeeMenu = new JMenu("Employees");
    employeeMenu.add(newEmployee);

}

/**
 * Private inner class that handles the event that is generated when the
 * user selects New from the file menu.
 */

private class NewListener extends JFrame implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void actionPerformed(ActionEvent e) {

        name = "";
        employeeID = "";
        rateOfPay = "";
        hoursWorked = "";
        JButton Apply = new JButton("Apply");

        Apply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                name = nameField.getText();
                employeeID = IDField.getText();
                rateOfPay = payRateField.getText();
                hoursWorked = hoursField.getText();
                setVisible(false);
            }
        });
        setLayout(new GridLayout(4, 4));
        add(nameField = new JTextField("Enter the Employee Name"));
        add(IDField = new JTextField("Enter the Employee's ID Number"));
        add(payRateField = new JTextField("Enter the Employee's Pay Rate"));
        add(hoursField = new JTextField("Enter the Employee's Hours Worked"));
        add(Apply);

        pack();
        setVisible(true);

    }

}

Example of current running program

我已经做到了 textField 组件中存储的数据被保存到字段中但是我无法在主程序 [=29] 中成功创建 JButton =] 使用应用按钮。

感谢任何帮助!

实现一种在 ProgramDisplay 中添加 JButton 的方法class,例如

 void addButton(String name, String employeeID, String rateOfPay, String hoursWorked) {
      add(new JButton(name + ":" + employeeID + ", " + rateOfPay + " - " + hoursWorked); }

将 ProgramDisplay 实例作为最终参数传递给 NewListener,例如。 G。

private class NewListener extends JFrame implements ActionListener {
     final ProgramDisplay pd;
     NewListener(ProgramDisplay pd) {
          this.pd = pd;
     }  
 .....

并从 ActionPerformed 方法中调用 addButton 方法:

    Apply.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            name = nameField.getText();
            employeeID = IDField.getText();
            rateOfPay = payRateField.getText();
            hoursWorked = hoursField.getText();
            setVisible(false);
            NewListener.this.pd.addButton(name, employeeID, rateOfPay, hoursWorked);
        }
    });