我想创建一个程序,在不同的位置同时显示多个弹出窗口。
此程序创建弹出窗口向右移动,一个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()
}
}
答案 0 :(得分:2)
首先看一下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
辅助方法,因为它们会创建{{1}的实例1}}
答案 1 :(得分:0)
是的当然是, 唯一的方法是:
让我们想象一下我们有一个面板:myPanel
在myPanel中我们有一个按钮btn1在btn1的eventListner中我们有一个显示JOptionPan pop1的方法 有另一个按钮btn2
我们可以在btn2的eventListner里面显示另一个JOptionPan pop2。
所以有可能。 并且您可以添加自定义的JOptionPans。