【发布时间】:2011-04-17 17:20:55
【问题描述】:
我是 Swing 新手。这是我写的代码
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 等...),我的标签都在面板的中心对齐。
如何更改标签的对齐方式?
提前致谢。
【问题讨论】: