如何更改JTextField的值? [英] How to change the value of the JTextField?
问题描述
我正在尝试使用Swing使用 JFrame
构建一个Java应用程序,我在那里有5个 JTextField
个实例。其中一个是Sum。
我需要 JTextField
在我在文本中输入一些数字时自动更改这是怎么回事?
我写的是这里。
private void displaytotalActionPerformed(java.awt.event.ActionEvent evt){
//显示总计:
Float num1,num2,num3 ,num4,num5,NUM6,结果;
num1 = display1b.getText()。equals()? 0:Float.parseFloat(display1b.getText());
num2 = display2b.getText()。equals()? 0:Float.parseFloat(display2b.getText());
num3 = display3b.getText()。equals()? 0:Float.parseFloat(display3b.getText());
num4 = display4b.getText()。equals()? 0:Float.parseFloat(display4b.getText());
num5 = display5b.getText()。equals()? 0:Float.parseFloat(display5b.getText());
num6 = display6b.getText()。equals()? 0:Float.parseFloat(display6b.getText());
result = num1 + num2 + num3 + num4 + num5 + num6;
System.out.println(result);
}
我试图获得总和并使用按钮在此文本字段中显示它工作。但我希望它能自动完成。但上面的代码在文本字段上没有显示任何内容。
我对此很新,如果你善意的话,我很感激。
我认为你在寻找
-
setText( )
JTextField
的方法。 - 写一个 DocumentListener 知道文本何时更改并对其应用总和
- 写一个 Docu mentFilter 确保您的程序不会因输入与数字不同而崩溃。
另一种方法是使用 JFormattedTextField 并使用监听器。
一些建议。
- 开发直觉,通常属于java类的属性由
(get | set)属性
。使用像Netbeans这样的IDE,它可以帮助你找到东西。 - 自动化通常是通过使用一个监听器来实现的,只需要知道何时使用它(这也是直觉的一部分)。
- 当您发现自己编写重复代码时,请考虑使用函数。即
而不是6次:
display1b.getText()。equals()? 0:Float.parseFloat(display1b.getText());
考虑将您的字段放在 ArrayList
中编写一个迭代它们的函数,并使用上面的一行来设置所有值。
- 按照 JavaTutorials 在进行额外的硬编码工作之前,或者在这里询问。对你来说会更有成效。因为您将学习如何自学并深入学习文档。
I am trying to built a Java application using JFrame
with Swing, and I have 5 JTextField
instances in there. One of these is for Sum.
I need JTextField
to change automatically as soon as I type some number in text fields.
How is that possible?
What I wrote is here.
private void displaytotalActionPerformed(java.awt.event.ActionEvent evt) {
// display total:
Float num1,num2,num3,num4,num5,num6,result;
num1 = display1b.getText().equals("") ? 0 : Float.parseFloat(display1b.getText());
num2 = display2b.getText().equals("") ? 0 : Float.parseFloat(display2b.getText());
num3 = display3b.getText().equals("") ? 0 : Float.parseFloat(display3b.getText());
num4 = display4b.getText().equals("") ? 0 : Float.parseFloat(display4b.getText());
num5 = display5b.getText().equals("") ? 0 : Float.parseFloat(display5b.getText());
num6 = display6b.getText().equals("") ? 0 : Float.parseFloat(display6b.getText());
result = num1+num2+num3+num4+num5+num6;
System.out.println(result);
}
I tried to get the sum and display it in this textfield using button and it worked. But i want it to be done automatically. But the code above doesnt display anything on textfield.
I am quite new to this and I appreciate if you kindly guide.
I think you're looking for
- the
setText()
method of aJTextField
. - Writing a DocumentListener to know when the text changes and apply sum to it
- Writing a DocumentFilter to make sure your program does not crash with input different to numbers.
An alternative to this is to use a JFormattedTextField and using listeners.
A couple of recommendations.
- Develop intuition, usually properties to java classes are changed by
(get|set)Property
. Use an IDE like Netbeans and it will help you finding stuff. - Automatization is usually achieved by using a Listener, just learn when to use which (this is also part of intuition).
- When you find yourself writing repeated code, consider using a function. i.e.
Instead of having 6 times:
display1b.getText().equals("") ? 0 : Float.parseFloat(display1b.getText());
Consider having your fields in an ArrayList
and write a function that iterates them and have a single line of the above to set all the values.
- Follow the JavaTutorials before additional efforts of hard-coding, or asking here. Will be more productive to you. Because you will learn how to learn for yourself and dig into documentation.
这篇关于如何更改JTextField的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!