JList 不是您正在执行的操作的正确控件。 JList 用于可变数量的数据项,并且总是应该放置在 JScrollPane 中。
您可能需要一组 JButton,它们放置在使用 GridLayout 配置为只有一列的 JPanel 中。 GridLayout 将强制其所有子级足够大以填充其相应的容器。
MenuPanel() {
setBackground(new Color(0,168,243));
setLayout(new BorderLayout());
JPanel menuList = new JPanel(new GridLayout(0, 1));
menuList.setBackground(new Color(0,200,250));
menuList.setForeground(new Color(80,80,80));
Font menuFont = new Font("Arial",Font.BOLD,20);
JButton menuButton;
menuButton = new JButton("Page 1");
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
menuButton = new JButton("Page 2");
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
menuButton = new JButton("Page 3");
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
menuButton = new JButton("Page 4");
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
menuButton = new JButton("Page 5");
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
JLabel menuLabel = new JLabel();
menuLabel.setText("Menu");
menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
menuLabel.setFont(new Font("Arial",Font.BOLD,50));
menuLabel.setForeground(new Color(50,50,50));
menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));
add(menuList, BorderLayout.CENTER);
add(menuLabel, BorderLayout.NORTH);
}
当然,使用循环可以让上面的代码短很多:
MenuPanel() {
setBackground(new Color(0,168,243));
setLayout(new BorderLayout());
JPanel menuList = new JPanel(new GridLayout(0, 1));
menuList.setBackground(new Color(0,200,250));
menuList.setForeground(new Color(80,80,80));
Font menuFont = new Font("Arial",Font.BOLD,20);
String[] pageLabels = {
"Page 1", "Page 2", "Page 3", "Page 4", "Page 5",
};
JButton menuButton;
for (String pageLabel : pageLabels) {
menuButton = new JButton(pageLabel);
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
}
JLabel menuLabel = new JLabel();
menuLabel.setText("Menu");
menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
menuLabel.setFont(new Font("Arial",Font.BOLD,50));
menuLabel.setForeground(new Color(50,50,50));
menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));
add(menuList, BorderLayout.CENTER);
add(menuLabel, BorderLayout.NORTH);
}
如果您的按钮标签确实是第 1 页到第 5 页,则可以只使用数字循环:
MenuPanel() {
setBackground(new Color(0,168,243));
setLayout(new BorderLayout());
JPanel menuList = new JPanel(new GridLayout(0, 1));
menuList.setBackground(new Color(0,200,250));
menuList.setForeground(new Color(80,80,80));
Font menuFont = new Font("Arial",Font.BOLD,20);
JButton menuButton;
for (int i = 1; i <= 5; i++) {
menuButton = new JButton("Page " + i);
menuButton.setHorizontalAlignment(SwingConstants.LEADING);
menuButton.setFont(menuFont);
menuButton.setBorderPainted(false);
menuButton.setContentAreaFilled(false);
menuList.add(menuButton);
}
JLabel menuLabel = new JLabel();
menuLabel.setText("Menu");
menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
menuLabel.setFont(new Font("Arial",Font.BOLD,50));
menuLabel.setForeground(new Color(50,50,50));
menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));
add(menuList, BorderLayout.CENTER);
add(menuLabel, BorderLayout.NORTH);
}
请注意,在 Java 中,变量名称始终以小写字母开头(除非它们是静态最终字段,在这种情况下,名称应为 ALL_CAPS)。