【问题标题】:Creation of GUI in a correct window在正确的窗口中创建 GUI
【发布时间】:2019-03-09 23:47:20
【问题描述】:

我正在尝试创建一个包含 4 个字段的 GUI。

  1. 网址
  2. 用户名
  3. 密码
  4. 声明

在第一次时,这些字段应该是空的。稍后,密码字段旁边的所有字段都应包含上次的信息。

问题:

  1. GUI 窗口不应有标准尺寸

    我想要达到的目标:

    • 当窗口打开时,它应该动态调整到笔记本电脑的屏幕尺寸,例如居中,占屏幕的 20%
  2. 第四个字段(语句)可能很长,GUI 窗口会自动变得过长。

    我想要达到的目标:

    • 对于第四个字段,应将字符串分解为多行。在将字段拆分为三行而不是继续拆分为第四行后,一个选项可能是滚动条。

到目前为止,我已经找到了一些可以提供帮助的对象。

  • JOptionPane
  • JPanel
  • JTextArea 用于第四个长字段

    对于JTextArea,还有

    • JScrollPane
    • .setLineWrap(true) 换行
    • .setWrapStyleWord(true) 在单词后换行

我的代码示例:

JPanel pane = new JPanel();
// adding GridLayout
pane.setLayout(new GridLayout(4,2));
// fields are filled just as an example
// later they will get substituted by variables
JTextField url = new JTextField ("https:testin.com");
JTextField username = new JTextField ("theDude");
JTextArea statement = new JTextArea("This statement can becomme very very very long :)");
statement.setLineWrap(true);
statement.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(statement);
pane.add(scrollPane);
// add infos to pane
pane.add(new JLabel ("Enter url: "));
pane.add(url);
pane.add(new JLabel ("Enter username: "));
pane.add(username);
pane.add(new JLabel ("Enter password: "));
pane.add(new JPasswordField());
pane.add(new JLabel ("Enter statement: "));
pane.add(statement);
//write it to a OK_CANCEL JOptionPane
int option = JOptionPane.showConfirmDialog(null,pane, "Fill all the fields",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);

【问题讨论】:

  • “20% 的屏幕”——GUI 开发人员不应该以这种方式强制大小。 Swing 和桌面以及用户的偏好决定了字体大小,Swing 组件自动适应各自字体的大小。 JOptionPane 将为您将窗口居中。至于安排你的组件,请参阅docs.oracle.com/javase/tutorial/uiswing/layout。通常标签/字段 UI 模式是通过 GridBagLayout 完成的。
  • 除了GridBag,注意Oracle 实际上推荐使用GUI 布局工具,比如NetBeans。查看他们的tutorial 并阅读开头附近的特别说明,其中推荐了 GUI 布局工具并链接到 NetBeans 页面。
  • @markspace 如果我使用 intellij IDE 会怎样?
  • 我不知道那个 IDE,所以你必须在他们的论坛上提问,我猜。没有理由不能在同一个项目中同时使用 NetBeans 和 IntelliJ。 NetBeans 只是为其 GUI 类编写常规代码。
  • @VGR 感谢您的提示。它有点适用于 GridBagLayout,尽管我必须事先设置字段输入的大小。有没有办法从字段中获取输入并将给定的长输入分解为更多行?

标签: java swing user-interface


【解决方案1】:

您可以使用setRows() 设置JTextArea 的高度(可见行数)。试试下面的例子。我从你的代码开始,做了一些改动。

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

public class ThreeLinesTextArea
{
  public static void main(String[] args)
  {
    JPanel pane = new JPanel();
    // Change to GridBagLayout
    pane.setLayout(new GridBagLayout());
    JTextField url = new JTextField("https:testin.com");
    JTextField username = new JTextField("theDude");

    JTextArea statement = new JTextArea("This statement can becomme very very very long :)");
    statement.setLineWrap(true);
    statement.setWrapStyleWord(true);
    // Use setRows() to make text area have multiple lines
    statement.setRows(3);
    JScrollPane scrollPane = new JScrollPane(statement);

    //This line is removed. scrollPane is added at the end.
    //pane.add(scrollPane);

    pane.add(new JLabel("Enter url: "),
        new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(url,
        new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(new JLabel("Enter username: "),
        new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(username,
        new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(new JLabel("Enter password: "),
        new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(new JPasswordField(15),
        new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(new JLabel("Enter statement: "),
        new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
    pane.add(scrollPane,
        new GridBagConstraints(1, 3, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));

    int option = JOptionPane.showConfirmDialog(null, pane, "Fill all the fields",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
  }
}

输出:

【讨论】:

  • 太棒了 :) 不过有两个问题。我认为使用“ipadx”我可以选择白色字段的宽度大小。这不知何故不起作用。在哪一部分中,您选择了所有四个白色字段的大小?第二个问题。如何获取用户在白色字段中写入的值?
  • 经过一些挖掘和测试,我发现“GridBagConstraints.HORIZONTAL”在某种程度上起到了作用。它只是将整个窗口调整为参数的宽度(水平)。
  • @Georgios,是的,当我们将GridBagConstraints.fill 设置为GridBagConstraints.HORIZONTAL 时,组件“填充”了单元格。要从文本组件中获取值,您可以使用 getText() 方法。
猜你喜欢
  • 2012-04-24
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2016-12-02
  • 2021-05-23
相关资源
最近更新 更多