我的GUI有一个JTextArea:JTextArea textArea = new JTextArea();
我需要实现点击一个JButton来触发事件,打印出输入到JTextArea内的内容。由用户输入。
请问如何实现。我的监听器接收打印,但如何把用户的输入获取?用输入流吗?怎么实现?谢谢。

解决方案 »

  1.   

    System.out.println(textArea.getText());
      

  2.   

    我就是用了上面的方法,却无法得到字符串。这个方法要用在PANEL中还是监听器中?
      

  3.   

    你输入的能在JTextArea区显示吗
      

  4.   

    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();无法收到 。
            请检查一下我这里出现的错误,谢谢。
      

  5.   

    首先实例化一个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;
       }
      

  6.   

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

  7.   

    同意你的看法,不过你可以考虑有jbuilder来试试
      

  8.   

    学习
    JAVA的图形界面看来还是很有一搞的
      

  9.   

    很明显,在ShellPanelAction 中的panel不是你gui中的那个,你又new 了一个没有显示出来的panel,还要获取其中的textarea中的信息,当然出错了~~~