如何使用JMenuItem和ActionListener创建JButton

时间:2017-01-13 19:46:36

标签: java jbutton jmenuitem jmenubar

我试图通过使用JMenuItem来创建JButton。我有一个新员工JMenuItem,点击它时会打开一个包含四个JTextFields的框架:员工姓名,员工IDNumber,员工工资率和工作时间,以及一个JButton,Apply。在用户将数据输入JTextFields并点击Apply JButton之后,主窗口应该添加一个带有输入数据的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组件中存储的数据保存到字段中,但我还未能在主程序窗口中成功创建JButton使用“应用”按钮。

感谢任何帮助和帮助!

1 个答案:

答案 0 :(得分:0)

实现在ProgramDisplay类中添加JButton的方法,例如

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