试图了解BorderLayout和内部填充

问题描述 投票:0回答:1

该任务是添加代码以创建GUI,该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);
        });
    }
}
java swing padding layout-manager gridbaglayout
1个回答
0
投票

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

减小插图的大小!例如。

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

可能是:

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

我被困在[计算]和[退出]按钮的位置,应该位于JTextField下方。

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

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


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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.