当我设置弹簧布局时,JPanel将不会显示

问题描述:

我有一个JFrame和3个JPanel集装箱,一个在顶部,另一个在左侧,最后一个在中心占据框架的其余部分。问题是当我尝试为侧面板设置SpringLayout时,它不会显示在面板上。该框架具有默认的边框布局。当我设置弹簧布局时,JPanel将不会显示

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); 
    } 
} 

这就是它应该为我的工作方式,但事实并非如此。

+1

你在哪里编码? – KyleKW

+0

请分享你的代码和屏幕,这完全不清楚你有什么问题。 –

+0

@KyleKW,我加了我的代码 –

问题是当我尝试为侧面板设置SpringLayout时,它不会显示在面板上。

SpringLayout是一个复杂的布局管理器。

side.setLayout(layoutT); 
    side.add(calendar, SpringLayout.SOUTH); 

您没有正确使用它。

阅读从Swing教程中的部分上How to Use SpringLayout作为工作示例。

然而,根据提供你可能可以用其他的标准布局管理器的一个代码。也许BoxLayout会更容易。