2016-12-15 98 views
-1

我有一個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,我加了我的代碼 –

回答

1

問題是當我嘗試爲側面板設置SpringLayout時,它不會顯示在面板上。

SpringLayout是一個複雜的佈局管理器。

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

您沒有正確使用它。

閱讀從Swing教程中的部分上How to Use SpringLayout作爲工作示例。

然而,根據提供你可能可以用其他的標準佈局管理器的一個代碼。也許BoxLayout會更容易。