当前位置: 技术问答>java相关
JAVA事件问题
来源: 互联网 发布时间:2017-04-12
本文导语: 先建立了一个JTextField,一个JButton. 当我按下BUTTON时,想查出TextField中有无文字,怎实现? | Button.addActionListener usage: JTextField.getText(); | import java.awt.*; import javax.swing.*; import java.awt.event.*; public cla...
先建立了一个JTextField,一个JButton.
当我按下BUTTON时,想查出TextField中有无文字,怎实现?
当我按下BUTTON时,想查出TextField中有无文字,怎实现?
|
Button.addActionListener
usage:
JTextField.getText();
usage:
JTextField.getText();
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Check extends JApplet {
JTextField text = new JTextField(20);
JTextField result = new JTextField(10);
JButton check = new JButton("Check");
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(text);
check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (text.getText() == null || text.getText().equals("")) {
result.setText("empty");
} else {
result.setText("not empty");
}
}
}
cp.add(check);
cp.add(result);
}
}
import javax.swing.*;
import java.awt.event.*;
public class Check extends JApplet {
JTextField text = new JTextField(20);
JTextField result = new JTextField(10);
JButton check = new JButton("Check");
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(text);
check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (text.getText() == null || text.getText().equals("")) {
result.setText("empty");
} else {
result.setText("not empty");
}
}
}
cp.add(check);
cp.add(result);
}
}