首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

事件监听有有关问题了

2013-01-28 
事件监听有问题了import java.awt.BorderLayoutpublic class mainform_2 extends JFrame implements Acti

事件监听有问题了
import java.awt.BorderLayout;


public class mainform_2 extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 2908232839539938626L;

private JPanel contentPane;

 JButton  btnNewButton_1; 

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainform_2 frame = new mainform_2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
 * Create the frame.
 */
public mainform_2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JButton btnNewButton_1 = new JButton("test1");
contentPane.add(btnNewButton_1);
btnNewButton_1.addActionListener(this);
}
    public void actionPerformed(ActionEvent e)
    {
    //String app=e.getActionCommand();
    if(e.getSource()==btnNewButton_1)
    
    {JOptionPane.showMessageDialog(null,"aa");}
    }
    
}
在点击按钮输出“aa”,不加IF语句可以输出,但加了这个判断却没有任何反应,不知为什么,求高手。
但在网上说这种写法是可以的啊。》》》》》》》
[解决办法]
JButton btnNewButton_1 = new JButton("test1");

这是你在方法内设的局部变量,

在开始的地方有 JButton  btnNewButton_1; 
这两个不一样啊。。。。

应该把JButton btnNewButton_1 = new JButton("test1");改为btnNewButton_1 = new JButton("test1");


要学会调试。。

热点排行