我正在尝试为比萨饼定购单设置框架,而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);

}
}

最佳答案

根据JavaDoc for PAGE_END


  对于Western,从左到右和从上到下的方向,这等效于SOUTH。


BorderLayout“区域”只能包含一个组件,因此,如果使用同一“区域”多次调用add(),则仅显示最后一个。

获得所需布局的一种方法是创建另一个Panel,为其提供BorderLayout,将orderPanel添加到新Panel的NORTH,将buttonPanel添加到新Panel的SOUTH,然后将新Panel添加到现有pizzaOrderFrame的SOUTH。

关于java - BorderLayout带来的小麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43263716/

10-10 09:21