请问Java中使用ButtonListener的错误,代码内容如下,在eclipse中编译不过。
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTestActionEven...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent extends JFrame {
//Create to buttons
private JButton jbtOk = new JButton ("OK");
private JButton jbtCancel = new JButton ("Cancel");
public TestActionEvent (){
//Set the window title
setTitle ("TestActionEvent");
//Set FlowLayout manger to arrange the components
//inside the frame
getContentPane ().setLayout (new FlowLayout ());
//Add button to the frame
getContentPane ().add(jbtOk);
getContentPane ().add(jbtCancel);
//Create a listener object
ButtonListener btListener = new ButtonListener ();
//Register listeners
jbtOk.addActionListener(btListener);
jbtCancel.addActionListener(btListener);
}
/**Main method**/
public static void main (String [] args){
TestActionEvent frame = new TestActionEvent ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize (100, 80);
frame.setVisible(true);
}
} 展开
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent extends JFrame {
//Create to buttons
private JButton jbtOk = new JButton ("OK");
private JButton jbtCancel = new JButton ("Cancel");
public TestActionEvent (){
//Set the window title
setTitle ("TestActionEvent");
//Set FlowLayout manger to arrange the components
//inside the frame
getContentPane ().setLayout (new FlowLayout ());
//Add button to the frame
getContentPane ().add(jbtOk);
getContentPane ().add(jbtCancel);
//Create a listener object
ButtonListener btListener = new ButtonListener ();
//Register listeners
jbtOk.addActionListener(btListener);
jbtCancel.addActionListener(btListener);
}
/**Main method**/
public static void main (String [] args){
TestActionEvent frame = new TestActionEvent ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize (100, 80);
frame.setVisible(true);
}
} 展开
展开全部
ButtonListener btListener = new ButtonListener ();
ButtonListener 不是核心类,需要你自己写一下,放在源代码里面,做内部类就行
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}
}
或者就像楼上说的方法,直接使用匿名内部类的代码方式
jdtOK.addActionListener ( new ActionListener() {
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}});
ButtonListener 不是核心类,需要你自己写一下,放在源代码里面,做内部类就行
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}
}
或者就像楼上说的方法,直接使用匿名内部类的代码方式
jdtOK.addActionListener ( new ActionListener() {
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}});
展开全部
你是想实现button的事件绑定么?
jdk中没有ButtonListener这个类。
如果要实现按钮绑定的话用
jdtOK.addActionListenernew ActionListener(){
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}
});
这种方法
jdk中没有ButtonListener这个类。
如果要实现按钮绑定的话用
jdtOK.addActionListenernew ActionListener(){
public void actionPerformed(ActionEvent e) {
//你要执行的任务
}
});
这种方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package first1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button extends JFrame {
//Create to buttons
private JButton jbtOk = new JButton ("OK");
private JButton jbtCancel = new JButton ("Cancel");
public Button (){
//Set the window title
setTitle ("TestActionEvent");
//Set FlowLayout manger to arrange the components
//inside the frame
getContentPane ().setLayout (new FlowLayout ());
//Add button to the frame
getContentPane ().add(jbtOk);
getContentPane ().add(jbtCancel);
//Create a listener object
// ButtonListener btListener = new ButtonListener();
//Register listeners
jbtOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
}
});
jbtCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
}
});
}
/**Main method**/
public static void main (String [] args){
Button frame = new Button ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize (100, 80);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button extends JFrame {
//Create to buttons
private JButton jbtOk = new JButton ("OK");
private JButton jbtCancel = new JButton ("Cancel");
public Button (){
//Set the window title
setTitle ("TestActionEvent");
//Set FlowLayout manger to arrange the components
//inside the frame
getContentPane ().setLayout (new FlowLayout ());
//Add button to the frame
getContentPane ().add(jbtOk);
getContentPane ().add(jbtCancel);
//Create a listener object
// ButtonListener btListener = new ButtonListener();
//Register listeners
jbtOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
}
});
jbtCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
}
});
}
/**Main method**/
public static void main (String [] args){
Button frame = new Button ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize (100, 80);
frame.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询