gpt4 book ai didi

java - 如何关闭后窗

转载 作者:行者123 更新时间:2023-11-30 11:21:30 25 4
gpt4 key购买 nike

我有一个名为 Jtable 的 Java 类。当我运行这个类时它工作正常但是如果我运行这个类 10 次然后打开 10 个新窗口我不希望这样,我希望如果我运行这个 java 类任意次数它应该关闭以前的窗口。

我的代码如下:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Jtable extends JFrame {
DefaultTableModel model;
JTable table;
String col[] = {"Name","Address","Phone","hi","","","","","",""};

public static void main(String args[]) {
new Jtable().start();
}

public void start() {
model = new DefaultTableModel(col,9);
table = new JTable(model) {
@Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
};

JScrollPane pane = new JScrollPane(table);
pane.setBounds(50,100,700,400);
String s="hello";
table.setValueAt(s,0,1);

add(pane);
setVisible(true);
setSize(500,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane.setLayout(null);
}
}

最佳答案

将以下修改添加到您的代码中:

public class Jtable extends JFrame
{
//add object of Jtable as class variable
public static Jtable jtable = null;
...
}

public static void main(String args[])
{
//completely change the main method code
//checking whether is there any jtable object exists

if (jtable != null)
{
//if exist it will dispose it
jtable.dispose();
}

//creating a new jtable instance
jtable=new Jtable();
jtable.start();
}

关于java - 如何关闭后窗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22192019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com