java textfield 清空_java 无法清空textfield内容

importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.lang.Math;importjavax.swing.text.NumberFormatter;importjava.text.NumberFormat;publicclassJunglePart...

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.lang.Math;

import javax.swing.text.NumberFormatter;

import java.text.NumberFormat;

public class JungleParty extends JFrame implements ActionListener{

public static int correctAnswer = 10;

JLabel questionLabel = new JLabel("How many animals have come to the party?");

JPanel imagePanel = new JPanel();

JLabel[] imageLabel = new JLabel[10] ;

JPanel bottom = new JPanel();

JFormattedTextField textField;

/**

* This is a constructor of JungleFrame,

* used to create the window frame for application.

*/

public JungleParty(){

super("Welcome to the jungle party!");

setSize(750,500);

setLocationRelativeTo(null);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

/**

* This is a method to start displaying and asking.

* No argument and no return type here.

*/

public void ask(){

NumberFormat format = NumberFormat.getInstance();

NumberFormatter formatter = new NumberFormatter(format);

formatter.setValueClass(Integer.class);

formatter.setMinimum(1);

formatter.setMaximum(10);

formatter.setAllowsInvalid(false);

formatter.setCommitsOnValidEdit(true);

textField = new JFormattedTextField(formatter);

textField.setColumns(2);

textField.setMaximumSize(textField.getPreferredSize());

textField.addActionListener(this);

JPanel askPanel = new JPanel();

askPanel.setLayout(new FlowLayout());

askPanel.add(questionLabel);

askPanel.add(textField);

addPic();

无关的

public void actionPerformed(ActionEvent e){

int userNumber = Integer.valueOf(textField.getText()).intValue();

if (userNumber != correctAnswer){//correct answer

questionLabel.setText("Wrong! Try again!");

textField.setText("");

}

else{

correctAnswer = (int)(Math.random()*10 + 1);

questionLabel.setText("Correct! How many animals are in the party now?");

textField.setText("");

imagePanel.removeAll();

for(int i = 0; i

imagePanel.add( imageLabel[i]);

}

imagePanel.repaint();

}

}

public static void main(String[] args){

JungleParty myParty = new JungleParty();

myParty.ask();

}

}

我明明在每次getText输入后都写了textField.setText("");

但是一旦输入了数字就再也清空不掉了???

为什么 很急。。求帮助

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值