setUndecorated(true)为从JOptionPane实例创建的JDialog [英] setUndecorated(true) to a JDialog created from an instance of JOptionPane
问题描述
我目前有一个 JDialog
是通过从我的<$ c $实例调用 createDialog()
方法创建的c> JOptionPane :
I currently have a JDialog
created by calling the createDialog()
method from my instance of JOptionPane
:
JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
dialog = pane.createDialog(null, "");
我希望能够从 JDialog中删除标题栏
通过将 setUndecorated(true)
调用到 JDialog
,但我得到 IllegalComponentStateException:当我尝试运行程序时,该对话框是可显示的
异常。
I wanted to be able to remove the title bar from the JDialog
by calling setUndecorated(true)
onto the JDialog
, but I get an IllegalComponentStateException: The dialog is displayable
exception when I attempt to run my program.
据我所知,在调用 dialog.show()
之前,对话框没有显示,让我相信在通过 pane.createDialog()
实例化对话框时,对话框确实是可显示的远远超出了我对 JDialog的理解
API。
As far as I know, the dialog is not being displayed before I call dialog.show()
, which leads me to believe that the dialog is indeed "displayable" upon instantiating the dialog through pane.createDialog()
far beyond my understanding of the JDialog
API.
在使用<$ c之前,我试图调用 setVisible(false)
$ c> setUndecorated(true),但无济于事。
I have attempted to call setVisible(false)
prior to using setUndecorated(true)
, but to no avail.
如果可以删除 JDialog
这种类型。从正常的 JDialog
中删除标题栏很容易,从许多其他类型问题的答案中可以看出,但我似乎无法让它为<$ $工作c $ c> JDialog 通过 createDialog()
创建。
Any help would be appreciated as to how or of it is possible at all to remove the title bar of a JDialog
of this type. Removing the title bar from a normal JDialog
is easy enough, as seen from numerous other answers to questions of this type, but I cannot seem to get it to work for a JDialog
created through createDialog()
.
相关代码:
input= new JTextField(50);
input.addKeyListener(new ConsoleKeyListener());
input.addAncestorListener( new RequestFocusListener() );
field = new JTextArea();
field.setEditable(false);
field.setLineWrap(true);
JScrollPane area = new JScrollPane(field, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
field.setRows(10);
field.setText(consoleText);
JPanel myPanel = new JPanel();
myPanel.setLayout(new BorderLayout(0,0));
myPanel.add(input, BorderLayout.PAGE_END);
myPanel.add(area, BorderLayout.PAGE_START);
input.setFocusable(true);
input.requestFocus();
int result = 101;
//int result = JOptionPane.showOptionDialog(null, myPanel,"", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
dialog = pane.createDialog(null, "");
dialog.setVisible(false);
dialog.setUndecorated(true);
//dialog.undecorated = true;
//dialog.setOpacity(0.55f);
removeMinMaxClose(dialog);
removeMinMaxClose(pane);
removeMinMaxClose(myPanel);
dialog.getRootPane().setOpaque(false);
//JDialog dialog = new JDialog();
//dialog.setVisible(false);
//dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//myPanel.setUndecorated(true);
//dialog.setUndecorated(true);
//dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
//dialog.setBounds( 100, 100, 300, 200 );
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.out.println("yo");
}
});
dialog.setVisible(true);
dialog.show();
推荐答案
你需要阅读 Component#isDisplayable上的JavaDoc条目,然后有查看创建对话框的源代码
You need to read the JavaDoc entry on Component#isDisplayable, then have a look at the source code for create dialog
组件在添加到可显示的包含层次结构时可显示或者当它的包含层次结构是
可显示时。当
祖先窗口打包或显示时,可以显示包含层次结构。
"A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible."
基本上对话框是作为 createDialog
方法的一部分打包的
Basically the dialog is packed as part of the createDialog
method
可能的解决方案
可能的解决方案是创建自己的对话框......
A possible solution is to create your own dialog...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane11 {
public static void main(String[] args) {
new TestOptionPane11();
}
public TestOptionPane11() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JDialog dialog = new JDialog((Frame)null, "Boo");
JOptionPane op = new JOptionPane("Look ma, no hands", JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
op.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if ("value".equals(name)) {
dialog.dispose();
}
}
});
dialog.setUndecorated(true);
dialog.setLayout(new BorderLayout());
dialog.add(op);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
});
}
}
这篇关于setUndecorated(true)为从JOptionPane实例创建的JDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!