使用JTextArea而不是文本字段的JOptionPane? [英] JOptionPane with a JTextArea instead of a text field?

查看:75
本文介绍了使用JTextArea而不是文本字段的JOptionPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从弹出JOptionPane(或其他一些弹出窗口)中获取多行输入,但是我的大部分搜索将我定向到那里,这是我遇到的问题..).我坚持使用

JOptionPane.showInputDialog(null, new JTextArea(20,20));

但是我只希望将20x20区域读取为字符串,并且不显示任何文本字段. 我认为必须有某种方法可以执行此操作,但是其他类型的对话框似乎只返回一个int ...不必是JOptionPane,只要它是与主GUI分开的弹出窗口即可读回一个字符串以供读取.

解决方案

在传递给JOptionPane之前保留对JTextArea的引用,JOptionPane会告诉您用户的操作(他们如何关闭对话框) ),并根据结果,您可能想做不同的事情

JTextArea ta = new JTextArea(20, 20);
switch (JOptionPane.showConfirmDialog(null, new JScrollPane(ta))) {
    case JOptionPane.OK_OPTION:
        System.out.println(ta.getText());
        break;
}

有关更多详细信息,请参见如何制作对话框 >

I need to grab multiple lines of input from a popup JOptionPane (or some other popup, but most of my searches direct me there, which is where I get stuck..). I am stuck at using

JOptionPane.showInputDialog(null, new JTextArea(20,20));

but I want just the 20x20 area to be read to a String and no text field shown. I figure there must be some way to do this, but the other types of dialog only seem to return an int... It doesn't have to be a JOptionPane, as long as it is a separate popup from my main GUI that can read a string back in to be read.

解决方案

Keep a reference to the JTextArea before passing to the JOptionPane, the JOptionPane will tell you what the user did (how they closed the dialog) and depending on the result, you might want to do different things

JTextArea ta = new JTextArea(20, 20);
switch (JOptionPane.showConfirmDialog(null, new JScrollPane(ta))) {
    case JOptionPane.OK_OPTION:
        System.out.println(ta.getText());
        break;
}

See How to Make Dialogs for more details

这篇关于使用JTextArea而不是文本字段的JOptionPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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