2012-11-16 470 views
1

我正在設計一個應用程序,其中應該有5個不同的JPanel s包含不同的Swing組件。對於JRadioButton部分,我遇到了一個找不到合適解決方案的問題。 'radioSizePanel'應該放置在主面板的上部中間的某處。有沒有人解決過這個問題?下面是代碼,我使用:Swing:面板尺寸問題

import java.awt.*; 
import javax.swing.*; 

public class PizzaShop extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    private JRadioButton[] radio_size = new JRadioButton[3]; 
    private JRadioButton[] radio_type = new JRadioButton[3]; 
    public PizzaShop() 
    { 
     initializaUI(); 
    } 

    private void initializaUI() 
    { 
     setSize(700, 600); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Panel container to wrap checkboxes and radio buttons 
     JPanel panel = new JPanel(); 

     //sizes radio buttons 
     String Size[] = {"Small: $6.50", "Medium: $8.50", "Large: $10.00"}; 
     JPanel radioSizePanel = new JPanel(new GridLayout(3, 1)); 
     ButtonGroup radioSizeGroup = new ButtonGroup(); 
     for (int i=0; i<3; i++) 
     { 
      radio_size[i] = new JRadioButton(Size[i]); 
      radioSizePanel.add(radio_size[i]); 
      radioSizeGroup.add(radio_size[i]); 
     } 
     radioSizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Size")); 
     radioSizePanel.setPreferredSize(new Dimension(100, 200)); 

     // 
     panel.add(radioSizePanel); 
     setContentPane(panel); 
     setContentPane(radioSizePanel); 
    } 

    public static void main(String[] args) 
    { 
     // TODO Auto-generated method stub 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new PizzaShop().setVisible(true); 
      } 
     }); 
    } 
} 

這是我想作爲一個預期的輸出:

EXPECTED OUTPUT

+0

'frame.setContentPane()',只是增加了一個'JPanel'作爲'內容Pane'。現在你已經寫了兩次了,所以最後添加的'radioSizePanel',將會是隱藏'panel'的那個。可否請您在紙上寫下佈局,並將其視爲快照,並將其與您的問題一同發佈,作爲視圖的預期輸出結果? –

+0

當然,我有我的預期佈局圖像,你能告訴我怎麼把它張貼在這裏,所以你可以看到? –

+0

