构造器
JButton() 创建一个空按钮
JButton(Sting text ) 创建一个按钮,按钮带有文本标签“text”,即给按钮命名text
JButton(Icon icon ) 创建一个按钮,按钮带有图标
方法
setEnable
public void setEnable(boolean b) 启用或禁用按钮,参数b传递true时,启用按钮,参数b传递false时,禁用按钮
addActionListener
public void addActionListener(ActionListener l) 添加监听事件,默认使用actionPerformed方法
例如:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StopThreadTest.this.startButton.setEnabled(false);
StopThreadTest.this.stopButton.setEnabled(true);
startThread();
}
};
本文介绍了Swing中JButton的构造方法及如何通过setEnable和addActionListener方法控制按钮的状态和添加监听事件。通过示例代码展示了如何设置按钮启用状态及响应点击事件。
1715

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



