javaGUI中怎么实现等待用户输入

我写了一个小游戏,现在想把这个游戏移植到javaGUI上。我想问怎么利用JTextField实现system.in的功能。因为我要执行用户输入的命令。
我的游戏主engine是检查游戏最终目标完成没,像这样
while(!finished){
//commandline就是input.getText();
//input就是JTextField;
process(commandline);
finished = checkGoal; //return true or false
}
所以,必须考虑到JTextField为空的情况。而且像我的程序的话,整个程序会一直执行,非常浪费电脑资源。所以如果有哪位大侠知道怎么解决这个问题的话,请告诉我。答得好的话追加50-100分。谢谢了。(一定要附一个可以正常运行的例子,因为我是新手,才学了java2个月不到)

第1个回答  2010-11-28
JTextField jt=new JTextField(15);
String s=null;
s=jt.getText();
if(s==""||s.length()==0)
{
//写上你自己的判断啊
}
第2个回答  2010-11-28
java的GUI有个控件是专门用来等待输入的,你查查API看看
第3个回答  2010-11-28
可以使用Timer类,使用:
TimerTask tt=new TimerTask(){
public void run(){
...//TODO:加入你的代码
}
}
Timer tm=new Timer();
tm.schedule(tt,0,1000);
还有一种解决方法:
JButton submit=new JButton("提交");
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process(commandline);
}
});
我写了几个小游戏,地址是:
http://cid-9809ef920f6cc52a.office.live.com/browse.aspx/.Public/Java
另外我还写了一个贪食蛇:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SnakeGame extends JFrame {
public SnakeGame(){
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
gamePanel=new SnakePanel();
Dimension size=gamePanel.getPreferredSize();
this.setSize(size.width+6,size.height+56);
this.add(gamePanel,BorderLayout.CENTER);
newGame=new JButton("New Game");
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
gamePanel.newGame();
}
});
newGame.addKeyListener(listener);
this.add(newGame,BorderLayout.SOUTH);
}

public static void main(String[] args){
new SnakeGame().setVisible(true);
}

private JButton newGame;
private SnakePanel gamePanel;
private KeyListener listener=new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
int code=e.getKeyCode();
if(code==KeyEvent.VK_UP){
gamePanel.move(Snake.up);
}
else if(code==KeyEvent.VK_RIGHT){
gamePanel.move(Snake.right);
}
else if(code==KeyEvent.VK_LEFT){
gamePanel.move(Snake.left);
}
else if(code==KeyEvent.VK_DOWN){
gamePanel.move(Snake.down);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
};
}

