我是学习javax.swing
的新手并且有疑问。
我有一个JFrame
,其defaultCloseOperation
设置为EXIT_ON_CLOSE
。我有另一个组件 - JOptionPane
- 超过 JFrame
。我想要发生的是,即使焦点在JOptionPane
消息对话框上,我也希望程序在我点击{{1}上的 x
按钮时终止窗口。
所以,确切地说,我希望将JFrame
置于焦点而不会忽略JFrame
消息对话框,以便我可以关闭JOptionPane
窗口,因此,使程序终止。
这是我的代码:
JFrame
使用当前代码,当我点击import javax.swing.*;
public class JJSS {
public JFrame jf;
public JJSS(){
jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 400);
jf.setVisible(true);
}
public void runMethod(){
String str = JOptionPane.showInputDialog(jf, "Enter something...");
str = String.valueOf(new StringBuffer(str).reverse());
JOptionPane.showMessageDialog(jf, "Reversed: "+str, "Output", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args){
new JJSS().runMethod();
System.exit(0);
}
}
窗口的关闭按钮( x
)时,没有任何反应。
当JOptionPane对话框仍在其上时,如何将焦点放在JFrame窗口上,并通过关闭JFrame窗口终止程序?
答案 0 :(得分:5)
您无法使用普通的JOptionPane或任何模式对话框执行此操作,因为模式会阻止用户在显示对话框时与GUI的其他组件交互。如果创建非模态对话框,只能使用JOptionPane静态工厂方法创建JOptionPane,而不是使用JOptionPane构造函数以非传统方式创建JOptionPane,则只能使其工作 - 检查{{ 3}}如何做到这一点。
例如:
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class NonModalJOptionPane {
private static void createAndShowGui() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
final JFrame frame = new JFrame("NonModalJOptionPane");
panel.add(new JButton(new AbstractAction("Push Me") {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE);
JDialog dialog = optionPane.createDialog(frame, "My Option");
dialog.setModalityType(ModalityType.MODELESS); // **** key ***
dialog.setVisible(true);
}
}));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
此代码的关键在于:
// create the JOptionPane using one of its constructors
JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE);
// create a JDialog from it, tying it to the parent JFrame, here called "frame"
JDialog dialog = optionPane.createDialog(frame, "My Option");
// setting the modality type so that it is **not** modal
dialog.setModalityType(ModalityType.MODELESS); // **** key ***
// and then displaying it
dialog.setVisible(true);
我通过构造函数而不是通过静态方法创建JOptionPane,我创建了一个JDialog并将其设置为 MODELESS ,然后显示它。
另一个可行的选择是创建自己的JDialog,确保将其设置为非模态。
例如,您可以将以下代码添加到上面的代码中:
panel.add(new JButton(new AbstractAction("Push Me 2 -- Using Dialog") {
@Override
public void actionPerformed(ActionEvent e) {
// button that when pressed, closes the JDialog that holds it
// similar to a JOptionPane's OK button
JButton disposeWinBtn = new JButton(new DisposeWindowAction("OK", KeyEvent.VK_O));
// create a bunch of JPanels, add components to them, ...
JPanel bottomPanel = new JPanel();
bottomPanel.add(disposeWinBtn);
JLabel msgLabel = new JLabel("My Message");
JPanel msgPanel = new JPanel();
msgPanel.add(msgLabel);
JPanel panel = new JPanel(new BorderLayout());
panel.add(msgPanel, BorderLayout.CENTER);
panel.add(bottomPanel, BorderLayout.PAGE_END);
// create a JDialog whose parent component is the main JFrame
// and make sure that it is *****non-modal ***** <===== this is KEY *****
JDialog dialog = new JDialog(frame, "Dialog", ModalityType.MODELESS);
dialog.add(panel); // add the JPanel, panel, created above, with components
dialog.pack(); // have layout managers do their thing
dialog.setLocationRelativeTo(frame); // center it over the main JFrame
dialog.setVisible(true); // and display it
}
}));
在添加第一个按钮的位置。您还需要DisposeWindowAction类,它允许按钮关闭并处理显示它的窗口(这里是一个JDialog窗口):
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class DisposeWindowAction extends AbstractAction {
public DisposeWindowAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
if (component == null) {
return;
}
Window win = SwingUtilities.getWindowAncestor(component);
if (win == null) {
return;
}
win.dispose();
}
}