2012-11-16 240 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中计算出的那样,必须根据用户输入计算并显示特定的信息。我会非常感谢 –