1

因此,如果我有这样的 GUI:

import javax.swing.GroupLayout;
public class MyTest extends javax.swing.JFrame {

    public MyTest() {
        initComponents();
    }

    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("This is a sample GUI");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 244, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 85, Short.MAX_VALUE)
        );

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyTest().setVisible(true);
            }
        });
    }   
}

现在,当我运行程序时,它与屏幕左上角的 (0,0) 位置对齐。无论如何我可以将它与屏幕中心或另一个自定义位置对齐吗?
如果我只使用一个框架而不使用 GroupLayout,这很好用,比如 setLocation(left,top),但是通过这个实现,我如何更改这个 GUI 的默认定位?

4

3 回答 3

4

将带有 a 的 contianerGroupLayout作为 a 的单个组件,GridBagLayout没有约束。整个容器将居中。


我只需要将整个 GUI(其中包含所有内容)居中到碎石的中心,

That has nothing to do with GroupLayout and everything to do with setLocationRelativeTo(null). But read How to best position Swing GUIs for an even better way to position a GUI on-screen (i.e. at a more sensible place than the center of the screen - example below).

于 2011-12-09T10:33:14.913 回答
3

Like I mentioned in your earlier question, before I thought you were referring to centering the frame itself - use a GridBagLayout with no constraint. That will have the effect of putting the component in the center of the parent container. Note that since a frame has window decorations, that means the 'parent container' itself is slightly below screen center. If that is a problem, the only 'fix' I can think of is to use an undecorated frame.

I put 'fix' in inverted commas, because I think it is the requirement that is broken.

Centered GroupLayout

import java.awt.*;
import javax.swing.*;

public class MyTest extends JFrame {

    public MyTest() {
        initComponents();
    }

    private void initComponents() {

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("This is a sample GUI");
        getContentPane().setLayout(new GridBagLayout());

        JPanel gui = new JPanel();
        gui.setBackground(Color.GREEN);

        GroupLayout layout = new GroupLayout(gui);
        gui.setLayout(layout);

        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 244, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 85, Short.MAX_VALUE)
        );

        add(gui);

        pack();
        setExtendedState(Frame.MAXIMIZED_BOTH );
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyTest().setVisible(true);
            }
        });
    }
}
于 2011-12-12T12:16:55.190 回答
0

I guess you did answer the question but it wasn't that clear to me, regarding how to use it. Simply by calling below before pack(); was exactly what i was looking for:

this.setLocation(500,300); //value left, top in pixes
pack();  

So basically what i needed to know was the exact use of setLocation(x,y); and that with the use of keyword "this", thank you.

于 2011-12-15T14:39:04.307 回答