100分求助取得JTEXTAREA的值的问题

lsbaiquan 2006-03-08 11:09:50
我的GUI有一个JTextArea:JTextArea textArea = new JTextArea();
我需要实现点击一个JButton来触发事件,打印出输入到JTextArea内的内容。由用户输入。
请问如何实现。

我的监听器接收打印,但如何把用户的输入获取?

用输入流吗?

怎么实现?

谢谢。
...全文
300 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
petit 2006-03-12
  • 打赏
  • 举报
回复
很明显,在ShellPanelAction 中的panel不是你gui中的那个,你又new 了一个没有显示出来的panel,还要获取其中的textarea中的信息,当然出错了~~~
cfsego 2006-03-10
  • 打赏
  • 举报
回复
学习
JAVA的图形界面看来还是很有一搞的
grass_12 2006-03-10
  • 打赏
  • 举报
回复
同意你的看法,不过你可以考虑有jbuilder来试试
aaa2003gf 2006-03-08
  • 打赏
  • 举报
回复
你输入的能在JTextArea区显示吗
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
我就是用了上面的方法,却无法得到字符串。

这个方法要用在PANEL中还是监听器中?


hongke1490 2006-03-08
  • 打赏
  • 举报
回复
System.out.println(textArea.getText());
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
非常感谢。


我还是对象的观念没建立,要加强这一点。谢谢。
gtlang78 2006-03-08
  • 打赏
  • 举报
回复
class shellPanel
extends JPanel
{
public shellPanel()
{
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
ShellPanelAction rsaction = new ShellPanelAction(textArea);
JLabel state = new JLabel("use the shell ");
JButton button = new JButton("Run Shell");
button.addActionListener(rsaction);
add(state, BorderLayout.NORTH);
add(button, BorderLayout.EAST);
add(scrollPane, BorderLayout.CENTER);
}
}

class ShellPanelAction
implements ActionListener
{
private JTextArea textarea = null;

public ShellPanelAction(JTextArea textarea)
{
this.textarea = textarea;
}

public void actionPerformed(ActionEvent e)
{
shellPanel panel = new shellPanel();
String shell = textarea.getText();
System.out.println(shell);
//RunShell runshell = new RunShell();
//runshell.runshell(shell);
}
}
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
对不起,IO没有学好,请问如何用在我的代码中?谢谢。
rongdajian 2006-03-08
  • 打赏
  • 举报
回复
首先实例化一个JTextArea对象,然后用getText()方法,得到
文本域中的内容,可以用输入流来实现!具体的实现代码如下:

public String readString()
{
String string = new String();

BufferedReader in = new BufferedReader(new InputStreamReade(System.in));

try{

string = in.readLine();
}
catch(IOException e)
{
System.out.println("InputText.readString:Unknown error...");

System.exit(-1);

}
return string;
}
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
没有人关注一下吗?
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
public class shellPanel extends JPanel {

public shellPanel() {
setLayout(new BorderLayout());
JTextArea textArea=new JTextArea();
JScrollPane scrollPane=new JScrollPane(textArea);
ShellPanelAction rsaction=new ShellPanelAction();
JLabel state=new JLabel("use the shell ");
JButton button = new JButton("Run Shell");
button.addActionListener(rsaction);
add(state,BorderLayout.NORTH);
add(button,BorderLayout.EAST);
add(scrollPane,BorderLayout.CENTER);
}
}
这就是我的代码,是构建那个TEXTAREA的代码。下面是我的监听器代码。
public class ShellPanelAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
shellPanel panel = new shellPanel();
shell=panel.textArea.getText();
RunShell runshell = new RunShell();
runshell.runshell(shell);
}
String shell=null;
}
每次报错都是:Exception: Empty command,
请问要如何修改才能实现用户从JTextArea中输入信息,或者命令,然后监听器可以获取这些输入?
我用了JTextArea.getText();无法收到 。
请检查一下我这里出现的错误,谢谢。
happyboywzl 2006-03-08
  • 打赏
  • 举报
回复
放在Button的事件中
lsbaiquan 2006-03-08
  • 打赏
  • 举报
回复
可以。输入的可以显示。

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