1
我想將控制檯帶入具有與掃描儀輸入交互功能的jFrame窗口應用程序。基本上,無論在控制檯中發生什麼,我都希望在TextArea的應用程序窗口中顯示它。有一個簡單的解決方案嗎?如何模擬帶有掃描儀輸入的控制檯或將控制檯帶入jFrame應用程序
這是我非常簡單的代碼
ConsoleLogic類:
import java.util.Scanner;
public class ConsoleLogic {
public static void main(String[] args) {
System.out.println("How old are you?");
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
System.out.println("");
System.out.println("How many siblings do you have?");
int input2 = scanner.nextInt();
System.out.println("Thank you for your answer!");
System.out.println("You are "+input+ " years old and you have "+input2+" 2siblings.");
}
}
ConsoleGui類:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
public class ConsoleGui extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ConsoleGui frame = new ConsoleGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ConsoleGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblDisplayOutput = new JLabel("Display output:");
lblDisplayOutput.setBounds(22, 25, 124, 16);
contentPane.add(lblDisplayOutput);
JTextArea textArea = new JTextArea();// ideally I would like to bring
// console and the inputs inside
// here
textArea.setBounds(22, 65, 402, 185);
contentPane.add(textArea);
}
}
非常感謝您的幫助。
編輯:我想有這樣的事情在這樣的畫面:(在Photoshop製造)
嗨,謝謝你的回答。但不是隻獲取最後一個MessageDialog文本,我需要在窗口中執行掃描器輸入。基本上,控制檯中發生的一切都應該發生在窗口的某個地方。 – medzi
看我的編輯。測試一下。 –
我編輯了我的問題。像這樣可行嗎? – medzi