2016-04-11 13 views
0

ich an einem Projekt für die Arbeit arbeite und habe in ein bisschen eine gesperrte Straße. Bei diesem Projekt muss ein Benutzer eine XML-Datei generieren können. Der Benutzer wird zuerst mit einer Anzahl von GUI's konfrontiert. Diejenigen, die erscheinen, hängen von der Auswahl ab, die auf der ersten GUI getroffen wurde. Sobald der Benutzer mit den GUI's fortfährt, werden die Daten verwendet, um eine JTable zu füllen und dann, sobald sie bestätigt sind, in eine XML-Datei zu schreiben.Schließen eines JFrame auf Aktion Hörer innerhalb Methode Fehler

So albern es klingt kann mit etwas ziemlich kompliziert Ich habe mit JFrames auf ein Problem stoßen. Sobald der Benutzer die Informationen auf einem der GUI-Bildschirme ausfüllt und den "Bestätigen" -Knopf drückt, möchte ich, dass JFrame weggeht und der nächste erscheint. Ich habe kein Problem damit, den nächsten zu bekommen, aber aufgrund des Entwurfs der Klasse kann ich nicht herausfinden, wie man die JFrame.dispose() Methode richtig benutzt. Ich werde meine Klassen unten schreiben:

Tester Klasse

package mainClasses; 

import gui.AllGUI; 

public class Tester 

{ 

    public static void main(String args[]){ 

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

    } 

} 

erste GUI-Bildschirm

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); 

    } 

} 

Zweite GUIBildschirm

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); 

    } 

} 

Insbesondere die JComboBox Auswahl auf der ersten GUI Bestimmen Sie, welche GUI als nächstes erscheint.

Also für jetzt habe ich nur Logik für, wenn der Benutzer die erste Option in der JComboBox wählt. Dies funktioniert ordnungsgemäß und die neue GUI wird geöffnet, sie wird jedoch nur auf dem ersten JFrame geöffnet. Ich habe mit JFrame.dispose versucht() innerhalb der Aktion Hörer wie folgt aus:

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

        JFrame.dispose(); 


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

jedoch als Fehler von Eclipse gekennzeichnet ist:

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

Ich verstehe, warum dieser Fehler auftritt und was die Problem ist jedoch, ich habe keine Ahnung, wie zu beheben. Ich habe zahlreiche Ansätze ausprobiert, aber nichts scheint zu funktionieren. Ich würde jede Hilfe sehr schätzen, wenn ich diesen ersten JFrame schließen möchte, wenn der andere sich öffnet.

Antwort

1

Wenn Sie die Taste kennen, auf den geklickt wurde, dann müssen Sie den Rahmen finden die Taste gehört.

So in der ActionListener Ihrer Schaltfläche Sie Code so etwas wie verwenden:

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

Das funktionierte perfekt! Vielen Dank für Ihre Hilfe! – jesric1029

Verwandte Themen