Java 在正确的窗口中创建GUI
我正在尝试创建一个包含4个字段的GUIJava 在正确的窗口中创建GUI,java,swing,user-interface,Java,Swing,User Interface,我正在尝试创建一个包含4个字段的GUI 网址 用户名 密码 声明 第一次,字段应该是空的。稍后,密码字段旁边的所有字段都应该包含上次的信息 问题: GUI窗口不应具有标准大小 我希望达到的目标是: 当窗口打开时,应根据笔记本电脑的屏幕大小进行动态调整,例如中心,屏幕的20% 第四个字段(语句)可能很长,GUI窗口会自动变长 我希望达到的目标是: 对于第四个字段,字符串应分解为多行。在将字段拆分为三行而不是继续拆分为第四行之后,一个选项可以是滚动条 到目前为止,我已经找到了一些可以帮助我的
- 当窗口打开时,应根据笔记本电脑的屏幕大小进行动态调整,例如中心,屏幕的20%
- 对于第四个字段,字符串应分解为多行。在将字段拆分为三行而不是继续拆分为第四行之后,一个选项可以是滚动条
- JOptionPane
- 杰帕内尔
- 第四个长场地的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);
您可以使用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);
}
}
输出:
您可以使用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);
}
}
输出:
“20%的屏幕”-GUI开发人员不应该强制使用这种大小。Swing和桌面以及用户的首选项决定字体大小,Swing组件会自动适应各自字体的大小。JOptionPane将为您居中显示窗口。有关组件的排列,请参阅。通常,标签/字段UI模式是通过一个网格来完成的。除了
GridBag
,请注意Oracle实际上建议使用GUI布局工具,如NetBeans。查看他们的,并阅读开头附近的特别说明,其中建议使用GUI布局工具并链接到NetBeans页面。@markspace如果我使用intellij IDE怎么办?我不知道该IDE,所以我想你必须在他们的论坛上询问。没有理由不能在同一个项目上同时使用NetBeans和IntelliJ。NetBeans只是为它的GUI类编写常规代码。@VGR感谢您的提示。它与GridBagLayout配合使用,尽管我必须预先设置字段输入的大小。有没有一种方法可以从字段中提取输入,并将一个较长的给定输入分解为更多行?“屏幕的20%”GUI开发人员不应该这样强制大小。Swing和桌面以及用户的首选项决定字体大小,Swing组件会自动适应各自字体的大小。JOptionPane将为您居中显示窗口。有关组件的排列,请参阅。通常,标签/字段UI模式是通过一个网格来完成的。除了GridBag
,请注意Oracle实际上建议使用GUI布局工具,如NetBeans。查看他们的,并阅读开头附近的特别说明,其中建议使用GUI布局工具并链接到NetBeans页面。@markspace如果我使用intellij IDE怎么办?我不知道该IDE,所以我想你必须在他们的论坛上询问。没有理由不能在同一个项目上同时使用NetBeans和IntelliJ。NetBeans只是为它的GUI类编写常规代码。@VGR感谢您的提示。它与GridBagLayout配合使用,尽管我必须预先设置字段输入的大小。有没有一种方法可以从字段中提取输入,并将一个较长的给定输入分解成更多的行?很好:)不过有两个问题。我认为使用“ipadx”可以选择白色字段的宽度和大小。不知怎么的,这是行不通的。在哪一部分中,您选择了所有四个白色字段的大小?第二个问题。如何才能获得用户在白色字段中写入的值?经过一些挖掘和测试,我发现“GridBagConstraints.HORIZONTAL”在某种程度上起到了作用。它只是将整个窗口调整为参数的宽度(水平)。@Georgios,是的,当我们将GridBagConstraints.fill
设置为GridBagConstraints.HORIZONTAL
,组件“填充”单元格。要从文本组件中获取值,可以使用getText()
method.Brilliant:)两个问题。我认为使用“ipadx”可以选择白色字段的宽度和大小。不知怎么的,这是行不通的。在哪一部分中,您选择了所有四个白色字段的大小?第二个问题。如何才能获得用户在白色字段中写入的值?经过一些挖掘和测试,我发现“GridBagConstraints.HORIZONTAL”在某种程度上起到了作用。它只是将整个窗口调整为参数的宽度(水平)。@Georgios,是的,当我们将GridBagConstraints.fill
设置为GridBagConstraints.HORIZONTAL
,组件“填充”单元格。要从文本组件获取值,可以使用getText()
方法。