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

按钮事件有关问题(事件为什么不能触发?)

2012-02-11 
按钮事件问题(事件为什么不能触发?)事件为什么不能触发?button1,button2,不就是两个事件的事件源吗?为什么

按钮事件问题(事件为什么不能触发?)
事件为什么不能触发?
button1,button2,不就是两个事件的事件源吗?为什么事件不能被触发呀?
System.out.println(e.getSource()==button1);
System.out.println(e.getSource()==button2);
两句打出来的怎么都是false呀?(这两个都是false一定触发不了,可他们怎么会是false呢?  
e.getSource()获得的不是对象button1,button2吗?要是的话怎么不相等呢?
代码如下:


import   java.io.*;
import   java.net.*;
import   java.awt.*;
import   javax.swing.*;
import   java.awt.event.*;
import   javax.swing.Box;

public   class   test   extends   JFrame   implements   ActionListener
{
JFrame   frame;
JPanel   contentpane,panel2;
JTextArea     textarea;
JScrollPane   panel3;
JButton   button1;
JButton   button2;
JLabel   message;
JLabel   kehu;
JTextField     textField;
JComboBox   box1
test()
{
JFrame   frame=new   JFrame( "服务器端 ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(450,300);

JPanel   contentpane=new   JPanel();
JPanel   panel2=new   JPanel();


JTextArea   textarea=new   JTextArea(15,10);
JScrollPane   panel3=new   JScrollPane(textarea);

JButton   button1=new   JButton( "接受连接 ");
button1.addActionListener(this);
/*button1.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
System.out.println( "button1 ");
}
});*/

JButton   button2=new   JButton( "发送 ");
button2.addActionListener(this);
/*button2.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
System.out.println( "button2 ");
}
});*/
JTextField   textField=new   JTextField(20);
JComboBox   box1=new   JComboBox();

JLabel   message=new   JLabel( "消息   : ");
JLabel   kehu=new   JLabel( "客户端   : ");

Toolkit   kit   =   Toolkit.getDefaultToolkit();
Image   image   =   kit.getImage( "Ghosthuo.jpg ");
frame.setIconImage(image);


        Box   hbox1   =   Box.createHorizontalBox();
        hbox1.add(button1);

        //   添加一个20像素的支柱
        hbox1.add(Box.createHorizontalStrut(20));
        hbox1.add(message);
        hbox1.add(Box.createHorizontalStrut(20));
        hbox1.add(textField);    

      Box   hbox2   =   Box.createHorizontalBox();
      hbox2.add(Box.createHorizontalStrut(105));        
      hbox2.add(kehu);
        //   添加一个20像素的支柱
        hbox2.add(Box.createHorizontalStrut(8));
        hbox2.add(box1);
        hbox2.add(Box.createHorizontalStrut(20));
        hbox2.add(button2);        
       
        Box   vbox   =   Box.createVerticalBox();
        vbox.add(hbox1);
        vbox.add(hbox2);


frame.setContentPane(contentpane);
contentpane.setLayout(new   BorderLayout());
contentpane.add(vbox,BorderLayout.NORTH);
contentpane.add(panel3,BorderLayout.CENTER);

frame.setSize(450,300);
frame.setVisible(true);
frame.pack();
}

public   void   actionPerformed(ActionEvent   e)
{
System.out.println(e.getSource()==button1);
System.out.println(e.getSource()==button2);

if(button1   ==   e.getSource())
{button1.setEnabled(false);}

else   if(button2==(JButton)e.getSource())
{     System.out.println( "button2 ");}

}

public   static   void   main(String[]   args)  
{
new   test();
}

}


[解决办法]
两个不同的对象,因为你在构造方法中对button1和button2进行了重新定义,而不是作为你这个类的成员变量,JButton button1=new JButton( "接受连接 ");JButton button2=new JButton( "发送 ");

如果在构造函数中改为:button1=new JButton( "接受连接 ");button2=new JButton( "发送 ");就可以了。


[解决办法]
两个不同的对象,因为你在构造方法中对button1和button2进行了重新定义,而不是作为你这个类的成员变量,JButton button1=new JButton( "接受连接 ");JButton button2=new JButton( "发送 ");
==================================================
就是这样啊。完全两个东西,你的button1,button2还都没实例化呢。实例化的是另外的两个

[解决办法]
真不是一般晕死,你actionPerformed()里面根本没有取事件。例如:button1的事件要if(e.getCommand().equals( "接受连接 ")){//所要采取的操作},楼主还是要多看看基本的东西啊

热点排行
Bad Request.