我被禁止在這裏上傳圖片,因爲我需要贏得聲望!有任何其他方式來演示我的佈局? :( –

回答

2

請不要觀​​看示例代碼,請大家仔細的看一切將事物添加到JFrame的順序。由於在你的例子中,在JFrame之前已經添加了很多東西,所以你需要調用setSize(),因此首先將組件添加到容器中,然後將其稱爲pack()/setSize()方法,以便以一種好的方式實現。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PizzaLayout 
{ 
    /* 
    * Five JPanels we will be using. 
    */ 
    private JPanel headerPanel; 
    private JPanel footerPanel; 
    // This JPanel will contain the middle components. 
    private JPanel centerPanel; 
    private JPanel toppingPanel; 
    private JPanel sizePanel; 
    private JPanel typePanel; 
    private JPanel buttonPanel; 

    private String[] toppings = { 
            "Tomato", 
            "Green Pepper", 
            "Black Olives", 
            "Mushrooms", 
            "Extra Cheese", 
            "Pepproni", 
            "Sausage" 
           }; 

    private JCheckBox[] toppingsCBox; 

    private String[] sizePizza = { 
            "Small $6.50", 
            "Medium $8.50", 
            "Large $10.00" 
           }; 

    private JRadioButton[] sizePizzaRButton; 

    private String[] typePizza = { 
            "Thin Crust", 
            "Medium Crust", 
            "Pan" 
           }; 

    private JRadioButton[] typePizzaRButton; 

    private JButton processButton; 

    private ButtonGroup bGroupType, bGroupSize; 

    private JTextArea orderTArea; 

    private StringBuilder sBuilderOrder; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Pizza Shop"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     /* 
     * This JPanel is the base of all the 
     * other components, and at the end 
     * we will set this as Content Pane 
     * for the JFrame. 
     */ 
     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout(5, 5)); 
     contentPane.setBorder(
      BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     /* 
     * TOP PART of the LAYOUT. 
     */ 
     headerPanel = new JPanel(); 
     JLabel headerLabel = new JLabel(
       "Welcome to Home Style Pizza Shop" 
            , JLabel.CENTER); 
     headerLabel.setForeground(Color.RED); 
     headerPanel.add(headerLabel); 

     /* 
     * CENTER PART of the LAYOUT. 
     */ 
     centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridheight = 2; 
     gbc.weightx = 0.3; 
     gbc.weighty = 1.0; 

     /* 
     * Above Constraints are for this part. 
     */ 
     toppingPanel = new JPanel(); 
     toppingPanel.setBorder(
      BorderFactory.createTitledBorder(
       BorderFactory.createLineBorder(Color.RED), 
              "Each Topping $1.50")); 
     JPanel checkBoxesPanel = new JPanel(); 
     checkBoxesPanel.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     checkBoxesPanel.setLayout(new GridLayout(0, 1, 5, 5)); 

     toppingsCBox = new JCheckBox[toppings.length]; 
     for (int i = 0; i < toppings.length; i++) 
     { 
      toppingsCBox[i] = new JCheckBox(toppings[i]); 
      checkBoxesPanel.add(toppingsCBox[i]); 
     } 

     toppingPanel.add(checkBoxesPanel); 
     centerPanel.add(toppingPanel, gbc); 

     // Till this. 

     gbc.gridx = 1; 
     gbc.gridheight = 1; 
     gbc.weighty = 0.7; 

     /* 
     * Above Constraints are for this part. 
     */ 
     sizePanel = new JPanel(); 
     sizePanel.setBorder(
      BorderFactory.createTitledBorder(
       BorderFactory.createLineBorder(Color.RED), 
              "Pizza Size")); 
     JPanel radioBoxesPanel = new JPanel(); 
     radioBoxesPanel.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     radioBoxesPanel.setLayout(new GridLayout(0, 1, 10, 10)); 

     sizePizzaRButton = new JRadioButton[sizePizza.length]; 
     bGroupSize = new ButtonGroup(); 
     for (int i = 0; i < sizePizza.length; i++) 
     { 
      sizePizzaRButton[i] = new JRadioButton(sizePizza[i]); 
      bGroupSize.add(sizePizzaRButton[i]); 
      radioBoxesPanel.add(sizePizzaRButton[i]); 
     } 

     sizePanel.add(radioBoxesPanel); 
     centerPanel.add(sizePanel, gbc); 
     // Till this. 

     gbc.gridx = 2; 
     gbc.weighty = 0.7; 

     /* 
     * Above Constraints are for this part. 
     */ 
     typePanel = new JPanel(); 
     typePanel.setBorder(
      BorderFactory.createTitledBorder(
       BorderFactory.createLineBorder(Color.RED), 
              "Pizza Type")); 
     JPanel radioBoxesTypePanel = new JPanel(); 
     radioBoxesTypePanel.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     radioBoxesTypePanel.setLayout(new GridLayout(0, 1, 10, 10)); 

     typePizzaRButton = new JRadioButton[typePizza.length]; 
     bGroupType = new ButtonGroup(); 
     for (int i = 0; i < typePizza.length; i++) 
     { 
      typePizzaRButton[i] = new JRadioButton(typePizza[i]); 
      bGroupType.add(typePizzaRButton[i]); 
      radioBoxesTypePanel.add(typePizzaRButton[i]); 
     } 

     typePanel.add(radioBoxesTypePanel); 
     centerPanel.add(typePanel, gbc); 
     // Till this. 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.weighty = 0.3; 
     gbc.gridwidth = 2; 

     processButton = new JButton("Process Selection"); 
     processButton.addActionListener(
      new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) 
      { 
       sBuilderOrder = new StringBuilder(); 
       sBuilderOrder.append("Pizza type : "); 
       for (int i = 0; i < typePizza.length; i++) 
       { 
        if (typePizzaRButton[i].isSelected()) 
         sBuilderOrder.append(typePizzaRButton[i].getText()); 
       } 

       sBuilderOrder.append("\n"); 
       sBuilderOrder.append("Pizza Size : "); 

       for (int i = 0; i < sizePizza.length; i++) 
       { 
        if (sizePizzaRButton[i].isSelected()) 
         sBuilderOrder.append(sizePizzaRButton[i].getText()); 
       } 

       sBuilderOrder.append("\n"); 
       sBuilderOrder.append("Toppings : "); 
       /* 
       * I hope you can do this part yourself now :-) 
       */ 

       orderTArea.setText(sBuilderOrder.toString()); 
      } 
     }); 
     centerPanel.add(processButton, gbc); 

     footerPanel = new JPanel(); 
     footerPanel.setLayout(new BorderLayout(5, 5)); 
     footerPanel.setBorder(
      BorderFactory.createTitledBorder("Your Order : ")); 
     orderTArea = new JTextArea(10, 10); 
     footerPanel.add(orderTArea, BorderLayout.CENTER); 

     contentPane.add(headerPanel, BorderLayout.PAGE_START); 
     contentPane.add(centerPanel, BorderLayout.CENTER); 
     contentPane.add(footerPanel, BorderLayout.PAGE_END); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new PizzaLayout().displayGUI(); 
      } 
     }); 
    } 
} 

這裏是相同的輸出:

PIZZALAYOUT

+0

非常感謝,如果我請求您對'流程按鈕'執行的操作提供幫助,請不要介意嗎? –

+0

Ahha,當然,但我也有我的考試,但會嘗試爲你解決一些問題。 –

+0

再次感謝,我也在努力,但是在我尋求並行幫助之前,我遇到了很多錯誤。正如您從OUTPUT中計算出的那樣,必須根據用戶輸入計算並顯示特定的信息。我會非常感謝 –