确实不可能在运行时删除任何 JDialog 或 JWindow
正如我所尝试的那样,看起来这是不可能的,没有成功,或者以另一种方式存在?
import java.awt.*;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class SuperConstructor extends JFrame {
private static final long serialVersionUID = 1L;
private int i = 1;
private boolean runProcess;
private int top = 20;
private int left = 20;
public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
setLocation(150, 150);
pack();
setVisible(true);
Point loc = this.getLocation();
top += loc.x;
left += loc.y;
runProcess = true;
Thread th = new Thread(new AddTask());
th.setDaemon(false);
th.setPriority(Thread.MIN_PRIORITY);
th.start();
}
private class AddTask implements Runnable {
@Override
public void run() {
while (runProcess) {
for (int j = 0; j < 6; j++) {
if (j % 2 == 0) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SecondDialog secondDialog = new SecondDialog();
}
});
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FirstDialog firstDialog = new FirstDialog();
}
});
}
Window[] wins = Window.getWindows();
for (int i = 0; i < wins.length; i++) {
if (wins[i] instanceof JFrame) {
System.out.print("JFrame");
} else if (wins[i] instanceof JDialog) {
System.out.print(", JDialog");
} else if (wins[i] instanceof JWindow) {
System.out.print(", JWindow");
}
}
System.out.println(" ");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
runProcess = false;
}
remWins();
}
}
public void remWins() {
runProcess = true;
Thread th = new Thread(new RemTask());
th.setDaemon(false);
th.setPriority(Thread.MIN_PRIORITY);
th.start();
}
private class RemTask implements Runnable {
@Override
public void run() {
while (runProcess) {
Window[] wins = Window.getWindows();
for (int i = 0; i < wins.length; i++) {
if (wins[i] instanceof JFrame) {
System.out.print("JFrame");
} else if (wins[i] instanceof JDialog) {
System.out.print(", Remove JDialog");
wins[i].setVisible(false);
wins[i].dispose();
WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING);
wins[i].dispatchEvent(windowClosing);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
Runtime runtime = Runtime.getRuntime();
runtime.gc();
runtime.runFinalization();
} else if (wins[i] instanceof JWindow) {
System.out.print(", Remove JWindow");
wins[i].setVisible(false);
wins[i].dispose();
WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING);
wins[i].dispatchEvent(windowClosing);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
Runtime runtime = Runtime.getRuntime();
runtime.gc();
runtime.runFinalization();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(" Remove Done ");
Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
System.out.println(" Checking if still exists any of TopLayoutContainers ");
Window[] wins1 = Window.getWindows();
for (int i = 0; i < wins1.length; i++) {
if (wins1[i] instanceof JFrame) {
System.out.print("JFrame");
wins1[i].setVisible(true);
} else if (wins1[i] instanceof JDialog) {
System.out.print(", JDialog");
wins1[i].setVisible(true);
} else if (wins1[i] instanceof JWindow) {
System.out.print(", JWindow");
wins1[i].setVisible(true);
}
}
System.out.println(" ");
System.out.println(" No Idea, That's it ");
runProcess = false;
}
}
}
private class FirstDialog extends JWindow {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent) {
super(parent);
Point loc = parent.getLocation();
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private FirstDialog() {
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
private class SecondDialog extends JDialog {
private static final long serialVersionUID = 1L;
SecondDialog(final Frame parent) {
super(parent, "SecondDialog " + (i++));
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.MODELESS);
pack();
setVisible(true);
}
private SecondDialog() {
setTitle("SecondDialog " + (i++));
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.MODELESS);
pack();
setVisible(true);
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}
}
as I tried, looks like as that isn't possible, without success as I tried, or exist there another way?
import java.awt.*;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class SuperConstructor extends JFrame {
private static final long serialVersionUID = 1L;
private int i = 1;
private boolean runProcess;
private int top = 20;
private int left = 20;
public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
setLocation(150, 150);
pack();
setVisible(true);
Point loc = this.getLocation();
top += loc.x;
left += loc.y;
runProcess = true;
Thread th = new Thread(new AddTask());
th.setDaemon(false);
th.setPriority(Thread.MIN_PRIORITY);
th.start();
}
private class AddTask implements Runnable {
@Override
public void run() {
while (runProcess) {
for (int j = 0; j < 6; j++) {
if (j % 2 == 0) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SecondDialog secondDialog = new SecondDialog();
}
});
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FirstDialog firstDialog = new FirstDialog();
}
});
}
Window[] wins = Window.getWindows();
for (int i = 0; i < wins.length; i++) {
if (wins[i] instanceof JFrame) {
System.out.print("JFrame");
} else if (wins[i] instanceof JDialog) {
System.out.print(", JDialog");
} else if (wins[i] instanceof JWindow) {
System.out.print(", JWindow");
}
}
System.out.println(" ");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
runProcess = false;
}
remWins();
}
}
public void remWins() {
runProcess = true;
Thread th = new Thread(new RemTask());
th.setDaemon(false);
th.setPriority(Thread.MIN_PRIORITY);
th.start();
}
private class RemTask implements Runnable {
@Override
public void run() {
while (runProcess) {
Window[] wins = Window.getWindows();
for (int i = 0; i < wins.length; i++) {
if (wins[i] instanceof JFrame) {
System.out.print("JFrame");
} else if (wins[i] instanceof JDialog) {
System.out.print(", Remove JDialog");
wins[i].setVisible(false);
wins[i].dispose();
WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING);
wins[i].dispatchEvent(windowClosing);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
Runtime runtime = Runtime.getRuntime();
runtime.gc();
runtime.runFinalization();
} else if (wins[i] instanceof JWindow) {
System.out.print(", Remove JWindow");
wins[i].setVisible(false);
wins[i].dispose();
WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING);
wins[i].dispatchEvent(windowClosing);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
Runtime runtime = Runtime.getRuntime();
runtime.gc();
runtime.runFinalization();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(" Remove Done ");
Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
System.out.println(" Checking if still exists any of TopLayoutContainers ");
Window[] wins1 = Window.getWindows();
for (int i = 0; i < wins1.length; i++) {
if (wins1[i] instanceof JFrame) {
System.out.print("JFrame");
wins1[i].setVisible(true);
} else if (wins1[i] instanceof JDialog) {
System.out.print(", JDialog");
wins1[i].setVisible(true);
} else if (wins1[i] instanceof JWindow) {
System.out.print(", JWindow");
wins1[i].setVisible(true);
}
}
System.out.println(" ");
System.out.println(" No Idea, That's it ");
runProcess = false;
}
}
}
private class FirstDialog extends JWindow {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent) {
super(parent);
Point loc = parent.getLocation();
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private FirstDialog() {
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
private class SecondDialog extends JDialog {
private static final long serialVersionUID = 1L;
SecondDialog(final Frame parent) {
super(parent, "SecondDialog " + (i++));
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.MODELESS);
pack();
setVisible(true);
}
private SecondDialog() {
setTitle("SecondDialog " + (i++));
setLocation(top, left);
top += 20;
left += 20;
setPreferredSize(new Dimension(200, 200));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.MODELESS);
pack();
setVisible(true);
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少,您的 RemTask 应该在 EDT 上运行,而不是在某个任意线程上运行(也对该任务使用 invokeLater)。我非常怀疑您是否应该在 EDT 之外调用
Windows.getWindows()
。At the very least, your RemTask should be running on the EDT, not some arbitrary thread (use invokeLater for that task as well). I very much doubt you should be calling
Windows.getWindows()
outside of the EDT either.