我的GUI有一个JTextArea:JTextArea textArea = new JTextArea();
我需要实现点击一个JButton来触发事件,打印出输入到JTextArea内的内容。由用户输入。
请问如何实现。我的监听器接收打印,但如何把用户的输入获取?用输入流吗?怎么实现?谢谢。
我需要实现点击一个JButton来触发事件,打印出输入到JTextArea内的内容。由用户输入。
请问如何实现。我的监听器接收打印,但如何把用户的输入获取?用输入流吗?怎么实现?谢谢。
调试欢乐多
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();无法收到 。
请检查一下我这里出现的错误,谢谢。
文本域中的内容,可以用输入流来实现!具体的实现代码如下: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;
}
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);
}
}
JAVA的图形界面看来还是很有一搞的