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

时间:2016-04-11 14:27:52

标签: java swing jframe

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

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

Tester 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的顶部打开。我尝试在动作监听器中使用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打开时关闭。

1 个答案:

答案 0 :(得分:1)

如果你知道点击的按钮,那么你需要找到按钮所属的框架。

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

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