2017-04-06 35 views
0

我想設置比薩訂單窗體的框架,並且我遇到了orderPanel和buttonsPanel的問題。我可以讓一個或另一個出現,但不是兩個?在我發佈的這個當前代碼中,按鈕顯示,但textbox/orderPanel不是。我已經得到它,所以orderPanel能顯示,但它然後隱藏按鈕,這也不好。我想要最下方的按鈕和右上方的orderPanel按鈕;我怎樣才能做到這一點?BorderLayout的小麻煩

class PizzaOrderFrame extends JFrame 
{ 
final private JPanel crustPanel, sizePanel, toppingsPanel, orderPanel, buttonsPanel; 
final private TitledBorder crustBorder, sizeBorder, toppingsBorder, orderBorder; 
final private JButton quitButton, clearButton, orderButton; 
final private JTextArea orderTextArea; 
final private JRadioButton thin, regular, deepDish; 
final private JCheckBox pepperoni, sausage, bacon, extraCheese, olives, mushrooms; 
double smallSizeCost = 8.0; 
double mediumSizeCost = 12.0; 
double largeSizeCost = 16.0; 
double superSizeCost = 20.0; 
double toppingsCost = 1.0; 
double toppingsCount = 0; 
double tax = 0.07; 
double orderSubTotal = 0; 
double orderTotal = 0; 

public PizzaOrderFrame() 
{ 
    setTitle("Pizza Order Form"); 
    Toolkit kit = Toolkit.getDefaultToolkit(); 
    Dimension screenSize = kit.getScreenSize(); 
    int screenHeight = screenSize.height; 
    int screenWidth = screenSize.width; 
    double setScreen = screenWidth * .80; 
    double setScreen3 = screenHeight * .80; 
    int setScreen2 = (int) setScreen; 
    int setScreen4 = (int) setScreen3; 
    setSize(setScreen2, setScreen4); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    crustPanel = new JPanel(); 
    crustBorder = new TitledBorder("Select your crust"); 
    crustBorder.setTitleJustification(TitledBorder.CENTER); 
    crustBorder.setTitlePosition(TitledBorder.TOP); 
    crustPanel.setBorder(crustBorder); 
    thin = new JRadioButton("Thin"); 
    regular = new JRadioButton("Regular"); 
    deepDish = new JRadioButton("Deep-Dish"); 
    ButtonGroup group = new ButtonGroup(); 
    group.add(thin); 
    group.add(regular); 
    group.add(deepDish); 
    crustPanel.add(thin); 
    crustPanel.add(regular); 
    crustPanel.add(deepDish); 
    add(crustPanel, BorderLayout.WEST); 

    sizePanel = new JPanel(); 
    sizeBorder = new TitledBorder("Select your size"); 
    sizeBorder.setTitleJustification(TitledBorder.CENTER); 
    sizeBorder.setTitlePosition(TitledBorder.TOP); 
    sizePanel.setBorder(sizeBorder); 
    String[] sizeOptions = new String [] {"Small", "Medium", "Large", "Super" }; 
    JComboBox<String> size = new JComboBox<>(sizeOptions); 
    String selectedSize = (String) size.getSelectedItem(); 
    sizePanel.add(size); 
    add(sizePanel, BorderLayout.CENTER); 

    toppingsPanel = new JPanel(); 
    toppingsBorder = new TitledBorder("Select your toppings"); 
    toppingsBorder.setTitleJustification(TitledBorder.CENTER); 
    toppingsBorder.setTitlePosition(TitledBorder.TOP); 
    toppingsPanel.setBorder(toppingsBorder); 
    pepperoni = new JCheckBox("Pepperoni"); 
    sausage = new JCheckBox("Sausage"); 
    extraCheese = new JCheckBox("Extra Cheese"); 
    mushrooms = new JCheckBox("Mushrooms"); 
    olives = new JCheckBox("Olives"); 
    bacon = new JCheckBox("Bacon"); 
    toppingsPanel.add(pepperoni); 
    toppingsPanel.add(sausage); 
    toppingsPanel.add(extraCheese); 
    toppingsPanel.add(mushrooms); 
    toppingsPanel.add(olives); 
    toppingsPanel.add(bacon); 
    add(toppingsPanel, BorderLayout.EAST); 

    orderPanel = new JPanel(); 
    orderBorder = new TitledBorder("Your Order"); 
    orderBorder.setTitleJustification(TitledBorder.CENTER); 
    orderBorder.setTitlePosition(TitledBorder.TOP); 
    orderPanel.setBorder(orderBorder); 
    orderTextArea = new JTextArea(); 
    JScrollPane orderSP = new JScrollPane(orderTextArea); 
    orderSP.setPreferredSize(new Dimension(300, 100)); 
    orderPanel.add(orderSP); 
    add(orderPanel, BorderLayout.SOUTH); 

    buttonsPanel = new JPanel(); 
    quitButton = new JButton("Quit"); 
    clearButton = new JButton("Clear"); 
    orderButton = new JButton("Order"); 
    buttonsPanel.add(quitButton); 
    buttonsPanel.add(clearButton); 
    buttonsPanel.add(orderButton); 
    add(buttonsPanel, BorderLayout.PAGE_END); 

} 
} 

回答

1

按照JavaDoc for PAGE_END

對於西方,由左到右,上到下的方向,這等同於SOUTH。

BorderLayout「區域」只能包含一個組件,因此如果您使用相同的「區域」多次調用add(),則只顯示最後一個組件。

獲得所需佈局的一種方法是創建另一個面板,給它一個BorderLayout,將OrderPanel添加到新面板的NORTH,將按鈕面板添加到新面板的SOUTH,然後將新面板添加到現有pizzaOrderFrame的SOUTH。

+0

完美運作;我將銘記未來。非常感謝你 :) – user7427726