自学Java帮忙写个最简单的按钮监听:要求点击按钮后在文本框内显示"你好"
展开全部
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Test89 {
public static void main(String args[]) {
//Jframe
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
final TextField text = new TextField(30);
Button btn = new Button("点击");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.setText("你好");;
}
});
f.setTitle("例子");
f.setLayout(new FlowLayout());
f.add(text);
f.add(btn);
f.setBackground(Color.WHITE);
f.setSize(280, 100);
f.setLocationRelativeTo(f);//position center.
f.setVisible(true);//show.
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询