我对扩展 JDialog 的类有一个奇怪的问题:
class MyDialog extends JDialog {
private static final long serialVersionUID = 11564288421L;
public MyDialog(JFrame owner, MyObject object) {
super(owner, true);
setSize(300, 200);
setLocationRelativeTo(owner);
String title = "Object ID: " + object.getId();
setTitle(title);
setVisible(true);
JLabel lblTitle = new JLabel(title);
lblTitle.setBounds(0, 0, this.getWidth(), 22);
lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().setLayout(null);
getContentPane().add(lblTitle);
}
}
当我用 , 调用它时new MyDialog(existingJFrameInstance, existingMyObjectInstance);
,MyDialog
它会显示并且标题很好,但它的ContentPane
.
如果我将课程更改为:
class MyDialog extends JFrame {
private static final long serialVersionUID = 11564288421L;
public MyDialog(JFrame owner, MyObject object) {
super();
setSize(300, 200);
setLocationRelativeTo(owner);
String title = "Object ID: " + object.getId();
setTitle(title);
setVisible(true);
JLabel lblTitle = new JLabel(title);
lblTitle.setBounds(0, 0, this.getWidth(), 22);
lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().setLayout(null);
getContentPane().add(lblTitle);
}
}
一切正常。我无法想象这里发生了什么:\
我需要将 Window 设为 Modal,这就是为什么我必须使用JDialog
而不是JFrame
.
我尝试在每种可能的组合中使用this.pack();
, this.validate();
, getContentPane().revalidate();
, getContentPane().repaint();
etc etc etc 并且我在 EDT 中(SwingUtilities.isEventDispatchThread()
返回true
)。
请帮忙 :)