在方法错误内关闭动作侦听器上的JFrame

jesric1029

我正在为一个工作项目工作,遇到了一些障碍。该项目实际上涉及允许用户生成XML文件。首先为用户提供许多GUI-出现的GUI取决于在第一个GUI上所做的选择。用户使用GUI进行处理后,数据将用于填充JTable,然后一旦确认放入XML文件中即可。

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

测试人员类别

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的顶部打开。我已经尝试过在动作监听器中使用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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章