【发布时间】:2020-06-07 07:58:17
【问题描述】:
任务是添加代码以创建允许用户订购比萨饼和选择浇头的 GUI。我被困在计算和退出按钮的位置上,它应该低于JTextField。
我还试图弄清楚如何减少JCheckBox 面板中的内部填充,因为 GUI 似乎也需要这样做。
import java.awt.*;
import javax.swing.*;
public class PizzaOrderFrame extends JFrame {
JRadioButton smallRadioButton, mediumRadioButton, largeRadioButton;
JCheckBox sausageCheckBox, pepperoniCheckBox, salamiCheckBox,
olivesCheckBox, mushroomsCheckBox, anchoviesCheckBox;
JTextField priceTextField;
JButton calculateButton, exitButton;
PizzaOrderFrame() {
initComponents();
}
private void initComponents() {
setTitle("Pizza Calculator");
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
smallRadioButton = new JRadioButton("Small");
mediumRadioButton = new JRadioButton("Medium");
largeRadioButton = new JRadioButton("Large");
ButtonGroup sizeGroup = new ButtonGroup();
sizeGroup.add(smallRadioButton);
sizeGroup.add(mediumRadioButton);
sizeGroup.add(largeRadioButton);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createTitledBorder("Size"));
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPanel.add(smallRadioButton);
buttonPanel.add(mediumRadioButton);
buttonPanel.add(largeRadioButton);
sausageCheckBox = new JCheckBox("Sausage");
pepperoniCheckBox = new JCheckBox("Pepperoni");
salamiCheckBox = new JCheckBox("Salami");
olivesCheckBox = new JCheckBox("Olives");
mushroomsCheckBox = new JCheckBox("Mushrooms");
anchoviesCheckBox = new JCheckBox("Anchovies");
JPanel chkBoxPanel = new JPanel();
chkBoxPanel.setBorder(BorderFactory.createTitledBorder("Toppings"));
chkBoxPanel.setLayout(new GridBagLayout());
chkBoxPanel.add(sausageCheckBox, getConstraints(0, 0));
chkBoxPanel.add(olivesCheckBox, getConstraints(1, 0));
chkBoxPanel.add(pepperoniCheckBox, getConstraints(0, 1));
chkBoxPanel.add(salamiCheckBox, getConstraints(0, 2));
chkBoxPanel.add(mushroomsCheckBox, getConstraints(1, 1));
chkBoxPanel.add(anchoviesCheckBox, getConstraints(1, 2));
priceTextField = new JTextField(10);
priceTextField.setEnabled(false);
JPanel pricePanel = new JPanel();
pricePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
pricePanel.add(new JLabel("Price"));
pricePanel.add(priceTextField);
calculateButton = new JButton("Calculate");
exitButton = new JButton("Exit");
ButtonGroup bottomButtons = new ButtonGroup();
bottomButtons.add(calculateButton);
bottomButtons.add(exitButton);
JPanel buttonPanel2 = new JPanel();
buttonPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPanel2.add(calculateButton);
buttonPanel2.add(exitButton);
add(buttonPanel, BorderLayout.NORTH);
add(chkBoxPanel, BorderLayout.WEST);
add(pricePanel, BorderLayout.SOUTH);
add(buttonPanel2, BorderLayout.EAST);
setSize(270, 280);
}
// helper method for getting a GridBagConstraints object
private GridBagConstraints getConstraints(int x, int y) {
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(5, 5, 0, 5);
c.gridx = x;
c.gridy = y;
c.ipadx = 0;
c.ipady = 0;
return c;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
PizzaOrderFrame frame = new PizzaOrderFrame();
frame.setVisible(true);
});
}
}
【问题讨论】:
-
“需要帮助!!!” 需要问题! “我没有为动作事件添加代码..” 很好,但这是一个布局问题,所以一开始不应该添加任何动作。发布minimal reproducible example。
-
我 “发布一个最小的可重现示例。” 我已经编辑了这个问题以包含没有任何无关的代码,但可以按原样编译和运行。请将其复制到 IDE 中的新项目中并确认它显示问题(MRE 的另一个重要方面)。
标签: java swing padding layout-manager gridbaglayout