BorderLayout的小麻烦

时间:2017-04-06 19:02:20

标签: java

我正在尝试为披萨订单表单设置框架,而我在使用orderPanel和buttonsPanel时遇到了问题。我可以让一个或另一个出现,但不是两个?在我发布的当前代码中,按钮显示,但textbox / orderPanel不显示。而且我已经得到它,所以orderPanel显示,但它隐藏了按钮,这也是不好的。我想要最底部的按钮和它上面的orderPanel;我怎么能这样做?

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 个答案:

答案 0 :(得分:1)

根据JavaDoc for PAGE_END

  

对于西方,从左到右和从上到下的方向,这相当于南方。

BorderLayout&#34;地区&#34;只能包含一个组件,因此如果您使用相同的&#34;区域&#34;多次调用add(),则只会显示最后一个组件。

获得所需布局的一种方法是创建另一个Panel,给它一个BorderLayout,将orderPanel添加到新Panel的NORTH,buttonsPanel添加到新Panel的南方,然后添加新的面板到现有的pizzaOrderFrame南方。