2016-04-11 11 views
0

Bir proje üzerinde çalışmak için çalışıyorum ve bir barikata biraz geçtim. Bu proje aslında bir kullanıcının bir XML dosyası oluşturmasına izin vermeyi içerir. Kullanıcı ilk olarak birkaç GUI ile sunulur - İlk GUI'de yapılan seçimlere bağlı olanlar. Kullanıcı GUI'lerle ilerledikten sonra, veriler bir JTable'ı doldurmak için kullanılacaktır ve bir kez doğrulandıktan sonra bir XML dosyasına konacaktır.Yöntem hatası içinde bir JFrame'in eylem dinleyicide kapatılması

Oldukça karmaşık bir şeyle ilgili olabileceği gibi aptalca olduğu için JFrames ile bir problem yaşadım. Kullanıcı, GUI ekranlarından birindeki bilgileri doldurup "onayla" düğmesine bastığında, JFrame'in gitmesini ve bir sonraki görüntülenmesini istiyorum. Bir sonraki görünecek bir sorunum yok, ancak sınıfın tasarımı nedeniyle JFrame.dispose() yönteminin nasıl doğru şekilde kullanılacağını anlayamıyorum. Aşağıda benim sınıfları göndeririz:

Tester Sınıf

package mainClasses; 

import gui.AllGUI; 

public class Tester 

{ 

    public static void main(String args[]){ 

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

    } 

} 

İlk GUI Ekranı

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

    } 

} 

İkinci GUI Ekranı Özellikle

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

    } 

} 

ilk GUI üzerinde JComboBox seçimi olacak Daha sonra hangi GUI’nın görüneceğini belirleyin.

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"; 
       } 

      } 

     }); 

Yani şimdilik sadece kullanıcı JComboBox ilk seçeneği seçerse için mantığı var. Bu düzgün çalışır ve yeni GUI açılır, ancak sadece ilk JFrame'in üstünde açılır. onun için Eclipse tarafından hata olarak işaretlenir Ancak

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 

bu hata oluşur anlamak ve ne böyle eylem dinleyici içinde JFrame.dispose() kullanarak denedi Ancak sorun nasıl çözüleceğine dair bir fikrim yok. Çok sayıda yaklaşım denedim ama hiçbir şey işe yaramıyor. Diğer JFrame'in açıldığı zaman kapanmasını sağlayan herhangi bir yardımı gerçekten takdir ediyorum.

cevap

1

Tıklanan düğmeye basarsanız, düğmenin ait olduğu çerçeveyi bulmanız gerekir.

Böylece gibi kod şey kullanabilirsiniz Düğmenizin ActionListener yılında:

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

mükemmel çalıştı ki! Yardımlarınız için çok teşekkürler! – jesric1029

İlgili konular