我创建了一个产生StartUpWindow对象的类。 JButton组件之一关闭JFrame,然后调用要实例化的新型帧AdminMainWindow。但是,当实例化AdminMainWindow时,将打开两个帧。

谁能在我的代码中看到错误?
Main

public class Main {

    public static void main(String[] args) {
        StartUpWindow startUpWindow = new StartUpWindow();
    }
}
StartUpWindow
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StartUpWindow extends JFrame implements ActionListener {

    private JButton admin;
    private JButton captain;
    private JButton grades;

    public StartUpWindow() {
        setTitle("Start Up Menu");
        setButtons();
        setAction();
        setSize(200, 400);
        setVisible(true);
    }

    void setButtons() {

        admin = new JButton("Admin");
        captain = new JButton("Captain");
        grades = new JButton("Grades");
        getContentPane().setLayout(new FlowLayout());
        add(admin);
        add(captain);
        add(grades);
        setAction();
    }

    void setAction() {
        admin.addActionListener(this);
        captain.addActionListener(this);
        grades.addActionListener(this);

    }

    public void actionPerformed(ActionEvent eve) {

        if (eve.getSource() == admin)
            createAdminMainWindow();
        else if (eve.getSource() == captain)
            createCaptainMainWindow();
        else if (eve.getSource() == grades)
            createGradesMainWindow();

    }

    void createAdminMainWindow() {
        this.dispose();
        AdminMainWindow adminMainWindow = new AdminMainWindow();
    }

    void createCaptainMainWindow() {
        this.dispose();
        //this.adminMainWindow = new CaptainMainWindow();
    }

    void createGradesMainWindow() {
        this.dispose();
        //this.adminMainWindow = new GradesMainWindow();
    }
}
AdminMainWindow
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AdminMainWindow extends JFrame implements ActionListener {

    private JMenuItem add;
    private JMenuItem edit;
    private JMenuItem delete;
    private JMenuItem view;
    private JMenuItem viewAll;
    private JTextField jt;

    public AdminMainWindow() {
        setTitle("JMenuBar_test");
        setJMenuBarAndMenuBarItems();
        setAction();
        setJTextField();
        setSize(1400, 600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void setJTextField() {
        jt = new JTextField(10);
        add(jt);
    }

    void setJMenuBarAndMenuBarItems() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu1 = new JMenu("Edit");
        add = new JMenuItem("Add");
        edit = new JMenuItem("Edit");
        delete = new JMenuItem("Delete");
        JMenu menu2 = new JMenu("View");
        viewAll = new JMenuItem("View All Entries");
        view = new JMenuItem("View Entry");
        menu1.add(add);
        menu1.add(edit);
        menu1.add(delete);
        menu2.add(viewAll);
        menu2.add(view);
        menuBar.add(menu1);
        menuBar.add(menu2);
        setJMenuBar(menuBar);
    }


    void setAction() {
        add.addActionListener(this);
        edit.addActionListener(this);
        delete.addActionListener(this);
        viewAll.addActionListener(this);
        view.addActionListener(this);
    }

    JFrame setJTextField(JFrame jFrame) {
        JTextField jt = new JTextField(10);
        jFrame.add(jt);
        return jFrame;
    }

    public void actionPerformed(ActionEvent eve) {
        if (eve.getSource() == add)
            jt.setText("Add");
        else if (eve.getSource() == edit)
            jt.setText("Edit");
        else if (eve.getSource() == delete)
            jt.setText("Delete");
        else if (eve.getSource() == viewAll)
            jt.setText("ViewAll");
        else if (eve.getSource() == view)
            jt.setText("ViewOne");
    }
}

在第一个窗口中单击“管理员”按钮,它会打开一个AdminMainWindow JFrame,而正在生成两个。

最佳答案

您已添加addActionListener事件两次。从构造函数中删除setAction();,然后您的应用程序应按预期运行。

  • 第一次添加到构造函数

  • public StartUpWindow() {
        setTitle("Start Up Menu");
        setButtons();
    -- Here -->    setAction();
        setSize(200, 400);
        setVisible(true);
    }
    
  • 创建按钮并将其添加到框架
  • 时将第二次添加
    void setButtons() {
        admin = new JButton("Admin");
        captain = new JButton("Captain");
        grades = new JButton("Grades");
    
        getContentPane().setLayout(new FlowLayout());
        add(admin);
        add(captain);
        add(grades);
    -- Here -->    setAction();
    }
    

    08-16 16:24