gpt4 book ai didi

java - 在方法错误中关闭 Action 监听器上的 JFrame

转载 作者:行者123 更新时间:2023-12-01 10:04:31 26 4
gpt4 key购买 nike

我正在开发一个工作项目,但遇到了一些障碍。该项目实际上涉及允许用户生成 XML 文件。首先向用户呈现多个 GUI - 出现的 GUI 取决于在第一个 GUI 上所做的选择。一旦用户继续使用 GUI,数据将用于填充 JTable,然后在确认后放入 XML 文件中。

尽管听起来很愚蠢,但我遇到了 JFrames 的问题。一旦用户在其中一个 GUI 屏幕上填写信息并点击“确认”按钮,我希望该 JFrame 消失并出现下一个。我没有问题让下一个出现,但由于类的设计,我无法弄清楚如何正确使用 JFrame.dispose() 方法。我将在下面发布我的类(class):

测试员类

package mainClasses;

import gui.AllGUI;

public class Tester

{

public static void main(String args[]){

AllGUI aGUI = new AllGUI();
aGUI.createAllGUI();

}

}

第一个 GUI 屏幕

package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AllGUI

{

private static final Insets normalInsets = new Insets(10, 10, 0, 10);
private static final Insets comboInsets = new Insets(10,10,10,10);
public static String type = null;
public boolean finished = false;

public void createAllGUI(){

JFrame frame = new JFrame("All File Types Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);


}

private JPanel createMainPanel(){

JPanel mainPanel = new JPanel(new BorderLayout());
JPanel formPanel = new JPanel(new GridBagLayout());

int gridy=0;

JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
addComponent(formPanel, groupMessageIdTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField groupMessageIdText = new JTextField("",10);
addComponent(formPanel, groupMessageIdText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
addComponent(formPanel, isoDateTimeTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

JTextField isoDateTimeText = new JTextField("",10);
addComponent(formPanel, isoDateTimeText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

JLabel notificationIdTitle = new JLabel("Notification Id:");
addComponent(formPanel, notificationIdTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

JTextField notificationIdText = new JTextField("",10);
addComponent(formPanel, notificationIdText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);

JLabel notificationAcctIdTitle = new JLabel("Notification Account Id");
addComponent(formPanel, notificationAcctIdTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField notificationAcctIdText = new JTextField("",10);
addComponent(formPanel, notificationAcctIdText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel numberOfEntriesTitle = new JLabel("Number of Entries");
addComponent(formPanel, numberOfEntriesTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField numberOfEntriesText = new JTextField("",10);
addComponent(formPanel,numberOfEntriesText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts");
addComponent(formPanel,sumOfAmountsTitle, 2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField sumOfAmountsText = new JTextField("",10);
addComponent(formPanel,sumOfAmountsText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel fileTypeTitle = new JLabel("Camt54 File Type");
addComponent(formPanel,fileTypeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

String[] fileTypes = {"OTC-R Message","Home Banking","Cleared Checks"};

final JComboBox<String> fileTypesComboBox = new JComboBox<String>(fileTypes);
addComponent(formPanel,fileTypesComboBox,1,gridy,1,1,comboInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JPanel confirmPanel = new JPanel();

JButton confirmButton = new JButton("Confirm");

confirmButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

if(fileTypesComboBox.getSelectedIndex()==0){
type="OTC";

TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
tcrGUI.createTCRSpecificGUI();

}else if(fileTypesComboBox.getSelectedIndex()==1){
type="HOME";
}else if(fileTypesComboBox.getSelectedIndex()==2){
type="CLEARED";
}

}

});

confirmPanel.add(confirmButton);

mainPanel.add(formPanel,BorderLayout.NORTH);

mainPanel.add(confirmPanel,BorderLayout.CENTER);

return mainPanel;

}

private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth
,int gridheight, Insets insets, int anchor, int fill){

GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0D, 0.0D
,anchor, fill, insets, 0,0);

container.add(component,gbc);

}

}

第二个 GUI 屏幕

package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TCRSpecificGUI

{

private static final Insets normalInsets = new Insets(10,10,0,10);

public void createTCRSpecificGUI(){

JFrame frame = new JFrame("TCR-Specific Tags");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

}

private JPanel createMainPanel(){

JPanel mainPanel = new JPanel(new BorderLayout());
JPanel formPanel = new JPanel(new GridBagLayout());

int gridy=0;

JLabel proprietaryPartyTypeTitle = new JLabel("Proprietary Party Type:");
addComponent(formPanel,proprietaryPartyTypeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField proprietaryPartyTypeText = new JTextField("",10);
addComponent(formPanel, proprietaryPartyTypeText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel proprietaryPartyIdTitle = new JLabel("Proprietary Party ID:");
addComponent(formPanel, proprietaryPartyIdTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField proprietaryPartyIdText = new JTextField("",10);
addComponent(formPanel, proprietaryPartyIdText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel transactionDateTimeTitle = new JLabel("Transaction Date/Time:");
addComponent(formPanel, transactionDateTimeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField transactionDateTimeText = new JTextField("",10);
addComponent(formPanel, transactionDateTimeText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel rMessageFileNameTitle = new JLabel("R-Message File Name:");
addComponent(formPanel,rMessageFileNameTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField rMessageFileNameText = new JTextField("", 10);
addComponent(formPanel, rMessageFileNameText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JLabel supplementaryXPathTitle = new JLabel("Supplementary X-Path:");
addComponent(formPanel, supplementaryXPathTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JTextField supplementaryXPathText = new JTextField("",10);
addComponent(formPanel, supplementaryXPathText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
,GridBagConstraints.HORIZONTAL);

JPanel confirmPanel = new JPanel();

JButton confirmButton = new JButton("Confirm");

confirmButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){


}

});

confirmPanel.add(confirmButton);
mainPanel.add(formPanel,BorderLayout.NORTH);
mainPanel.add(confirmPanel,BorderLayout.CENTER);

return mainPanel;

}



private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth
,int gridheight, Insets insets, int anchor, int fill){

GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0D, 0.0D
,anchor,fill,insets,0,0);

container.add(component,gbc);

}

}

具体而言,第一个 GUI 上的 JComboBox 选择将确定下一个出现的 GUI。

confirmButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

if(fileTypesComboBox.getSelectedIndex()==0){
type="OTC";

TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
tcrGUI.createTCRSpecificGUI();

}else if(fileTypesComboBox.getSelectedIndex()==1){
type="HOME";
}else if(fileTypesComboBox.getSelectedIndex()==2){
type="CLEARED";
}

}

});

所以现在我只有用户选择 JComboBox 中的第一个选项的逻辑。这可以正常工作并且新的 GUI 会打开,但它只是在第一个 JFrame 之上打开。我尝试在 Action 监听器中使用 JFrame.dispose() ,如下所示:

if(fileTypesComboBox.getSelectedIndex()==0){
type="OTC";

JFrame.dispose();


TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
tcrGUI.createTCRSpecificGUI();

但是它被 Eclipse 标记为错误:

Cannot make a static reference to the non-static method dispose() from the type Window

我明白为什么会出现此错误以及问题是什么,但我不知道如何修复。我尝试了多种方法,但似乎没有任何效果。我真的很感激任何帮助让第一个 JFrame 在另一个 JFrame 打开时关闭。

最佳答案

如果您知道被单击的按钮,那么您需要找到该按钮所属的框架。

因此,在按钮的 ActionListener 中,您可以使用类似以下的代码:

Component component = (Component)e.getSource();
Window window = SwingUtilties.windowForComponent(component);
window.dispose();

关于java - 在方法错误中关闭 Action 监听器上的 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551569/

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