使用不同的System.in冻结JFrame

时间:2013-12-14 18:11:18

标签: java swing jframe swingworker

任何人都可以告诉我为什么TryGraphic在第一个JFrame中使用扫描仪冻结main()?如果我删除Scanner或直接执行代码,则一切正常。 (最初的“尝试”类显然做了很多不同的事情。我写这些类是为了简单。)

import java.util.Scanner;


public class Try {

    public Try(){

    }

    public static void main(String[] args){
        System.out.println("FOO");
        String s = new Scanner(System.in).nextLine();
        System.out.println(s);
    }
}

这是图形实现:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;

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


public class TryGraphic extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 7491282237007954227L;



    private JButton execute = new JButton("Esegui");

    private PipedInputStream inPipe = new PipedInputStream(); 
    private PipedInputStream outPipe = new PipedInputStream(); 
    private JTextField tfIn = new JTextField();
    private JTextArea outputArea = new JTextArea();

    private PrintWriter inWriter;


    public TryGraphic(){
        super("TRY");

        System.setIn(inPipe); 

        try {
            System.setOut(new PrintStream(new PipedOutputStream(outPipe), true)); 
            inWriter = new PrintWriter(new PipedOutputStream(inPipe), true); 
        }catch (IOException ioe){
            ioe.printStackTrace();
        }               

        tfIn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent event){
                    String text = tfIn.getText();
                    tfIn.setText("");
                    inWriter.println(text); 
            }
        });

        this.add(execute,BorderLayout.SOUTH);
        this.add(new JScrollPane(outputArea),BorderLayout.CENTER);
        this.add(tfIn, BorderLayout.NORTH);

        execute.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

                SwingWorker<Void,String> worker = new SwingWorker<Void, String>() { 
                     protected Void doInBackground() throws Exception { 
                        Scanner s = new Scanner(outPipe);
                        while (s.hasNextLine()) {
                                 String line = s.nextLine();
                                 publish(line);

                        }
                        return null; 
                    } 

                     @Override 
                     protected void process(java.util.List<String> chunks) { 
                         for (String line : chunks){ 
                             outputArea.append(line+System.lineSeparator());
                             outputArea.validate();
                             } 

                     } 
                };

                worker.execute();

                Try.main(new String[]{""});
            }

        });

        this.setSize(300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }



    public static void main(String[] args){
        new TryGraphic();
    }
}

1 个答案:

答案 0 :(得分:1)

您正在阻止GUI事件调度线程。您需要生成一个单独的线程来等待输入,这样您就可以保持GUI响应。

通过创建SwingWorker来处理TryGraphic课程中的I / O,您已经做了正确的事情。您应该执行类似的操作,将Try.main(new String[]{""});调用从事件调度线程移开,这将使您的JFrame无法锁定。