按钮事件问题(事件为什么不能触发?)
事件为什么不能触发?
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);