如何解决如何在 JTextField 中使用条件语句
我是一名高中级别的初级程序员,我对我的一些代码有疑问。我想检索用户在 JTextField 中输入的内容,并在条件语句中使用它来执行某个操作。每当我在字段中输入内容时,什么也没有发生。我不确定我哪里出错了,我真的很感激任何帮助。这是我的代码:
import java.awt.event.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
public JTextField t;
public text()
{
// create a new frame to store text field and button
JFrame f = new JFrame("textfield");
// create a label to display text
JLabel l = new JLabel("nothing entered");
// create a new button
JButton b = new JButton("submit");
b.addActionListener(this);
// create a object of the text class
//text te = new text();
// addActionListener to button
//b.addActionListener(te);
// create a object of JTextField with 16 columns and a given initial text
final JTextField t = new JTextField("",16);
// create a panel to add buttons and textfield
JPanel p = new JPanel();
JTextField xa = new JTextField("",16);
xa.setText("A");
//String s = t.getText();
//char chars = s.charAt(0);
String text=t.getText();
if (text.equals("A")) {
System.out.println("Hello");
}
// add buttons and textfield to panel
p.add(t);
p.add(b);
p.add(l);
// add panel to frame
f.add(p);
// set the size of frame
f.setSize(300,300);
f.show();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。