我必须说我是Java的初学者。我用Eclipse。我想要完成以下场景,但无法找到如何做到这一点:
当一个java程序运行它将文本输出到控制台时,我还希望能够输入文本并处理它,而不需要等待输入来阻止输出。
假设如下:
-Thread 1每秒钟向控制台输出一个数字。
-Thread 2侦听输入
(代码是一个模型)
//**Thread 1:**
int incrementBy = 0;
for (int i = 0; i < 1000; i++) {
i = i + incrementBy;
//Pause for 1 seconds
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("TEXT OUTPUT INTERUPTED");
}
//Print text
System.out.println(i);
}
//**Thread 2:**
String myIncrement = System.console().readLine();
(Now process the input and change the incrementBy var in Thread 1)现在,在我的程序中,我使用一个线程作为输入,另一个线程用于输出。但我可以很容易地改变设计。我所能找到的只是关于服务器和客户端的东西,我想把我的代码保存在一个地方-包。我目前还不知道如何用文本框来输出和输入一个GUI。
能给我推荐一下吗?
发布于 2014-11-09 20:22:45
解决了--原来我对JAVA非常陌生。
java似乎允许用户在另一个线程输出到控制台时输入文本。
这就是为什么我在搜索诸如"java输入和输出到控制台异步“的搜索中找不到任何东西的原因。在我的输入代码中,我的输入代码出现了问题,这正是我要求输入的地方,而且由于我从单线程程序中知道程序停止,直到我输入文本并按enter键,所以我假设抛出错误是因为输出线程正在接管控制台并终止输入线程。
以下是我为那些搜索者编写的代码(将其作为指南,如果编译可能无法工作):
//Main app
public class textInpuOutputManager {
//here we create the two threads (objects that implement the runnable interface)
static TextInputObject ti;
static TextOutputObject to;
public static void main(String[] args) {
//we instantiate the objects
ti = new TextInputObject();
to = new TextOutputObject();
//we call the start method to start the threads for input and output
ti.start();
to.start();
}
}
//TextInputObject class
public class TextInputObject implements Runnable {
//Method that gets called when the object is instantiated
public TextInputObject() {
System.out.println("Created TextInputObject");
}
//create a thread object and check if it's not already created
static Thread thread;
//This method gets called from the main
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
//this method gets called by the thread.start(); from above
@
Override
public void run() {
System.out.println("Text input thread created and now it runs");
readTextFromConsole();
}
Scanner inputReader = new Scanner(System.in);
//check for input all the time - THIS WILL NOT HALT THE PROGRAM
public void readTextFromConsole() {
System.out.println("Enter something:");
String myinput = inputReader.nextLine();
System.out.println("You Entered: " + myinput);
readTextFromConsole();
}
}
//TextOutputObject
public class TextOutputObject implements Runnable {
//Method that gets called when the object is instantiated
public TextOutputObject() {
System.out.println("Created TextOutputObject");
}
static Thread thread;
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
@
Override
public void run() {
System.out.println("Text output thread created and now it runs");
//Make it output text every 4 seconds to test if you can input text while it's used for output
for (int i = 0; i < 100; i++) {
//Pause for 4 seconds
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
System.out.println("TEXT OUTPUT INTERUPTED");
}
//Print i to console
System.out.println(i);
}
}
}
也非常感谢你们花时间回应
发布于 2014-11-09 17:55:21
我不知道你到底想做什么,但如果你是新来的,你不知道怎么做guis,我想试试JOptionPane
String input = JOptionPane.showInputDialog("User input is returned as a string; use Integer.parseInt(input) to retrieve an integer from this method");发布于 2014-11-09 18:03:31
您可以创建两个内部类,并在它们中实现Runnable。
import java.util.Scanner;
public class Test{
private Thread t1;
private Thread t2;
public static void main(String[] args){
new Test();
}
private class TOne implements Runnable{
public void run(){
int incrementBy = 0;
for (int i = 0; i < 1000; i++) {
i = i + incrementBy;
//Pause for 1 seconds
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("TEXT OUTPUT INTERUPTED");
}
//Print text
System.out.println(i);
}
}
}
private class TTwo implements Runnable{
public void run(){//Code for Thread 2
try{
Scanner scr = new Scanner(System.in);
System.out.println(scr.next());
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public Test(){
t1 = new Thread(new TOne());
t1.run();
t2 = new Thread(new TTwo());
t2.run();
}
}这不是最优雅的方式,也不是完美无缺的。你得再多修一下第二条线。有关GUI等工作方式的信息,您应该检查Swing库。在谷歌上搜索应该能正常工作。
一些重要的关键词是: JFrame,JPanel,LayoutManager,JTextArea,JTextField,JButton,ActionListener,内部类
https://stackoverflow.com/questions/26831190
复制相似问题