import java.awt.*;import javax.swing.*;import java.awt.event.*;
@SuppressWarnings("serial")
public class AlarmClock extends JFrame{
public static void main(String args[]){
//if((e.getActionCommand()).equals("闹铃时间")){
TextField tfHour;
TextField tfMinute;
TextField tfSecond;
Button btSure;
tfHour=new TextField(1);
tfMinute=new TextField(1);
tfSecond=new TextField(1);
JFrame ff=new JFrame("闹铃时间");
ff.setSize(250,100);
ff.setVisible(true);
final Panel p1=new Panel();
final Panel p2=new Panel();
p1.add(new Label("闹铃时间"));
btSure=new Button("确定");
//btStop=new Button("关");
p2.add(btSure);
//p2.add(btStop);
p1.add(tfHour);
p1.add(new Label(":"));
p1.add(tfMinute);
p1.add(new Label(":"));
p1.add(tfSecond);
ff.add(p1);
ff.add(p2,BorderLayout.SOUTH);
}}
以上代码,如何给按钮"确定"注册监听器,请指教,谢谢。
@SuppressWarnings("serial")
public class AlarmClock extends JFrame{
public static void main(String args[]){
//if((e.getActionCommand()).equals("闹铃时间")){
TextField tfHour;
TextField tfMinute;
TextField tfSecond;
Button btSure;
tfHour=new TextField(1);
tfMinute=new TextField(1);
tfSecond=new TextField(1);
JFrame ff=new JFrame("闹铃时间");
ff.setSize(250,100);
ff.setVisible(true);
final Panel p1=new Panel();
final Panel p2=new Panel();
p1.add(new Label("闹铃时间"));
btSure=new Button("确定");
//btStop=new Button("关");
p2.add(btSure);
//p2.add(btStop);
p1.add(tfHour);
p1.add(new Label(":"));
p1.add(tfMinute);
p1.add(new Label(":"));
p1.add(tfSecond);
ff.add(p1);
ff.add(p2,BorderLayout.SOUTH);
}}
以上代码,如何给按钮"确定"注册监听器,请指教,谢谢。
btSure.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// 这里写处理的代码好了,兄弟
}});
if(e.getSurse().equals(btSure){
//添加想要实现的动作;
}
}});