监听器删除当前页面的窗口
TestFrame.this.dispose();
当前页面class .this .dispose()
监听3种实现
package chuangkou;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import lovo.Mytest;
public class TestFrame extends JFrame implements ActionListener {
private MyTest nameText=new MyTest("姓名",70,50,this);
public TestFrame(){
this.setLayout(null);
MyButton b=new MyButton("确定", 50, 50, this);
/*
b.addActionListener(new MyAction());*/
// b.addActionListener(this);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String info=nameText.getText();
System.out.println(info);
}
});
this.setSize(500,400);
//可见度
this.setVisible(true);
//关闭
this.setDefaultCloseOperation(3);
//居中
this.setLocationRelativeTo(null);
}
public static void main(String[] args) {
TestFrame t=new TestFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("*********************---");
}
}
//第二种的class
package chuangkou;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("********");
}
}
本文介绍了一个使用Java Swing创建窗体并实现按钮监听功能的例子。通过具体代码展示了如何设置窗体属性、添加按钮及文本输入框,并为按钮配置了三种不同的监听器实现方式。
1万+

被折叠的 条评论
为什么被折叠?



