我正在尝试创建一个包含4个字段的GUI。
第一次,这些字段应该为空。稍后,密码字段旁边的所有字段都应包含上次的信息。
问题:
GUI窗口不应具有标准大小
我想要实现的目标:
第四个字段(语句)可能很长,并且GUI窗口会自动变得太长。
我想要实现的目标:
直到现在,我发现了一些可以提供帮助的对象。
JTextArea 用于第四个长字段
对于 JTextArea ,还有
我的代码示例:
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);
答案 0 :(得分:2)
您可以使用JTextArea
设置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);
}
}
输出: