简体   繁体   English

JOptionPane在不同的行上有多个输入

[英]JOptionPane with multiple inputs on different lines

I'd like to create a JOptionPane that allows a user to select an IP address and port to connect to. 我想创建一个允许用户选择要连接的IP地址和端口的JOptionPane It should be structured as such, 它的结构应如此,

IP Address: [textfield here] IP地址:[textfield here]

Port: [textfield here] 港口:[textfield here]

Cancel OK 取消确定

The labels should be aligned to the left, and the textfields should be left aligned too. 标签应该左对齐,文本字段也应该左对齐。 I can't really model the storyboard here, but basically the textfields shouldn't be misaligned, even if there is a space between the labels and the textfields. 我无法在这里对故事板进行真正的建模,但基本上文本字段不应该错位,即使标签和文本字段之间有空格也是如此。

Each label and textfield pair should be on seperate lines, and the Cancel OK buttons should be aligned to the right, below the textboxes. 每个标签和文本字段对应位于单独的行上,取消确定按钮应与文本框下方的右侧对齐。

Is there any way to do this in code? 在代码中有没有办法做到这一点?

See this example that seems similar in layout. 看到这个布局似乎相似的例子

The example 这个例子

Right align 右对齐

Right aligns the text in the labels, which I think looks better, using this: 右边对齐标签中的文字,我觉得看起来更好,使用这个:

labels.add(new JLabel("User Name", SwingConstants.RIGHT));

For left aligned text, change it to: 对于左对齐文本,将其更改为:

labels.add(new JLabel("User Name"));

Improvement 起色

It is done using a nested layout, GridLayout instances in the WEST and CENTER of a BorderLayout . 它是使用嵌套布局, BorderLayoutWESTCENTER中的GridLayout实例完成的。

It might be better done using a more powerful layout such as MigLayout or more modern J2SE layout such as BoxLayout or GroupLayout . 使用更强大的布局(如MigLayout或更现代的J2SE布局(如BoxLayoutGroupLayout可能会更好。

GroupLayout can provide the type of alignment this UI needs, while not stretching the CENTER fields to the same width (which is also fixable in a nested layout as above, but requires 2 more constraining panels). GroupLayout可以提供此UI所需的对齐类型, GroupLayout CENTER字段拉伸到相同的宽度(这也可以在上面的嵌套布局中固定,但需要另外2个约束面板)。 I believe the other two could do the job as well, but don't have as much experience with them. 我相信其他两个人也可以做这个工作,但没有那么多经验。

Create a custom dialog (jdialog) or use JOptionPane.showInputDialog, see this tutorial: 创建自定义对话框(jdialog)或使用JOptionPane.showInputDialog,请参阅本教程:

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM