所以,我想知道是否Scanner可以System.in從JFrame. 這就是我的意思。
這是我的WriteToSystemIn(JFrame類),它是程式的 GUI 部分。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
public class WriteToSystemIn extends JFrame {
private static class ChangeNumber implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
ByteArrayInputStream s = null;
try {
s = new ByteArrayInputStream("1\n".getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
System.setIn(s);
}
}
WriteToSystemIn() {
JButton button = new JButton("try click it m8");
button.addActionListener(new ChangeNumber());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.setVisible(true);
this.pack();
}
}
這就是Main程式的功能。
import java.util.Scanner;
public class Main {
private static class MainRunnable implements Runnable {
@Override
public void run() {
new WriteToSystemIn();
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new MainRunnable());
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
System.out.println(s);
System.out.println("ended");
}
}
因此,當從 按下按鈕時WriteToSystemIn,應將“1\n”寫入System.inforScanner讀取。
但是,它沒有這樣做。它不會讀取任何內容。列印沒有問題,System.out所以我認為這不是問題,但顯然我錯了。所以,我在這里想知道,我在這里做錯了什么嗎?或者,我是否正在嘗試做一些不可能的事情?
uj5u.com熱心網友回復:
從@Torben 的回答繼續,您需要讓課堂Main 等到(在JButton課堂WriteToSystemIn上)被點擊。JButton單擊后,您可以通知它 Main可以停止等待并繼續執行。
班級Main
import java.util.Scanner;
public class Main {
private static class MainRunnable implements Runnable {
private Main main;
public MainRunnable(Main main) {
this.main = main;
}
@Override
public void run() {
new WriteToSystemIn(main);
}
}
public static void main(String[] args) {
Main main = new Main();
javax.swing.SwingUtilities.invokeLater(new MainRunnable(main));
synchronized (main) {
try {
main.wait();
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
System.out.println(s);
}
catch (InterruptedException x) {
x.printStackTrace();
}
}
System.out.println("ended");
}
}
班級WriteToSystemIn
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
public class WriteToSystemIn extends JFrame {
private static class ChangeNumber implements ActionListener {
private Main main;
public ChangeNumber(Main main) {
this.main = main;
}
@Override
public void actionPerformed(ActionEvent e) {
ByteArrayInputStream s = null;
try {
s = new ByteArrayInputStream("1\n".getBytes("UTF-8"));
System.setIn(s);
synchronized (main) {
main.notifyAll();
}
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
}
}
WriteToSystemIn(Main main) {
JButton button = new JButton("try click it m8");
button.addActionListener(new ChangeNumber(main));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.setVisible(true);
this.pack();
}
}
uj5u.com熱心網友回復:
當您呼叫時,new Scanner(System.in);您給掃描儀一個對當前設定為的流的參考System.in。當您稍后呼叫時System.setIn(s);,您只需更改存盤在System.in. 已經提供給掃描儀的參考沒有改變,它仍然指向原來的System.in。
您需要確保System.setIn(s);在初始化掃描儀之前呼叫它。您可以向這兩個陳述句添加除錯列印以驗證它們的執行順序。這在您學習多執行緒編程時會很有幫助。
System.out.println("Resetting System.in");
System.setIn(s);
...
System.out.println("Creating scanner");
Scanner scanner = new Scanner(System.in);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/516706.html
上一篇:java.awt.IllegalComponentStateException:組件必須在螢屏上顯示以確定其位置
