我正在尝试制作一个按钮来计算文本字段中的字符 [英] I'm trying to make a button to count characters in a text field
问题描述
我正在尝试使用 JButton count
来计算输入到 JTextField t
中的字符数.我是 Java 和 GUI 的新手,但这是我的代码:
I'm trying to use the JButton count
to count the number of characters entered into the JTextField t
. I'm new to Java and GUIs but here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends Frame implements ActionListener{
TextField t;
Button count;
int a;
Choice choice;
public GUI1(){
this.t = new TextField("", 30);
this.count = new Button("count");
this.count.addActionListener(this);
JTextField x = new JTextField();
x.setEditable(false);
this.setTitle("Character count");
this.setLayout(new FlowLayout());
this.add(x);
this.add(t);
this.add(count);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()== this.count)
t.setText(choice.getSelectedItem()+ " " +a);
}
我还试图在另一个不可编辑的 JTextField x
中输入值.任何帮助表示赞赏.
I'm also trying to enter the value in another uneditable JTextField x
. Any help is appreciated.
推荐答案
首先,我建议你不要使用 AWT
元素,因为它带来了很多问题,而且几乎没有支持,相反,您可以尝试使用 Swing
组件,它们是 AWT 的替代/修复.您可以在此处阅读更多信息.您可能还想阅读AWT vs Swing(优点和缺点).
First of all, I recommend you not to use AWT
elements, since it brings tons of problems and it has little to no support, instead you can try using Swing
components which are a replacement/fix for AWT. You can read more about here. You might also want to read AWT vs Swing (Pros and Cons).
现在进入你的问题:
您应该避免从 JFrame
扩展,我建议您改为创建一个新的 JFrame
对象.原因如下.话虽如此,您还可以使用 this
删除所有 this.t
和其他调用.
You should avoid extending from JFrame
, I might recommend you to create a new JFrame
object instead. Here's the reason to why. That being said, you can also remove all your this.t
and other calls with this
.
很高兴您使用了布局管理器!
I'm glad you're using a Layout Manager!
现在要计算 JTextField
上的字符数并将文本设置为其他 JTextField
,您应该使用以下代码:
And now to count the number of characters on your JTextField
and set text to your other JTextField
you should use this code:
count.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = t.getText().length();
System.out.println(count);
x.setText(t.getText());
}
});
我还修复了您的代码,我将 AWT 元素更改为 Swing 元素,并将列数添加到您的第二个 JTextField
以便它出现.
Also I fixed your code, I changed AWT elements to Swing ones and added number of cols to your second JTextField
so it would appear.
所以,这是我根据您的代码制作的运行示例(并删除了 Choice choice
行,因为您没有发布该代码):
So, here's a running example that I made from your code (And removed Choice choice
line since you didn't posted that code):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 {
JTextField t;
JButton count;
int a;
JFrame frame;
public GUI1(){
frame = new JFrame();
t = new JTextField("", 15);
count = new JButton("count");
JTextField x = new JTextField("", 15);
x.setEditable(false);
count.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = t.getText().length();
System.out.println(count);
x.setText(t.getText());
}
});
frame.setTitle("Character count");
frame.setLayout(new FlowLayout());
frame.add(x);
frame.add(t);
frame.add(count);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main (String args[]) {
new GUI1();
}
}
这篇关于我正在尝试制作一个按钮来计算文本字段中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!