如何将控件与事件处理代码相联(50分)

  • 主题发起人 主题发起人 WollyXF
  • 开始时间 开始时间
W

WollyXF

Unregistered / Unconfirmed
GUEST, unregistred user!
//下面的代码执行后,按下btnWolly没有反应,大家能告诉我错在哪里?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class C905{
private static C905 theApp;
private static JFrame frm;
public static void main(String[] args){
theApp = new C905();
theApp.init();
}
public void init(){
frm = new JFrame("C905");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit kit = frm.getToolkit();
Dimension wndSize = kit.getScreenSize();
frm.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);
CButtonW btn = new CButtonW("btnWolly");
Container ctn = frm.getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
ctn.setLayout(flow);
ctn.add(btn);
frm.setVisible(true);
}
class CButtonW extends JButton implements ActionListener{
private int ClickTimes = 0;
CButtonW(String Caption){
setText(Caption);
}
public void actionPerformed(ActionEvent e){
frm.getToolkit().beep();
frm.setTitle("I was Clicked "+ ++ClickTimes);
}
}
}
 
光定义不添加ActionListener有P用?
class CButtonW extends JButton implements ActionListener{
private int ClickTimes = 0;
CButtonW(String Caption){
setText(Caption);
[red]addActionListener(this);[/red]
}
public void actionPerformed(ActionEvent e){
frm.getToolkit().beep();
frm.setTitle("I was Clicked "+ ++ClickTimes);
}
}
 
接受答案了.
 
后退
顶部