2014-10-03 47 views
0

出於某種原因,我無法讓BorderLayout設置它應有的方式。只是想知道我要去哪裏錯了。BorderLayout不能正常工作JFrame

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class ColorFactory extends JFrame 
{ 

    final int width = 500; 
    final int height = 300; 

    private JPanel buttonPanel; 
    private JPanel radioButtonPanel; 
    private JLabel msgChangeColor; 

    public ColorFactory() 
    { 
     setTitle("Color Factory"); 
     setSize(width, height); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new BorderLayout()); 

     createTopPanel(); 
     add(buttonPanel, BorderLayout.NORTH); 

     createBottomPanel(); 
     add(radioButtonPanel, BorderLayout.SOUTH); 

     msgChangeColor = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color."); 
     add(msgChangeColor, BorderLayout.CENTER); 

     pack(); 
    } 

    private void createTopPanel() 
    { 
     buttonPanel = new JPanel(); 
     setLayout(new FlowLayout()); 

     JButton redButton = new JButton("Red"); 
     redButton.setBackground(Color.RED); 
     redButton.addActionListener(new ButtonListener()); 
     redButton.setActionCommand("R"); 

     JButton orangeButton = new JButton("Orange"); 
     orangeButton.setBackground(Color.ORANGE); 
     orangeButton.addActionListener(new ButtonListener()); 
     orangeButton.setActionCommand("O"); 

     JButton yellowButton = new JButton("Yellow"); 
     yellowButton.setBackground(Color.YELLOW); 
     yellowButton.addActionListener(new ButtonListener()); 
     yellowButton.setActionCommand("Y"); 

     buttonPanel.add(redButton); 
     buttonPanel.add(orangeButton); 
     buttonPanel.add(yellowButton); 

    } 

    private void createBottomPanel() 
    { 
     radioButtonPanel = new JPanel(); 
     setLayout(new FlowLayout()); 

     JRadioButton greenRadioButton = new JRadioButton("Green"); 
     greenRadioButton.setBackground(Color.GREEN); 
     greenRadioButton.addActionListener(new RadioButtonListener()); 
     greenRadioButton.setActionCommand("G"); 

     JButton blueRadioButton = new JButton("Blue"); 
     blueRadioButton.setBackground(Color.BLUE); 
     blueRadioButton.addActionListener(new RadioButtonListener()); 
     blueRadioButton.setActionCommand("B"); 

     JButton cyanRadioButton = new JButton("Cyan"); 
     cyanRadioButton.setBackground(Color.CYAN); 
     cyanRadioButton.addActionListener(new RadioButtonListener()); 
     cyanRadioButton.setActionCommand("C"); 

     radioButtonPanel.add(greenRadioButton); 
     radioButtonPanel.add(blueRadioButton); 
     radioButtonPanel.add(cyanRadioButton); 
    } 

    private class ButtonListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String actionColor = e.getActionCommand(); 
      if(actionColor.equals("R")) 
      { 
       buttonPanel.setBackground(Color.RED); 
       radioButtonPanel.setBackground(Color.RED); 
      } 

      if(actionColor.equals("O")) 
      { 
       buttonPanel.setBackground(Color.ORANGE); 
       radioButtonPanel.setBackground(Color.ORANGE); 
      } 

      if(actionColor.equals("Y")) 
      { 
       buttonPanel.setBackground(Color.YELLOW); 
       radioButtonPanel.setBackground(Color.YELLOW); 
      } 
     } 
    } 
     private class RadioButtonListener implements ActionListener 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       String actionTextColor = e.getActionCommand(); 
       if(actionTextColor.equals("G")) 
       { 
        msgChangeColor.setForeground(Color.GREEN); 
       } 

       if(actionTextColor.equals("B")) 
       { 
        msgChangeColor.setForeground(Color.BLUE); 
       } 

       if(actionTextColor.equals("C")) 
       { 
        msgChangeColor.setForeground(Color.CYAN); 
       } 
      } 
    } 

     public static void main(String[] args) 
     { 
      ColorFactory run = new ColorFactory(); 
      run.setVisible(true); 
     } 

} 

回答

1

問題是要更改的佈局管理器,當你創建你的頂部和底部面板框架...

private void createTopPanel() { 
    buttonPanel = new JPanel(); 
    setLayout(new FlowLayout()); // <--- This is call setLayout on the frame 

這就是爲什麼它是危險的......

  • 直接從JFrame類似的東西...
  • 動態構建組件

這一切都容易失去語境,並開始影響你實際上並沒有部件要...

+0

所以我將它改爲buttonPanel.setLayout(new lowLayout());它的工作原理。不得不刪除pack();將面板分開。感謝您的幫助 – user3752231 2014-10-03 07:31:58

+1

*「..和它的工作原理。必須刪除包()..」*'它工作'和'刪除包'在*相同*描述。多麼奇怪。如果某些東西只有*不帶*包才能起作用,那麼這是一個GUI非常壞的標誌。 – 2014-10-03 07:34:16

+0

@ user3752231與'pack'不同的問題。嘗試使用'EmptyBorder'在組件周圍生成更多空間 – MadProgrammer 2014-10-03 07:36:23

1

另一個問題(除了一張貼MadProgrammer)是您添加部件到JFrame本身。

您應該通過調用JFrame.getContentPane()將內容添加到框架的內容窗格中。

實施例:

JFrame f = new JFrame("Test"); 
Container c = f.getContentPane(); 
c.add(new JButton("In Center"), BorderLayout.CENTER); 
c.add(new JButton("At the Bottom"), BorderLayout.SOUTH); 
c.add(new JButton("At the Top"), BorderLayout.NORTH); 
c.add(new JButton("On the Left"), BorderLayout.WEST); 
c.add(new JButton("On the Right"), BorderLayout.EAST); 

可以設置/通過調用JFrame.setContentPane()改變內容面板。默認的內容面板已經有BorderLayout,所以你甚至不需要改變它,也不需要設置新的面板。

+0

從Java 5(我認爲)'JFrame#add'自動重定向到內容窗格,因此不需要使用'getContentPane',其他比美學... – MadProgrammer 2014-10-03 07:37:38

+0

@MadProgrammer你說得對,我沒有意識到它。是的,從5.0開始,相關方法被覆蓋以轉發到內容窗格。 – icza 2014-10-03 07:47:34