JOptionPane在不同的行上有多个输入 [英] JOptionPane with multiple inputs on different lines

查看:150
本文介绍了JOptionPane在不同的行上有多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个JOptionPane,允许用户选择要连接的IP地址和端口.它应该这样构造,

I'd like to create a JOptionPane that allows a user to select an IP address and port to connect to. It should be structured as such,

IP地址:[此处为文本字段]

IP Address: [textfield here]

端口:[此处为文本字段]

Port: [textfield here]

取消确定

标签应左对齐,文本字段也应左对齐.我不能在此处真正为情节提要建模,但基本上,即使标签和文本字段之间有空格,文本字段也不应错位.

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.

使用以下方法将标签中的文本右对齐:

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

改进

这是使用嵌套布局,BorderLayoutWESTCENTER中的GridLayout实例完成的.

Improvement

It is done using a nested layout, GridLayout instances in the WEST and CENTER of a BorderLayout.

最好使用功能更强大的布局(例如MigLayout)或更现代的J2SE布局(例如BoxLayoutGroupLayout)来完成.

It might be better done using a more powerful layout such as MigLayout or more modern J2SE layout such as BoxLayout or GroupLayout.

GroupLayout可以提供此UI所需的对齐类型,而不会将CENTER字段拉伸到相同的宽度(也可以在上述嵌套布局中固定,但需要2个以上的约束面板).我相信其他两个人也可以胜任,但是他们没有太多经验.

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). I believe the other two could do the job as well, but don't have as much experience with them.

这篇关于JOptionPane在不同的行上有多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