这是我使用的代码GridbagLayout
public IntroPanel(){
JPanel intro = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("Test test test");
label1.setFont(new Font("Helvetica", Font.PLAIN, 40));
label1.setHorizontalAlignment(SwingConstants.LEFT);
JLabel label2 = new JLabel("test2 test2 test2");
setPreferredSize(new Dimension(640,480));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = 10;
gbc.weightx = 0;
add(label1, gbc);
gbc.gridx = 20;
gbc.gridy = 10;
add(label2, gbc);
}
我更改了所有gridx
, gridy
,GridBagConstraints
以对齐它们,但没有任何效果。NORTHEAST
对于网格约束也不起作用。任何人都可以帮忙吗?学习如何创建自己的 GUI..
主要代码在这里
public static void main(String[] args) {
JFrame frame = new JFrame("Title111111111");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab("Intro", new IntroPanel());
tp.addTab("Catalogue", new CataloguePanel());
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
frame.add(panel);
frame.add(tp);
frame.pack();
frame.setVisible(true);
}