JTextField中添加DocumentListener事件后出现问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Welcome extends JFrame implements ActionListener,ChangeListener,DocumentListener
{
private JSlider inputSlider;
private JTextField inputField;
private JLabel inputLabel;
private JTextField outputField;
private JSlider outputSlider;
private JLabel outputLabel;
private void creatGUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window= this.getContentPane();
window.setLayout(null);
inputSlider= new JSlider(JSlider.HORIZONTAL,0,100,50);
inputSlider.setSize(new Dimension(250,20));
inputSlider.setLocation(25, 20);
inputSlider.addChangeListener(this);
window.add(inputSlider);
inputLabel= new JLabel("摄氏度:");
inputLabel.setSize(new Dimension(100,40));
inputLabel.setHorizontalAlignment(SwingConstants.CENTER);
inputLabel.setLocation(25, 50);
window.add(inputLabel);
inputField= new JTextField(Integer.toString(50));
inputField.setSize(new Dimension(100,30));
inputField.setHorizontalAlignment(SwingConstants.CENTER);
inputField.setLocation(150, 50);
inputField.getDocument().addDocumentListener(this);
window.add(inputField);
inputField.getDocument().addDocumentListener(this);
int lowTemp=(int)sheshiToHuashi(0);
int highTemp=(int)sheshiToHuashi(100)+1;
outputSlider= new JSlider(JSlider.HORIZONTAL,lowTemp,highTemp,(lowTemp+highTemp)/2);
outputSlider.setSize(new Dimension(250,20));
outputSlider.setLocation(25, 150);
outputSlider.setEnabled(false);
window.add(outputSlider);
outputLabel= new JLabel("华氏度:");
outputLabel.setSize(new Dimension(100,40));
outputLabel.setHorizontalAlignment(SwingConstants.CENTER);
outputLabel.setLocation(25, 180);
window.add(outputLabel);
outputField= new JTextField(Integer.toString((lowTemp+highTemp)/2));
outputField.setSize(new Dimension(100,30));
outputField.setHorizontalAlignment(SwingConstants.CENTER);
outputField.setLocation(150, 180);
outputField.setEditable(false);
window.add(outputField);
}
private double sheshiToHuashi(double sheshiTem)
{
return (sheshiTem*9)/5+32;
}
//实现ActionListener接口
public void actionPerformed(ActionEvent event)
{
}
//实现ChangeListener接口
public void stateChanged(ChangeEvent event)
{
int intOfSlider=inputSlider.getValue();
inputField.setText(Integer.toString(intOfSlider));
double tempOfTemp=this.sheshiToHuashi(intOfSlider);
outputSlider.setValue((int)tempOfTemp);
String tempOfTempString=String.format("%.2f", tempOfTemp);
outputField.setText(tempOfTempString);
}
//实现DocumentListener接口
public void changedUpdate(DocumentEvent e)
{
}
public void removeUpdate(DocumentEvent e)
{
inputSlider.setValue(0);
}
public void insertUpdate(DocumentEvent e)
{
double douOfField=Double.parseDouble(inputField.getText());
inputSlider.setValue((int)douOfField);
}
//主函数
public static void main(String [] args)
{
Welcome frame= new Welcome();
frame.setSize(300,300);
frame.creatGUI();
frame.setVisible(true);
}
}
哪位有时间的话帮我运行下这个程序,这个程序的主要问题是,inputTextField在添加DocumentListener监听之后无法正常输入文本,比如原来想输入52,但是输入之后却变成25;另外有其他什么问题还望大家可以不吝赐教下。