用javaswing定位

用javaswing定位,java,swing,alignment,Java,Swing,Alignment,我不太会荡秋千。这是我写的代码 import java.awt.*; import javax.swing.*; public class NewMain { public static void main(String[] args) { // TODO code application logic here JFrame frame = new JFrame("test"); frame.setVisible(true);

我不太会荡秋千。这是我写的代码

import java.awt.*;
import javax.swing.*;
public class NewMain {
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("test");
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        addItem(panel, new JLabel("Label"), 0, 0, GridBagConstraints.EAST);
        frame.add(panel);
    }

    private static void addItem(JPanel p, JComponent gc, int i, int i0, int align) {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = i;
        c.gridy = i0;
        c.anchor = align;
        p.add(gc,c);

    }
当我运行程序时,无论我传递什么作为
align
参数(
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
等),我的标签都在面板的中心对齐

如何更改标签的对齐方式


提前感谢。

这是因为JLabel是GUI中唯一的组件。如果添加更多构件,则将根据其位置相对于构件进行布置。这是程序的扩展,添加了一个空面板:

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


public class NewMain {

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("test");
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        JPanel emptyArea = new JPanel();
        emptyArea.setPreferredSize(new Dimension(200, 200));
        addItem(panel, new JLabel("Label"), 0, 0, GridBagConstraints.WEST);
        addItem(panel, emptyArea, 0, 0, GridBagConstraints.CENTER);
        frame.add(panel);
    }

    private static void addItem(JPanel p, JComponent gc, int i, int i0, int align) {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = i;
        c.gridy = i0;
        c.anchor = align;
        p.add(gc,c);

    }
}

这是因为JLabel是GUI中唯一的组件。如果添加更多构件,则将根据其位置相对于构件进行布置。这是程序的扩展,添加了一个空面板:

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


public class NewMain {

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("test");
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        JPanel emptyArea = new JPanel();
        emptyArea.setPreferredSize(new Dimension(200, 200));
        addItem(panel, new JLabel("Label"), 0, 0, GridBagConstraints.WEST);
        addItem(panel, emptyArea, 0, 0, GridBagConstraints.CENTER);
        frame.add(panel);
    }

    private static void addItem(JPanel p, JComponent gc, int i, int i0, int align) {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = i;
        c.gridy = i0;
        c.anchor = align;
        p.add(gc,c);

    }
}

由于GridBagConstraint中的
weightx
的默认值为零,因此整个布局将尽可能小,并以父组件为中心。使用非零的正
权重x
使组件使用整个水平长度,因此布局将占用所有可用空间。对于垂直方向,重量相同

...
c.anchor = align;
c.weightx = 1.0:
...

Javadoc:

由于GridBagConstraint中的
weightx
的默认值为零,因此整个布局将尽可能小,并以父组件为中心。使用非零的正
权重x
使组件使用整个水平长度,因此布局将占用所有可用空间。对于垂直方向,重量相同

...
c.anchor = align;
c.weightx = 1.0:
...

Javadoc:

为了澄清@DrDipShit所说的:您的标签确实按照您在面板中所需的方式对齐。但是,这是您的面板,位于JFrame内容窗格的中心。您可以尝试执行frame.setLayout(new GridBagLayout())并将面板替换为frame.getContentPane()。^好的,我会尝试,还想问,如果我必须将组件放在左上角,GridBagConstraints.NORTHWEST是否有效?是的,应该有效。这意味着“在单元格的左上角”,当然不是整个面板的左上角。为了澄清@DrDipShit所说的:您的标签确实按照您在面板内所需的方式对齐。但是,这是您的面板,位于JFrame内容窗格的中心。您可以尝试执行frame.setLayout(new GridBagLayout())并将面板替换为frame.getContentPane()。^好的,我会尝试,还想问,如果我必须将组件放在左上角,GridBagConstraints.NORTHWEST是否有效?是的,应该有效。这意味着“在其单元格的左上角”,而不是整个面板。