java 无法清空textfield内容 100
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.lang.Math;impo...
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 <correctAnswer; i++){
imagePanel.add( imageLabel[i]);
}
imagePanel.repaint();
}
}
public static void main(String[] args){
JungleParty myParty = new JungleParty();
myParty.ask();
}
}
我明明在每次getText输入后都写了textField.setText("");
但是一旦输入了数字就再也清空不掉了???
为什么 很急。。求帮助 展开
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 <correctAnswer; i++){
imagePanel.add( imageLabel[i]);
}
imagePanel.repaint();
}
}
public static void main(String[] args){
JungleParty myParty = new JungleParty();
myParty.ask();
}
}
我明明在每次getText输入后都写了textField.setText("");
但是一旦输入了数字就再也清空不掉了???
为什么 很急。。求帮助 展开
2个回答
2017-05-26
展开全部
输入校验的问题。
JFormattedTextField 设置的最小为1, 你设置为空所以不生效了。
JFormattedTextField 设置的最小为1, 你设置为空所以不生效了。
追问
那我现在该怎么办 既能输入1-10 又可以清空
追答
用回最初的 JTextField,那样自己在事件中处理内容。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询