我正在尝试制作一个GUI披萨菜单,但我在按钮/标签的展示位置时遇到问题
public class PizzaGUI extends JFrame {
private JRadioButton rdoSmall;
private JRadioButton rdoMedium;
private JRadioButton rdoLarge;
private JRadioButton rdoExtraLarge;
private JLabel lblSize;
private ButtonGroup grpSize;
JPanel panelSize;
private JCheckBox chkPepperoni;
private JCheckBox chkMushrooms;
private JCheckBox chkOlives;
private JCheckBox chkPineapple;
private JLabel lblToppings;
JPanel panelToppings;
private JRadioButton rdoSoda;
private JRadioButton rdoTea;
private JRadioButton rdoBottledWater;
private JRadioButton rdoTapWater;
private JLabel lblDrinks;
private ButtonGroup grpDrinks;
JPanel panelDrinks;
JPanel container;
JButton calculateTotal;
JLabel order;
PizzaGUI()
{
super("Pizza Menu");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createPanel();
add(container);
setVisible(true);
}
public void createPanel()
{
panelSize = new JPanel(new GridLayout(5, 1));
panelToppings = new JPanel(new GridLayout(5, 1));
panelDrinks = new JPanel(new GridLayout(5, 1));
container = new JPanel();
//Calculate Total
calculateTotal = new JButton("Calculate");
calculateTotal.setPreferredSize(new Dimension(95,45));
order = new JLabel("Your Order:");
//Pizza Sizes
lblSize = new JLabel("Choose a size:");
rdoSmall = new JRadioButton("Small ($7)");
rdoMedium = new JRadioButton("Medium ($9)");
rdoLarge = new JRadioButton("Large ($11)");
rdoExtraLarge = new JRadioButton("Extra Large ($14)");
//Toppings
lblToppings = new JLabel("Choose toppings ($1 Each):");
chkPepperoni = new JCheckBox("Pepperoni");
chkMushrooms = new JCheckBox("Mushrooms");
chkOlives = new JCheckBox("Olives");
chkPineapple = new JCheckBox("Pineapple");
//Drinks
lblDrinks = new JLabel("Choose a drink:");
rdoSoda = new JRadioButton("Soda ($2.00)");
rdoTea = new JRadioButton("Tea ($1.50)");
rdoBottledWater = new JRadioButton("Bottled Water ($1.25)");
rdoTapWater = new JRadioButton("Tap Water (No charge)");
//Add pizza sizes to button group
grpSize = new ButtonGroup();
grpSize.add(rdoSmall);
grpSize.add(rdoMedium);
grpSize.add(rdoLarge);
grpSize.add(rdoExtraLarge);
//Add drinks to button group
grpDrinks = new ButtonGroup();
grpDrinks.add(rdoSoda);
grpDrinks.add(rdoTea);
grpDrinks.add(rdoBottledWater);
grpDrinks.add(rdoTapWater);
//Add to panel
panelSize.add(lblSize);
panelSize.add(rdoSmall);
panelSize.add(rdoMedium);
panelSize.add(rdoLarge);
panelSize.add(rdoExtraLarge);
panelToppings.add(lblToppings);
panelToppings.add(chkPepperoni);
panelToppings.add(chkMushrooms);
panelToppings.add(chkOlives);
panelToppings.add(chkPineapple);
panelDrinks.add(lblDrinks);
panelDrinks.add(rdoSoda);
panelDrinks.add(rdoTea);
panelDrinks.add(rdoBottledWater);
panelDrinks.add(rdoTapWater);
container.add(panelSize);
container.add(Box.createHorizontalStrut(40));
container.add(panelToppings);
container.add(Box.createHorizontalStrut(40));
container.add(panelDrinks);
container.add(calculateTotal);
container.add(Box.createVerticalStrut(100));
container.add(order);
}
}
这是输出 Menu
我为每种类型的菜单都有三个单独的网格布局面板,然后我将其添加到一个主面板中,该主面板将添加到框架中。我想把“你的订单:”放在中间左边,就像图中显示的红色一样。我知道我可以将布局设置为null,这将允许我输入坐标,它将解决我的问题,但是我读到这样做是不好的做法,我正在努力有效地学习如何正确使用布局。我甚至不知道我是否走在正确的轨道上,任何例子都会有所帮助。感谢
答案 0 :(得分:0)
可能这可以帮到你。 您可以添加单独的面板,然后为该面板创建一个空边框。
private void datepickerName_Loaded(object sender, RoutedEventArgs e) {
DatePicker dp = sender as DatePicker;
dp.BlackoutDates.AddDatesInPast();
}
答案 1 :(得分:0)
您修改后的代码可以执行您想要的操作:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class PizzaGUI extends JFrame {
private JRadioButton rdoSmall;
private JRadioButton rdoMedium;
private JRadioButton rdoLarge;
private JRadioButton rdoExtraLarge;
private JLabel lblSize;
private ButtonGroup grpSize;
JPanel panelSize;
private JCheckBox chkPepperoni;
private JCheckBox chkMushrooms;
private JCheckBox chkOlives;
private JCheckBox chkPineapple;
private JLabel lblToppings;
JPanel panelToppings;
private JRadioButton rdoSoda;
private JRadioButton rdoTea;
private JRadioButton rdoBottledWater;
private JRadioButton rdoTapWater;
private JLabel lblDrinks;
private ButtonGroup grpDrinks;
JPanel panelDrinks;
JPanel container;
JButton calculateTotal;
JLabel order;
PizzaGUI()
{
super("Pizza Menu");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createPanel();
add(container);
setVisible(true);
}
public void createPanel()
{
panelSize = new JPanel(new GridLayout(5, 1));
panelToppings = new JPanel(new GridLayout(5, 1));
panelDrinks = new JPanel(new GridLayout(5, 1));
container = new JPanel(new GridLayout(2,1));
JPanel container2 = new JPanel();
JPanel placedOrderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
//Calculate Total
calculateTotal = new JButton("Calculate");
calculateTotal.setPreferredSize(new Dimension(95,45));
order = new JLabel("Your Order:");
placedOrderPanel.add(new JLabel("Your Order placed"));
//Pizza Sizes
lblSize = new JLabel("Choose a size:");
rdoSmall = new JRadioButton("Small ($7)");
rdoMedium = new JRadioButton("Medium ($9)");
rdoLarge = new JRadioButton("Large ($11)");
rdoExtraLarge = new JRadioButton("Extra Large ($14)");
//Toppings
lblToppings = new JLabel("Choose toppings ($1 Each):");
chkPepperoni = new JCheckBox("Pepperoni");
chkMushrooms = new JCheckBox("Mushrooms");
chkOlives = new JCheckBox("Olives");
chkPineapple = new JCheckBox("Pineapple");
//Drinks
lblDrinks = new JLabel("Choose a drink:");
rdoSoda = new JRadioButton("Soda ($2.00)");
rdoTea = new JRadioButton("Tea ($1.50)");
rdoBottledWater = new JRadioButton("Bottled Water ($1.25)");
rdoTapWater = new JRadioButton("Tap Water (No charge)");
//Add pizza sizes to button group
grpSize = new ButtonGroup();
grpSize.add(rdoSmall);
grpSize.add(rdoMedium);
grpSize.add(rdoLarge);
grpSize.add(rdoExtraLarge);
//Add drinks to button group
grpDrinks = new ButtonGroup();
grpDrinks.add(rdoSoda);
grpDrinks.add(rdoTea);
grpDrinks.add(rdoBottledWater);
grpDrinks.add(rdoTapWater);
//Add to panel
panelSize.add(lblSize);
panelSize.add(rdoSmall);
panelSize.add(rdoMedium);
panelSize.add(rdoLarge);
panelSize.add(rdoExtraLarge);
panelToppings.add(lblToppings);
panelToppings.add(chkPepperoni);
panelToppings.add(chkMushrooms);
panelToppings.add(chkOlives);
panelToppings.add(chkPineapple);
panelDrinks.add(lblDrinks);
panelDrinks.add(rdoSoda);
panelDrinks.add(rdoTea);
panelDrinks.add(rdoBottledWater);
panelDrinks.add(rdoTapWater);
container2.add(panelSize);
container2.add(Box.createHorizontalStrut(40));
container2.add(panelToppings);
container2.add(Box.createHorizontalStrut(40));
container2.add(panelDrinks);
container2.add(calculateTotal);
container2.add(Box.createVerticalStrut(100));
container2.add(order);
container.add(container2);
container.add(placedOrderPanel);
}
public static void main(String args[]) {
new PizzaGUI();
}
}