我有自己的主框架和两个JDialogs,当有人选择菜单时,items.What会弹出,我希望有一个带有一些规则、建议等的帮助对话框。setLayout(null)和Toolkit/维度当然没有帮助,因为它们集中在屏幕上的框架/对话框的左上角。如何使画面和对话框的中心都以屏幕为中心?提前谢谢!
发布于 2013-08-10 18:31:23
只有在setLocationRelativeTo(null)还没有大小的情况下,Dialog才会在左上角居中,例如,如果您在setLocationRelativeTo之后调用.pack()方法。
小例子:
JFrame testFrame = new JFrame();
JButton testButton = new JButton();
testButton.setPreferredSize(new Dimension(500, 500));
testFrame.add(testButton);
testFrame.pack();
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);这将显示一个以屏幕为中心的帧。
发布于 2013-08-10 18:35:59
这样做是为了:
1)在要显示的框架/对话框上调用pack()或setSize(...) (此时应该已经添加了其组件)。
2)然后,调用该容器上的setLocationRelativeTo(null)。或者,如果父进程被偏移,并且希望对话集中在父对话框的当前位置,则可以调用setLocationRelativeTo(parent)。
3)然后,调用setVisible(true)
发布于 2013-08-10 18:29:39
我认为这会有帮助:
frame.setLocationRelativeTo(null);通常我们将null作为参数,但是您可以使用Component类型的参数,我更喜欢JOptionPane,因为它总是显示在屏幕的中心。
frame.setLocationRelativeTo(new JOptionPane());
//OR
frame.setLocationRelativeTo(JOptionPane.getRootFrame());null布局(绝对定位),始终使用LayoutManagers。https://stackoverflow.com/questions/18164999
复制相似问题