我有一个JFrame
带有 3 个容器的JPanel
容器,一个在顶部,另一个在左侧,最后一个在中间,占据了框架的其余部分。问题是当我尝试SpringLayout
为侧面板设置 a 时,它不会显示在面板上。框架具有默认的边框布局。
public final class board extends JFrame {
public final JPanel top = new JPanel();
public final JPanel side = new JPanel();
public final JPanel center = new JPanel();
public board(){
initComponents();
initWindow();
}
public void initComponents(){
Font font = new Font("HelvLight", Font.PLAIN, 20);
Font fontT = new Font("Century Gothic", Font.BOLD, 30);
SpringLayout layoutT = new SpringLayout();
JSeparator sep = new JSeparator();
JLabel title = new JLabel("TITLE");
JLabel calendar = new JLabel("Calendar");
JButton settings = new JButton("C"); //Add Icon
title.setFont(fontT);
calendar.setFont(font);
title.setForeground(Color.WHITE);
calendar.setForeground(Color.WHITE);
sep.setBackground(Color.white);
sep.setForeground(Color.white);
//layoutT.putConstraint(SpringLayout.WEST, calendar, 50, SpringLayout.EAST, center);
top.setLayout(new BorderLayout());
top.add(settings, BorderLayout.EAST);
top.add(title, BorderLayout.CENTER); // Not centered
top.setBackground(Color.decode("#4C004C"));
center.setBackground(Color.green);
side.setBackground(Color.decode("#0f001e"));
side.setLayout(layoutT);
side.add(calendar, SpringLayout.SOUTH);
side.add(sep);
}
public void initWindow() {
Toolkit tk = Toolkit.getDefaultToolkit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize((int) tk.getScreenSize().getWidth() ,
(int) tk.getScreenSize().getHeight()
);
//setResizable(false);
add(top, BorderLayout.NORTH);
add(side, BorderLayout.WEST);
add(center, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
这就是它应该为我工作的方式,但事实并非如此。