gpt4 book ai didi

java - java swing中模态窗口之上的模态窗口?

转载 作者:行者123 更新时间:2023-11-30 03:28:16 24 4
gpt4 key购买 nike

我有以下问题:

我有一个充满整个屏幕的主应用程序窗口 (JFrame)。
单击按钮时,会出现一个较小的窗口,让用户输入一些数据。当用户执行此操作时,主窗口不应跳到其前面,也不应允许交互。
解决方案:打开模态 JDialog。我们称之为对话 1。
但是,当单击此对话框中的按钮时,应该会弹出一个新窗口(是/否对话框)..并且再次需要在已经模态的 JDialog 对话框 1 上进行模态操作。尝试这样做,第二个对话框不断消失在第一个对话框后面。
我尝试将 Dialog 1 设为 JFrame,但是,当然,我失去了“模态”位。强制 Dialog 1 保持静止状态以保持主窗口的按钮可点击。
我缺少什么?如何将一个模态 Swing 窗口放在另一个模态 Swing 窗口之上?

编辑:一个无法正常工作的版本的最小示例:

打开的主类:

public class MainWin extends JFrame {

public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}

}

主窗口:

 public class MainWin extends JFrame {

public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}

第一个对话:

public class Dia1 extends JDialog {


public Dia1(final JFrame parent){
super(parent, true);
this.setSize(400, 400);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Dia2(parent);
}
});
}
}

第二个对话框:

public class Dia2 extends JDialog {


public Dia2(JFrame parent){
super(parent, true);
this.setSize(200, 200);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
}

}

PS:我刚刚意识到:正如我怀疑的那样,对话框 2 并没有被隐藏..它根本不存在。很可能是因为父窗口被模式对话框阻止了?

最佳答案

这是一种按广告方式工作的 MCVE 模式。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ModalOverModal {

private JComponent ui = null;

ModalOverModal() {
initUI();
}

public void initUI() {
if (ui!=null) return;

ui = new JPanel(new GridLayout());
ui.setBorder(new EmptyBorder(40,40,40,40));

final JButton b1 = new JButton("Open Modal Dialog");
b1.setMargin(new Insets(40, 200, 40, 200));
ui.add(b1);

final JButton b2 = new JButton("Open 2nd Modal Dialog");

ActionListener al1 = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, b2);
}
};
b1.addActionListener(al1);

ActionListener al2 = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b2, "Close Me!");
}
};
b2.addActionListener(al2);
}

public JComponent getUI() {
return ui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ModalOverModal o = new ModalOverModal();

JFrame f = new JFrame("Modal over Modal");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - java swing中模态窗口之上的模态窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com