如何将表格放置在屏幕中央?

我是一个.Net开发人员,但不知何故,我有一个额外的原因,在Java中创build一个简单的应用程序的任务。 我能够创build该应用程序,但我的问题是如何在应用程序启动时在屏幕中居中表单?

这是我的代码:

private void formWindowActivated(java.awt.event.WindowEvent evt) { // Get the size of the screen Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // Determine the new location of the window int w = this.getSize().width; int h = this.getSize().height; int x = (dim.width-w)/2; int y = (dim.height-h)/2; // Move the window this.setLocation(x, y); } 

上面的代码工作正常,但问题是我已经看到从最前面的窗体移动到中心屏幕。 我也尝试在formWindowOpened事件中添加该代码,并仍然显示相同的操作。 有没有更好的办法呢? 就像在.NET Application有一个CenterScreen Position 。 或者,如果上面的代码是正确的,我会把它放在什么事件上?

感谢您阅读此。

在JFrame上调用pack之后,只需将location设置为null,就是这样。

例如,

  JFrame frame = new JFrame("FooRendererTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); // or whatever... frame.pack(); frame.setLocationRelativeTo(null); // *** this will center your app *** frame.setVisible(true); 

以下示例将屏幕中的一个框架居中:

 package com.zetcode; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GraphicsEnvironment; import java.awt.Point; import javax.swing.JFrame; public class CenterOnScreen extends JFrame { public CenterOnScreen() { initUI(); } private void initUI() { setSize(250, 200); centerFrame(); setTitle("Center"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void centerFrame() { Dimension windowSize = getSize(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Point centerPoint = ge.getCenterPoint(); int dx = centerPoint.x - windowSize.width / 2; int dy = centerPoint.y - windowSize.height / 2; setLocation(dx, dy); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { CenterOnScreen ex = new CenterOnScreen(); ex.setVisible(true); } }); } } 

为了在屏幕上居中框架,我们需要获得本地graphics环境。 从这个环境,我们确定中心点。 结合框架大小,我们设法将框架居中。 setLocation()是将框架移动到中心位置的方法。

请注意,这实际上是setLocationRelativeTo(null)所做的:

 public void setLocationRelativeTo(Component c) { // target location int dx = 0, dy = 0; // target GC GraphicsConfiguration gc = getGraphicsConfiguration_NoClientCode(); Rectangle gcBounds = gc.getBounds(); Dimension windowSize = getSize(); // search a top-level of c Window componentWindow = SunToolkit.getContainingWindow(c); if ((c == null) || (componentWindow == null)) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); gcBounds = gc.getBounds(); Point centerPoint = ge.getCenterPoint(); dx = centerPoint.x - windowSize.width / 2; dy = centerPoint.y - windowSize.height / 2; } ... setLocation(dx, dy); } 

改变这个:

 public FrameForm() { initComponents(); } 

对此:

 public FrameForm() { initComponents(); this.setLocationRelativeTo(null); } 
 public class Example extends JFrame { public static final int WIDTH = 550;//Any Size public static final int HEIGHT = 335;//Any Size public Example(){ init(); } private void init() { try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); SwingUtilities.updateComponentTreeUI(this); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); setSize(WIDTH, HEIGHT); setLocation((int) (dimension.getWidth() / 2 - WIDTH / 2), (int) (dimension.getHeight() / 2 - HEIGHT / 2)); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } } } 

我希望这会有所帮助。

把这个放在源代码的顶部:

 import java.awt.Toolkit; 

然后写下这段代码:

 private void formWindowOpened(java.awt.event.WindowEvent evt) { int lebar = this.getWidth()/2; int tinggi = this.getHeight()/2; int x = (Toolkit.getDefaultToolkit().getScreenSize().width/2)-lebar; int y = (Toolkit.getDefaultToolkit().getScreenSize().height/2)-tinggi; this.setLocation(x, y); } 

祝你好运 :)

其实,你并不需要编写代码才能使表单进入中间屏幕。

只要修改jframe的属性即可
按照以下步骤修改:

  • 右键单击表单
  • FormSize策略更改为 – 生成resize的代码
  • 然后编辑表格位置X -200 Y-200

你完成了。 为什么要采取编码的痛苦。 🙂

如果您使用NetBeans IDE,请右键单击表单

属性 – >代码 – >检出生成中心

使用这个函数你可以定义你的赢的位置

 setBounds(500, 200, 647, 418); 

尝试使用这个:

 this.setBounds(x,y,w,h);