gpt4 book ai didi

java - BorderLayout 的小问题

转载 作者:行者123 更新时间:2023-12-02 13:29:04 24 4
gpt4 key购买 nike

我正在尝试设置披萨订单表单的框架,但在使用 orderPanel 和 ButtonPanel 时遇到问题。我可以让其中之一出现,但不能同时出现?在我发布的当前代码中,显示了按钮,但未显示文本框/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

For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH.

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

获得所需布局的一种方法是创建另一个Panel,为其指定BorderLayout,将orderPanel添加到新Panel的北边,将buttonPanel添加到新Panel的南边,然后将新Panel添加到现有的pizzaOrderFrame的南边。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com