Swing窗口切换的问题 今天遇到一个问题,Swing的高手们能不能帮忙分析一下原因。在一个JFrame里面弹出一个模态Dialog A,这个Dialog里面有些控件,可以继续出发弹出新的模态Dialog B,偶尔会出现切换到该窗口的时候只显示B,底层的JFrame和A都不能显示的问题。是不是和2个模态窗口有关?该怎么解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没设父窗体的问题你应该把弹出的对话框的 onwer 设为弹出它的那个窗体 Dialog的构造函数. 使用传入父窗体的构造. 不要使用默认的无参数构造.即可. 看了一下父窗口设置的都是对的A的父窗口是JFrame,B的父窗口是A。但是还是有问题,是偶发不是必现 随便乱写了个,貌似没发现LZ你说的不显示的问题。基本解决方法上面有人说了。owner的指定。import java.awt.Component;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JPanel;public class DialogTest extends JFrame implements ActionListener { private JDialog d1; private JDialog d2; public DialogTest() { init(); } private void init() { this.getContentPane().add(initPanel()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 400); } private Component initPanel() { JPanel panel = new JPanel(); JButton b = new JButton("Dialog1"); b.addActionListener(this); panel.add(b); return panel; } public static void main(String[] args) { new DialogTest(); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Dialog1")) { JButton b = new JButton("Dialog2"); b.addActionListener(this); d1 = new JDialog(this, "Im dialog one!", true); d1.add(b); d1.setSize(200, 100); d1.setVisible(true); } else if(e.getActionCommand().equals("Dialog2")) { d2 = new JDialog(d1, "Im dialog two!", true); d2.setSize(200, 50); d2.setVisible(true); } }} import java.awt.Dialog;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JPanel;public class Main { private static JFrame frame = null; /** * @param args */ public static void main(String[] args) { initJFrame(); addComponent2Window(); frame.setVisible(true); } private static void initJFrame(){ frame = new JFrame("测试窗口"); frame.setBounds(new Rectangle(100, 100, 800, 600)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static void addComponent2Window(){ JPanel pane = new JPanel(); final JButton btn = new JButton(); JButton btn2 = new JButton(); final JDialog dialog = new JDialog(frame,Dialog.ModalityType.APPLICATION_MODAL); final JDialog dialog1 = new JDialog(dialog,Dialog.ModalityType.APPLICATION_MODAL); dialog.setPreferredSize(new Dimension(200, 200)); dialog.setMinimumSize(new Dimension(200, 200)); dialog.setResizable(false); dialog.setTitle("dialog"); dialog.setModal(true); dialog.add(btn2); dialog1.setPreferredSize(new Dimension(200, 100)); dialog1.setMinimumSize(new Dimension(200, 100)); dialog1.setTitle("dialog1"); dialog1.setModal(true); btn.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { super.mouseClicked(arg0); dialog.setVisible(true); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); } }); btn2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { super.mouseClicked(arg0); dialog1.setVisible(true); btn.setText("hhe"); } }); pane.add(btn); frame.getContentPane().add(pane); }}大概就是这样,偶尔把swing窗口切换到最小化时就会出现鼠标在任务栏上激活swing窗口就只显示最外层的dialog1,把dialog1关掉底下的dialog和jframe就马上出来了 公司的代码拷不出来,大概就是这个结构JFrame,dialog,dialog(每个dialog上面还会有些table等控件) 我的平台:window server2003 x86 JDK版本:java version "1.6.0_17"没发现你说的情况咧。打开模式对话框后,只能通过win + d 键隐藏窗体。任务栏上点击swing窗口图标后可以正常显示回原来的状态。 我觉得 我上面随手写的那个例子不存在这个问题我读了你的代码,发现那两个dialog是与其它组件同级生成的,之后只是调用setVisible做显示。dialog始终是同一份拷贝。我认为从业务上来说,应该每次都生成新的dialog吧,用完就消毁。 可变参数的问题 用Byte[]构造SerialBlob出问题了 大家用过window这个类吗? 关于JNI本地类型的问题. 关于list的问题 初学者关于Vector的问题,分不多,但请各位大侠帮帮忙 请教高手:我用jcreator学习编程,但不知如何对程序打包? Exception in thread "main" java.lang.NoClassDefFoundError:alysor 关于ImageObserver的问题(100分) 请教一个关于Collections.nCopies的问题 关于log4j使用常识的一个问题求解。 我是初学java的 发个代码问下,别喷我好么。
你应该把弹出的对话框的 onwer 设为弹出它的那个窗体
不要使用默认的无参数构造.即可.
看了一下父窗口设置的都是对的A的父窗口是JFrame,B的父窗口是A。但是还是有问题,是偶发不是必现
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DialogTest extends JFrame implements ActionListener {
private JDialog d1;
private JDialog d2;
public DialogTest() {
init();
} private void init() {
this.getContentPane().add(initPanel());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 400);
} private Component initPanel() {
JPanel panel = new JPanel();
JButton b = new JButton("Dialog1");
b.addActionListener(this);
panel.add(b);
return panel;
}
public static void main(String[] args) {
new DialogTest();
} @Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Dialog1")) {
JButton b = new JButton("Dialog2");
b.addActionListener(this);
d1 = new JDialog(this, "Im dialog one!", true);
d1.add(b);
d1.setSize(200, 100);
d1.setVisible(true);
} else if(e.getActionCommand().equals("Dialog2")) {
d2 = new JDialog(d1, "Im dialog two!", true);
d2.setSize(200, 50);
d2.setVisible(true);
}
}
}
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Main {
private static JFrame frame = null;
/**
* @param args
*/
public static void main(String[] args) {
initJFrame();
addComponent2Window();
frame.setVisible(true);
} private static void initJFrame(){
frame = new JFrame("测试窗口");
frame.setBounds(new Rectangle(100, 100, 800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static void addComponent2Window(){
JPanel pane = new JPanel();
final JButton btn = new JButton();
JButton btn2 = new JButton();
final JDialog dialog = new JDialog(frame,Dialog.ModalityType.APPLICATION_MODAL);
final JDialog dialog1 = new JDialog(dialog,Dialog.ModalityType.APPLICATION_MODAL);
dialog.setPreferredSize(new Dimension(200, 200));
dialog.setMinimumSize(new Dimension(200, 200));
dialog.setResizable(false);
dialog.setTitle("dialog");
dialog.setModal(true);
dialog.add(btn2);
dialog1.setPreferredSize(new Dimension(200, 100));
dialog1.setMinimumSize(new Dimension(200, 100));
dialog1.setTitle("dialog1");
dialog1.setModal(true);
btn.addMouseListener(new MouseAdapter() { @Override
public void mouseClicked(MouseEvent arg0) {
super.mouseClicked(arg0);
dialog.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
});
btn2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
super.mouseClicked(arg0);
dialog1.setVisible(true);
btn.setText("hhe");
}
});
pane.add(btn);
frame.getContentPane().add(pane);
}
}大概就是这样,偶尔把swing窗口切换到最小化时就会出现鼠标在任务栏上激活swing窗口就只显示最外层的dialog1,把dialog1关掉底下的dialog和jframe就马上出来了
没发现你说的情况咧。
打开模式对话框后,只能通过win + d 键隐藏窗体。任务栏上点击swing窗口图标后可以正常显示回原来的状态。
我读了你的代码,发现那两个dialog是与其它组件同级生成的,之后只是调用setVisible做显示。dialog始终是同一份拷贝。我认为从业务上来说,应该每次都生成新的dialog吧,用完就消毁。