class SnakePanel extends JPanel {

public SnakePanel(){
food=this.getFoodPoint(snake.getBody());
}

public void paintComponent() {
Graphics g=this.getGraphics();
if(g==null)
return;
//clear
g.clearRect(0, 0, this.getWidth(), this.getHeight());
//draw the border
g.setColor(Color.BLACK);
for(int i=0;i<WIDTH+2;i++){
g.fillRect(i*SIZE.width, 0, SIZE.width, SIZE.height);
g.fillRect(i*SIZE.width,(HEIGHT+1)*SIZE.height,SIZE.width,SIZE.height);
}
for(int i=1;i<HEIGHT+1;i++){
g.fillRect(0,i*SIZE.height , SIZE.width, SIZE.height);
g.fillRect((WIDTH+1)*SIZE.width,i*SIZE.height,SIZE.width,SIZE.height);
}
//draw the food
g.setColor(Color.RED);
g.fillRect(food.x*SIZE.width, food.y*SIZE.height,SIZE.width, SIZE.width);
//draw the snake
LinkedList<Point> body=snake.getBody();
Point head=body.get(0);
g.setColor(Color.ORANGE);
g.fillRect(head.x*SIZE.width, head.y*SIZE.height, SIZE.width, SIZE.height);
g.setColor(Color.GREEN);
for(int i=1;i<body.size();i++){
Point p=body.get(i);
g.fillRect(p.x*SIZE.width, p.y*SIZE.height, SIZE.width, SIZE.height);
}
//draw text
if(gameOver){
g.setColor(Color.BLUE);
Dimension size=this.getPreferredSize();
String s="Game Over";
Font f=new Font(g.getFont().getName(),Font.BOLD,32);
g.setFont(f);
FontMetrics fm=g.getFontMetrics(f);
int length=fm.stringWidth(s);
int height=fm.getHeight();
int x=(size.width-length)/2;
int y=(size.height-height)/2;
g.drawString(s, x, y);
}
}

public Dimension getPreferredSize(){
return new Dimension((WIDTH+2)*SIZE.width,(HEIGHT+2)*SIZE.height);
}

public Point getFoodPoint(Collection<Point> except) {
Random random = new Random();
int x = random.nextInt(30)+1;
int y = random.nextInt(30)+1;
Point result=new Point(x,y);
boolean has=false;
while(true){
Iterator<Point> iter=except.iterator();
while(iter.hasNext()){
if(iter.next().equals(result)){
has=true;
break;
}
}
if(!has)
break;
x = random.nextInt(30);
y = random.nextInt(30);
result=new Point(x,y);
has=false;
}
return new Point(x, y);
}

public void newGame(){
if(timer!=null)
timer.cancel();
gameOver=false;
snake.setBody(new Point[] {
new Point(10, 10), new Point(10, 11), new Point(10, 12),
new Point(10, 13) });
snake.setTowards(Snake.up);
food=this.getFoodPoint(snake.getBody());
timer=new Timer();
timer.schedule(new TimerTask(){
public void run(){
move(snake.getTowards());
}
}, 500, 500);
paintComponent();
}

public void move(int towards){
if(gameOver)
return;
Boolean r=SnakePanel.snake.move(towards,food);
if(r==null)
return;
LinkedList<Point> body=snake.getBody();
if(r)
food=this.getFoodPoint(body);
Point head=body.get(0);
boolean over=false;
for(int i=1;i<body.size();i++){
if(head.equals(body.get(i))){
System.out.println(head+""+i+body.get(i));
over=true;
break;
}
}
if(!over){
if(head.x<1||head.y<1||head.x>WIDTH||head.y>HEIGHT)
over=true;
}
if(over){
gameOver=true;
timer.cancel();
paintComponent();
return;
}
paintComponent();
}

private static final int WIDTH = 30;
private static final int HEIGHT = 30;
private static final Dimension SIZE=new Dimension(15,15);
private Point food;
private boolean gameOver=true;
private Timer timer;

public static final Snake snake = new Snake(new Point[] {
new Point(10, 10), new Point(10, 11), new Point(10, 12),
new Point(10, 13) }, Snake.up);
}

class Snake {

public Snake(Point[] body, int towards) {
this.setBody(body);
this.setTowards(towards);
}

public Boolean move(int orien,Point food) {
int x, y;
Point head = body.get(0);
switch (orien) {
case up:
if(towards==down)
return null;
x = head.x;
y = head.y-1;
this.setTowards(up);
break;
case down:
if(towards==up)
return null;
x = head.x;
y = head.y + 1;
this.setTowards(down);
break;
case left:
if(towards==right)
return null;
x = head.x - 1;
y = head.y;
this.setTowards(left);
break;
case right:
if(towards==left)
return null;
x = head.x + 1;
y = head.y;
this.setTowards(right);
break;
default:
return null;
}
Point newHead=new Point(x,y);
body.addFirst(newHead);
if(newHead.equals(food)){
return true;
}
body.removeLast();
return false;
}

public void eat(Point food) {
body.addFirst(food);
}

public int getTowards() {
return this.towards;
}

public void setTowards(int towards) {
this.towards=towards;
}

public LinkedList<Point> getBody(){
return body;
}

public void setBody(Point[] body){
this.body=new LinkedList<Point>();
for (Point b : body)
this.body.add(b);
}

public Point getHead(){
return this.body.get(0);
}

private LinkedList<Point> body;
private int towards = right;

public static final int up = 1;
public static final int down = 2;
public static final int left = 3;
public static final int right = 4;
}本回答被提问者和网友采纳