【问题标题】:Trying to understand BorderLayout and internal padding试图理解 BorderLayout 和内部填充
【发布时间】: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


【解决方案1】:

..如何减少JCheckBox 面板中的内部填充..

减小插图的大小!例如。

c.insets = new Insets(5, 5, 0, 5);

可能是:

c.insets = new Insets(0, 0, 0, 0); // try other values

我卡在计算和退出按钮的位置上,它应该在 JTextField 下方。

请注意,布局可以是combined完整的 GUI 视图很少使用单一布局。

在这种情况下,我可能会创建一个带有单列 GridLayoutJPanel,向其中添加价格/文本字段面板,然后为按钮添加另一个面板。然后将该网格布局面板添加到边框布局的SOUTH / PAGE_END


同时执行这些操作(并调用pack() 而不是setSize(..),这是一个猜测)的结果如下:

【讨论】:

  • BTW - 将带有浇头的面板放在CENTER 而不是EAST 中,以使其拉伸(宽度方向)到可用空间。我还建议使用布局/约束,将标签/文本字段和按钮定位在可用宽度的中心。
猜你喜欢
  • 2016-05-02
  • 2014-03-23
  • 2021-05-23
  • 2011-05-12
  • 1970-01-01
  • 2019-02-11
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多