【问题标题】:BorderLayout, asking user input for HGap and VGapBorderLayout,询问用户对 HGap 和 VGAp 的输入
【发布时间】:2016-01-03 01:26:02
【问题描述】:

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

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

【问题讨论】:

  • 您确定您甚至在使用为该项目分配的工具包并适当地标记了您的问题。您问题中的图像是 Swing UI 的屏幕截图,而不是 JavaFX UI。如果您正在编写 Swing 应用程序而不是 JavaFX 应用程序,请更新您问题中的标签以引用适当的技术(否则您最终会得到不适用的答案并使您感到困惑)。
  • 你说得对,它是 Swing,我对此感到困惑,感谢您指出!
  • Java Swing: How to change GUI dynamically 可能会给你一些想法。

标签: java swing border-layout


【解决方案1】:

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

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);
    }
}

【讨论】:

  • 谢谢,这正是我想要的!我已经放弃了,因为我有一个想法但不知道如何使用它,这真的救了我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 2019-02-06
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多