BorderLayout,要求用户输入 HGap 和 VGap

BorderLayout, asking user input for HGap and VGap

我想知道是否有人可以指导我从哪里开始我的任务。我应该创建一个程序,允许用户输入一个值,该值将在 BorderLayout 中的按钮之间设置 HGap 或 VGap。我试过在纸上画出来但我不知道我应该使用面板还是标签,或者如何实现设置文本区域间隙的方法,甚至不知道如何将框架扩展到按钮之外(我有按钮代码,但它们填满了整个 space)。它应该是这样的:

VGap 同样如此。非常感谢任何帮助,但请记住,我只是一个初学者。我只需要建议,我会尝试用我的编码更新你,但现在我什么都没有。

下面的代码可能会给你一个思路...

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class BorderDemo extends JFrame {
    private JPanel buttonPanel = new JPanel();
    private JPanel propertiesPanel = new JPanel();
    private JButton north = new JButton("North");
    private JButton south = new JButton("South");
    private JButton west = new JButton("West");
    private JButton east = new JButton("East");
    private JButton center = new JButton("Center");
    private BorderLayout border = new BorderLayout();
    private JLabel label = new JLabel("BorderLayout Properties:");
    private JLabel hGapLabel = new JLabel("HGap:");
    private JLabel vGapLabel = new JLabel("VGap:");
    private JTextField hGapField = new JTextField();
    private JTextField vGapField = new JTextField();
    private GridLayout grid = new GridLayout(2, 2);
    private Integer hGapInt;
    private Integer vGapInt;

    public BorderDemo() {
        buttonPanel.setLayout(border);
        buttonPanel.add(north, BorderLayout.NORTH);
        buttonPanel.add(center, BorderLayout.CENTER);
        buttonPanel.add(south, BorderLayout.SOUTH);
        buttonPanel.add(west, BorderLayout.WEST);
        buttonPanel.add(east, BorderLayout.EAST);
        propertiesPanel.setLayout(grid);
        propertiesPanel.add(hGapLabel);
        propertiesPanel.add(hGapField);
        propertiesPanel.add(vGapLabel);
        propertiesPanel.add(vGapField);

        add(buttonPanel, BorderLayout.CENTER);
        add(propertiesPanel, BorderLayout.SOUTH);

        hGapField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                hGapInt = Integer.parseInt(hGapField.getText());
                border.setHgap(hGapInt);
                setSize((int) (getWidth() + hGapInt), getHeight());
                validate();
            }
        });

        vGapField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                vGapInt = Integer.parseInt(vGapField.getText());
                border.setVgap(vGapInt);
                setSize(getWidth(), (int) (getHeight() + vGapInt));
                validate();
            }
        });
    }

    public static void main(String[] args) {
        BorderDemo borderDemo = new BorderDemo();
        borderDemo.setVisible(true);
        borderDemo.setSize(400, 400);
        borderDemo.setLocationRelativeTo(null);
        borderDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}