我有一个基于Swing的Java应用程序需要显示JDialog
。
这里我包含了示例代码:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JDialogHide {
public void buildGUI() {
final JFrame f = new JFrame("JFrame");
// f.setResizable(false);
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("SHow Dialog");
p.add(btn,new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JDialog jdialog = new JDialog(f, "JDialog");
jdialog.setPreferredSize(new Dimension(300,200));
jdialog.add(new JLabel("Test"));
jdialog.setLocationRelativeTo(f);
jdialog.pack();
jdialog.setVisible(true);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new JDialogHide().buildGUI();
}
});
}
}
我的问题是,当用户点击“显示桌面”时,对话框会自动隐藏,如果他们点击任务栏中的框架,则会再次显示该对话框。
即,
步骤:
注意:我使用的是Ubuntu 14.04 LTS
EDIT1: JDialog jdialog = new JDialog((Window) null, "JDialog");
上面的代码解决了我的jdialog问题,但我找不到对话框中心。
解决方案:交换pack()和setLocationRelativeTo()。现在,对话框位于框架的中心。
EDIT2:在JOptionPane上出现同样的问题。
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JDialogHide {
public void buildGUI() {
final JFrame f = new JFrame("JFrame");
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("SHow Dialog");
p.add(btn,new GridBagConstraints());
f.getContentPane().add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JDialog jdialog = new JDialog((Window) null, "JDialog");
jdialog.setPreferredSize(new Dimension(300,200));
JPanel p = new JPanel(new GridBagLayout());
JButton btn = new JButton("Show showOptionDialog");
p.add(btn,new GridBagConstraints());
jdialog.getContentPane().add(p);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showOptionDialog(jdialog, "Test ?", "TEST ", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
}
});
jdialog.pack();
jdialog.setLocationRelativeTo(null);
jdialog.setVisible(true);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new JDialogHide().buildGUI();
}
});
}
}
任何想法,如何为JOptionPane修复它?
先谢谢!!!