gpt4 book ai didi

java - JFrame 调用正在打开两个 JFrame

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:49 33 4
gpt4 key购买 nike

我创建了一个生成 StartUpWindow 对象的类。其中一个 JButton 组件关闭 JFrame,然后调用一种新类型的框架进行实例化,AdminMainWindow。但是,当 AdminMainWindow 被实例化时,会打开两个框架。

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

主要

public class Main {

public static void main(String[] args) {
StartUpWindow startUpWindow = new 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();,然后您的应用程序应该会按预期运行。

  1. 第一次在构造函数中添加
public StartUpWindow() {
setTitle("Start Up Menu");
setButtons();
-- Here --> setAction();
setSize(200, 400);
setVisible(true);
}
  1. 第二次是在创建按钮并将其添加到框架时添加
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();
}

关于java - JFrame 调用正在打开两个 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645258/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com