当前位置: 代码迷 >> Java相关 >> 晕,真的不知我这个程序可不可以,代码如下
  详细解决方案

晕,真的不知我这个程序可不可以,代码如下

热度:143   发布时间:2007-03-26 10:31:10.0
晕,真的不知我这个程序可不可以,代码如下

import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class MyClient extends JFrame{
JButton jButton1=new JButton("sent");
JTextArea jTextArea1=new JTextArea(5,10);
JTextArea jTextArea2=new JTextArea(3,10);
JLabel jLabel1=new JLabel("聊天内容");
JLabel jLabel2=new JLabel("你将发送的内容");
JMenuBar jMenuBar1=new JMenuBar();
JMenu jMenu1=new JMenu("文件");
JMenuItem jMenuItem1_1=new JMenuItem("编辑");
JMenuItem jMenuItem1_2=new JMenuItem("复制");
JMenuItem jMenuItem1_3=new JMenuItem("粘贴");
FlowLayout flowLayout1=new FlowLayout();
JMenu jMenu2=new JMenu("打开");
JMenu jMenu3=new JMenu("关闭");
JMenu jMenu4=new JMenu("退出");
Socket cSocket=null;
BufferedReader reader=null;
PrintStream writer=null;
String receiveMsg;
MyClient(){
super("客户聊天窗口");
setSize(700,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(flowLayout1);
setJMenuBar(jMenuBar1);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1_1);
jMenu1.add(jMenuItem1_2);
jMenu1.add(jMenuItem1_3);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenuBar1.add(jMenu4);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jTextArea1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jTextArea2);
this.getContentPane().add(jButton1);
jButton1.addActionListener(this);
jMenuItem1_1.addActionListener(this);
jMenuItem1_2.addActionListener(this);
jMenuItem1_3.addActionListener(this);
jMenu2.addActionListener(this);
jMenu3.addActionListener(this);
jMenu4.addActionListener(this);
setResizable(true);
setVisible(true);
connect();
}
public void actionPerFormed(ActionEvent e){
if(e.getSource()==jButton1){
writer.println(jTextArea2.getText());
writer.flush();
}
if(e.getSource()==jMenu4){
dispose();
System.exit(0);
}
}
public void connect(){
try{
cSocket=new Socket("localhost",6000);
reader=new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
writer=new PrintStream(cSocket.getOutputStream());
receiveMsg=reader.readLine();
jTextArea1.append(receiveMsg+"\n");
}
catch(Exception e){}
}
public void main(String args){
MyClient mc=new MyClient();
}
}
有些监听我觉得没必要就不写了,请你们看看可不可以,这样写!运行时出现了监听的错误!

搜索更多相关的解决方案: 代码  

----------------解决方案--------------------------------------------------------

没看到你有implements ActionListener
还有actionPerFormed
f是小写的.


----------------解决方案--------------------------------------------------------
  谢谢,好了~~~真是大意,因为改了一下监听!
----------------解决方案--------------------------------------------------------
   还有那个static又忘了!
----------------解决方案--------------------------------------------------------
   还有一点想知道,是不是菜单上不的键不能ActionListener去响应,只能用ActionCommand
----------------解决方案--------------------------------------------------------
以下是引用117ai在2007-3-26 11:11:04的发言:
还有一点想知道,是不是菜单上不的键不能ActionListener去响应,只能用ActionCommand

什么个意思?
----------------解决方案--------------------------------------------------------
  就是我那个程序用ActionListenter去监听,比如那个退出,它响应不了~
----------------解决方案--------------------------------------------------------
谁说响应不了,照样可以响应
----------------解决方案--------------------------------------------------------