如何用JTable打开窗口并关闭另一个窗口

时间:2014-01-01 14:10:30

标签: java swing window jtable jframe

当我想要关闭某个框架并打开那些在JTable里面生成的框架时,我有问题。在控制台中出现了很多错误。当我想要关闭并打开另一个框架而不是JTable时,我的代码可以正常工作。

我以这种方式创建表:

public table(){
        setLayout(new FlowLayout());
        String [] kolumny={"name of columns"};
        Object [][] dane ={
                {"","","","",""},
                {"","","","",""},

        };
        table=new JTable(dane,kolumny);
        table.setPreferredScrollableViewportSize(new Dimension(500,50));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
        table qui = new table();
        qui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        qui.setSize(500,500);
        qui.setVisible(true);
        qui.setTitle("Tabela");

关闭并打开另一帧的代码:

JMenuItem jakas nazwa = new JMenuItem("jakas nazwa");
        akcja.add(jakas nazwa);
        jakas nazwa.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            new qui().setVisible(true);  
            Nazwa okienka które zamykamy.this.dispose();
        }

    });

一些错误:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
    at javax.swing.LookAndFeel.installProperty(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonUI.installDefaults(Unknown Source)
    at javax.swing.plaf.metal.MetalButtonUI.installDefaults(Unknown Source)

1 个答案:

答案 0 :(得分:1)

您正在table构造函数中创建新的table。这就是你获得stackoverflow的原因。这是一个无限循环。

拿出来:

table qui = new table();

只需使用此

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(500,500);
setVisible(true);
setTitle("Tabela");

备注:使用Java命名约定。课程以大写字母开头