为了账号安全,请及时绑定邮箱和手机立即绑定

在正确的窗口中创建 GUI

在正确的窗口中创建 GUI

aluckdog 2022-07-27 20:32:06
我正在尝试创建一个包含 4 个字段的 GUI。网址用户名密码陈述在第一次,这些字段应该是空的。稍后,密码字段旁边的所有字段都应包含上次的信息。问题:GUI 窗口不应具有标准大小我想达到的目标:当窗口打开时,它应该动态调整到笔记本电脑的屏幕大小,例如中心,屏幕的 20%第四个字段(语句)可能很长,GUI 窗口会自动变得过长。我想达到的目标:对于第四个字段,应将字符串分解为多行。在将字段分解为三行而不是继续将其分解为第四行之后,一个选项可能是滚动条。到目前为止,我已经找到了一些可以提供帮助的对象。选项窗格面板第四个长字段的JTextArea对于JTextArea,还有滚动窗格.setLineWrap(true) 换行.setWrapStyleWord(true)在一个单词后换行我的代码示例:JPanel pane = new JPanel();// adding GridLayoutpane.setLayout(new GridLayout(4,2));// fields are filled just as an example// later they will get substituted by variablesJTextField 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 panepane.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 JOptionPaneint option = JOptionPane.showConfirmDialog(null,pane, "Fill all the fields",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
查看完整描述

1 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

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


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

  }

}

输出:

//img1.sycdn.imooc.com//62e1308300010dac03410218.jpg

查看完整回答
反对 回复 2022-07-27
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信