【发布时间】:2018-07-17 22:25:08
【问题描述】:
我在JDialog 中有一个JTextArea。 JDialog 使用 GridLayout。
我希望JTextArea 的每一行都有 7 位数字(每行将是一个 7 int 注册号)。出于用户体验的原因,我希望 JTextArea 在一行达到 7 个字符时添加新行或停止扩展。
没用的东西:
- 在JTextArea 构造函数中指定列数
- matriculesText.setLineWrap(true); 和 matriculesText.setWrapStyleWord(true);
恐怕uploadDialogManager.setHgap(20); 可能会破坏密码。我想知道JDialog 是否应该有一个固定的大小。
这就是我构建JDialog 的方式:
// initialization
Dialog uploadParent = null;
JDialog uploadDialog = new JDialog(uploadParent);
GridLayout uploadDialogManager = new GridLayout(UPLOAD_DIALOG_ROWS,
UPLOAD_DIALOG_COLUMNS);
// uploadDialog properties
uploadDialog.setSize(new Dimension(UPLOAD_DIALOG_WIDTH, UPLOAD_DIALOG_HEIGHT));
uploadDialog.setLayout(uploadDialogManager);
uploadDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//Set up the horizontal gap value
uploadDialogManager.setHgap(20);
//Set up the vertical gap value
uploadDialogManager.setVgap(20);
//Set up the layout of the buttons
//uploadDialogManager.layoutContainer();
// components initialization
JLabel exerciceLabel = new JLabel("exercice number : ");
JComboBox<Integer> exerciceNumbers = new JComboBox<Integer>
(EXERCICE_NUMBERS);
JLabel matriculeLabel = new JLabel("please enter your matricules, one per
line : ");
JTextArea matriculesText = new JTextArea(1, 1);
JButton confirm = new JButton("confirm");
JButton cancel = new JButton("cancel");
matriculesText.setLineWrap(true);
matriculesText.setWrapStyleWord(true);
【问题讨论】:
-
使用 JTextField 输入数据。您可以使用
DocumentFilter轻松地将文本限制为 7。然后用户可以使用 Enter 键或单击按钮将数据移动到文本区域。或者也许您应该将数据移动到 JList 以便可以单独处理数据。 -
7 x
new JSpinner(new SpinnerNumberModel(1000000,1000000,9999999,1));.. -
这是一个相当复杂的要求,但您需要以
DocumentFilter开头,诀窍是知道何时有新行。也许像this example 这样的东西可能会给你一个起点。就个人而言,我认为您将不断遇到这种方法的问题。我同意已经提出的建议,将输入限制为单个字段,这将为您提供更好的控制,然后在不同的组件中显示结果 -
感谢大家的反馈!我与我的主管重新检查了我的要求。
String.split("\n")我应该没问题,然后使用正则表达式检查每个分割的字符串。 (我会试试这个)
标签: java swing jtextarea documentfilter