gpt4 book ai didi

java - 无法按链顺序正确关闭 JOptionPane

转载 作者:行者123 更新时间:2023-12-02 00:42:00 25 4
gpt4 key购买 nike

我正在显示一个JOptionPane,假设从JFrame单击按钮A,并再次显示另一个JOptionPane,假设从JOptionPane A单击按钮B,并且我在JOptionPane B上有一个按钮,在按钮1的单击事件上,我正在使用代码 JOptionPane.getRootFrame().dispose() 来关闭 JOptionPane B,但它同时关闭了 A 和 B,请帮助我如何只关闭 B 但不是 A。

这是我的示例代码我希望第二个 JOptionPane 必须打开

import java.awt.Dimension;
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;

public class SampleCode extends JFrame {

public SampleCode() {
setSize(new Dimension(500, 500));
setLocation(450, 150);
but1 = new JButton("Click me");
add(but1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

but1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
but1Function();

}
});
}

public static void main(String args[]) {
new SampleCode();
}

void but1Function() {
JPanel panel1 = new JPanel();
JButton but2 = new JButton("Open new dialog");
panel1.add(but2);

but2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel pan2 = new JPanel();
JButton but3 = new JButton("click me to close");
pan2.add(but3);
but3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.getRootFrame().dispose();

}
});

JOptionPane.showMessageDialog(null, pan2);

}
});

JOptionPane jp = new JOptionPane(panel1, JOptionPane.CLOSED_OPTION,
JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null);

JDialog dialog = jp.createDialog(null, "This one must be remain open");
dialog.setLocation(500, 200);
dialog.setSize(new Dimension(345, 200));
dialog.setVisible(true);
}

JButton but1;

}

最佳答案

您不想获取根框架也不想处置它。您想要获取显示 JOptionPane 的窗口,该窗口应该是模态 JDialog。因此,如果您想以编程方式处置 JOPtionPane,请使用 SwingUtilities.getWindowAncestor(someComponentInJOptionPane),并在该窗口上调用 dispose()

import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class OptionPaneFun {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@SuppressWarnings("serial")
public void run() {
final JPanel panel1 = new JPanel();
panel1.add(new JButton(new AbstractAction("Show new option pane") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
@Override
public void actionPerformed(ActionEvent e1) {
final JPanel panel2= new JPanel();
panel2.add(new JButton(new AbstractAction("Dispose of this option pane") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_D);
}
@Override
public void actionPerformed(ActionEvent e2) {
Component comp = (Component) e2.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
if (win != null) {
win.dispose();
}
}
}));
JOptionPane.showMessageDialog(panel1, panel2);

}
}));
JOptionPane.showMessageDialog(null, panel1);
}
});

}
}

关于java - 无法按链顺序正确关闭 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931587/

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