2016-04-11 17 views
0

私は仕事のためのプロジェクトを進めており、少しのロードブロッキングを実行しています。このプロジェクトでは、実際にユーザーがXMLファイルを生成できるようにします。ユーザは、最初に多数のGUIを提示される。これらのGUIは、第1のGUI上で選択されたものに依存する。ユーザーがGUIを使用して処理を進めると、そのデータはJTableを埋めるために使用され、XMLファイルに入れられると確認されます。メソッドエラー内のアクションリスナーでJFrameを閉じる

私は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の上に開くだけです。それはのためのEclipseでエラーとしてフラグが立てられただし

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

        JFrame.dispose(); 


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

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

が、私はこのエラーが発生する理由を理解し、どのような私はこのようなアクションリスナー内JFrame.dispose()を使用して試してみました問題は解決する方法がわかりません。私は数多くのアプローチを試みましたが、何も動作していないようです。最初のJFrameを閉じるときに、他のJFrameを閉じるときには、本当に助けてください。

答えて

1

クリックされたボタンが分かっている場合は、そのボタンが属するフレームを見つける必要があります。完全に働いた

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

:あなたのようなコードのものを使用することができ、あなたのボタンのActionListenerでそう

!あなたの助けをありがとう! – jesric1029

関連する問題