JOptionPane自定义输入
我想要做的就是拥有一个带有 JTextArea 而不是 JTextField 的 JOptionPane inputDialog。
我尝试将 JTextArea 放入 Message 参数中,如下所示,
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
但它的底部仍然有文本字段,我无法从 JTextArea 获取文本。 有没有办法删除原始文本字段并从 jtextarea 获取文本或将文本字段完全替换为文本区域?如果可能的话,我试图避免创建自定义对话框,这“看起来”像是应该很容易做到的事情?
All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
But it still has the text field at the bottom and I cannot get the text from the JTextArea.
Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的做法是正确的;您只需使用
showConfirmDialog
而不是showMessageDialog
,它允许您传递任何Component
作为“消息”并将其显示在 <代码>JDialog。如果用户单击“确定”,您就可以捕获JTextArea
的内容;例如,如果您想与
JTextArea
一起显示 JLabel,您可以创建并传入包含两个Component
的JPanel
;例如You're on the right lines; you just need to use
showConfirmDialog
instead ofshowMessageDialog
, which allows you to pass anyComponent
as your "message" and have it displayed within theJDialog
. You can then capture the contents of theJTextArea
if the user clicks OK; e.g.If you want to show a JLabel in conjunction with your
JTextArea
you can create and pass in aJPanel
containing bothComponent
s; e.g.