java - 在正确的窗口中创建 GUI

标签 java swing user-interface

我正在尝试创建一个包含 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);

enter image description here

最佳答案

您可以使用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);
  }
}

输出:

enter image description here

关于java - 在正确的窗口中创建 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083122/

相关文章:

java - 在 Android 上搜索 SQLite 数据库

java - 尝试同步Gradle依赖项 “Failed to resolve: redstone.xmlrpc:xmlrpc:1.1.1”时出错

java - 获取旋转器的当前范围?

.net - Krypton Controls 在 Windows 7 机器上表现得很奇怪

Java异常处理机制

java - 如何将 spring bean 集成从 XML 转换为基于 Java 注解的 Config

java - 工具提示文本删除java中的面板绘图

java - 在 JTable 中隐藏一列

c++ - Qt 自动 UI 测试因消息框而停止。如何模拟在消息框上输入?

javascript - 奇怪的分配 IDE 警告