我想创建一个程序,在不同位置同时显示多个弹出窗口。
该程序创建向右移动的弹出窗口,并向左移动一个 jFrame。
我希望两个框架都是弹出窗口。
这是我的代码:
import javax.swing.*;
import java.awt.*;
public class jFrame{
public static void createWindow(){
JFrame frame = new JFrame();
JFrame frame2 = new JFrame();
frame.setVisible(true);
frame.setLocation(100,500);
frame2.setVisible(true);
frame2.setLocation(900,500);
int x = 0;
int y = 5;
while(y>x){
int reply = JOptionPane.showConfirmDialog(frame, "Do you wanna get rid of this popup?", "Message", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showConfirmDialog(frame, "Sike \n You thought", "Message", JOptionPane.OK_CANCEL_OPTION);
}
int reply2 = JOptionPane.showConfirmDialog(frame2, "Do you wanna get rid of this popup?", "Message", JOptionPane.YES_NO_OPTION);
if (reply2 == JOptionPane.YES_OPTION) {
JOptionPane.showConfirmDialog(frame2, "Sike \n You thought", "Message", JOptionPane.OK_CANCEL_OPTION);
}
}
}
public static void main(String[] args){
createWindow()
}
}
最佳答案
首先查看 How to Use Modality in Dialogs
基本上,对话框的默认模式(当 setModal
为 true
时)是阻止所有顶级容器。例如,您可以通过更改对话框的 ModalityType
来控制它
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DialogTest {
public static void main(String[] args) {
new DialogTest();
}
public DialogTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = device.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocation(bounds.x + ((bounds.width / 2) - frame.getWidth()),
bounds.y + ((bounds.height / 2) - frame.getHeight()));
frame.setVisible(true);
JFrame frame2 = new JFrame("Testing");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(new TestPane());
frame2.pack();
frame2.setLocation(bounds.x + ((bounds.width / 2)),
bounds.y + ((bounds.height / 2) - frame2.getHeight()));
frame2.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JButton popup = new JButton("Popup");
add(popup);
popup.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(TestPane.this));
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(new JLabel("I'll be your dialog today"), gbc);
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.windowForComponent(close).dispose();
}
});
panel.add(close, gbc);
dialog.add(panel);
dialog.pack();
dialog.setLocationRelativeTo(TestPane.this);
dialog.setVisible(true);
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
现在,在您询问是否让 JOptionPane
以这种方式工作之前,答案基本上是否定的。
您“可以”对 JOptionPane
进行自定义扩展,它会覆盖两个 createDialog
方法,但您将无法使用 static
辅助方法,因为它们创建 JOptionPane
关于java - 使用java中的JOptionPane是否可以同时显示多个弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33948856/