扫描程序无法在ActionListener中运行

时间:2016-12-22 05:02:14

标签: java console actionlistener jtextarea system.in

我正在尝试通过将用户输入定向到JTextArea来模拟具有System.in的控制台。测试字符串成功附加到JTextArea,主方法的Scanner.nextLine()成功等待并打印用户输入。按下按钮时,相同的追加和扫描方法不起作用。有什么建议?感谢。

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ScannerTest {
    public static void main(String[] args) throws IOException {
        PipedInputStream inPipe = new PipedInputStream();
        System.setIn(inPipe);
        PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);

        JTextArea console = console(inWriter);
        Scanner sc = new Scanner(System.in);

        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                console.append("button pressed\n");
                console.append("got from input: " + sc.nextLine() + "\n"); // cause of problem???
            }
        });

        JFrame frame = new JFrame("Console");
        frame.getContentPane().add(console);
        frame.getContentPane().add(button, "South");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        console.append("test\n");
        console.append("got from input: " + sc.nextLine() + "\n");
    }
    public static JTextArea console(final PrintWriter in) {
        final JTextArea area = new JTextArea();
        area.addKeyListener(new KeyAdapter() {
            private StringBuffer line = new StringBuffer();
            @Override public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (c == KeyEvent.VK_ENTER) {
                    in.println(line);
                    line.setLength(0); 
                } else if (c == KeyEvent.VK_BACK_SPACE) { 
                    line.setLength(line.length() - 1); 
                } else if (!Character.isISOControl(c)) {
                    line.append(e.getKeyChar());
                }
            }
        });
        return area;
    }
}

1 个答案:

答案 0 :(得分:0)

我觉得你过于复杂。既然你想要一个控制台,我在这里为你提供一个更简单的解决方案。由于bug导致sc.nextLine(),我不建议在您的上下文中使用BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String str = reader.readLine() 。这可能是你的问题的原因,看看它。您可以通过各种方式获得输入。例如:

String str = System.console().readLine();

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class MyConsole {
    public static void main(String[] args) {

        JTextField field = new JTextField();
        JTextArea area = new JTextArea();

        area.setLineWrap(true);

        field.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    area.setText(area.getText() + "\n" + field.getText()); //Do whatever you like with the stirng
                    field.setText("");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

        });


        JScrollPane scPane = new JScrollPane(area);
        scPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                area.setText(area.getText() + "\n" + field.getText()); //You can also use button as well
                field.setText("");
            }
        });

        JFrame frame = new JFrame("Console");

        frame.getContentPane().add(field, BorderLayout.NORTH);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        frame.getContentPane().add(scPane, BorderLayout.CENTER);
        frame.getContentPane().setPreferredSize(new Dimension(400, 400));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

长话短说,这就是我写的。它可能符合您的目的:

"feathers-primus": "^2.0.0",