| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 795 人关注过本帖
标题:监听器的问题~~麻烦大家纠正一下
只看楼主 加入收藏
bo5205203
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-25
收藏
 问题点数:0 回复次数:0 
监听器的问题~~麻烦大家纠正一下
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import *;
import java.util.*;
import *;


public class Chatlice implements ActionListener
{
    
    //指出需要那些对象
    JFrame frm;
    JPanel p;
    static JTextArea txtTag;//聊天记录文本域
    JTextArea txtContent;
    JLabel lbl;
    JButton btnsend,btnclose,btnconn;
    JList list,list1;
    JPanel p2;
    JPanel pmain;
    JPanel pButtom;
   
   
    Socket sktClient;//从客户访问连接服务,需要一个Socket类的对象
    ObjectOutputStream oop;//把数据发送到服务器。。需要一个ObjectOutputStream的对象
    ObjectInputStream ois;//
 
       BorderLayout borderlyt;//布局管理器(边界类型)
    
    //在类的构造函数那以上的对象构造出来
    public Chatlice()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);//界面变换
        frm=new JFrame("聊天室克服端");
        pmain=new JPanel();
        pmain.setLayout(new BorderLayout());
        JMenuBar mb=new JMenuBar();
        JMenu m=new JMenu("文件传送(F)");
        JMenuItem open=new JMenuItem("打开");
    //    JMenuItem mit=new JMenuItem("传送图片");
        m.add(open);
        m.addSeparator();
    //    m.add(mit);
        mb.add(m);
        frm.setJMenuBar(mb);
        
        open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
    //    mit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
        
        txtTag=new JTextArea(15,30);
        txtTag.setEditable(false);//设置聊天记录文本域不可编辑
        txtContent=new JTextArea(5,5);
        btnsend=new JButton("发送");
        btnclose=new JButton("关闭");
        btnconn=new JButton("连接");
        ImageIcon img=new ImageIcon("gx.jpg");
        lbl=new JLabel(img);
      //  txtQgg.setEditable(false);
        
        String[] strname={"老大","老二","小三","小四","小五","小六                                ","七妹"};
        list=new JList(strname);
    
    
        frm.getContentPane().add(pmain);
        
        //组建
        p=new JPanel();
        p.setLayout(new BorderLayout());//对面版p进行边界布局
        p.add(txtTag,"Center");
        p.add(txtContent,"South");
       
    
       
        
        pButtom=new JPanel();
        pButtom.setLayout(new FlowLayout(FlowLayout.RIGHT));//对面板pButtom进行流布局
        pButtom.add(btnsend);
        pButtom.add(btnclose);
        pButtom.add(btnconn);
        
        p.add(pButtom,"South");
        pmain.add(p,"Center");
        
        p2=new JPanel();
        p2.setLayout(new BorderLayout());//对面板p2边界布局
        p2.add(list,"South");
        p2.add(lbl,"North");
        pmain.add(p2,"East");
       
       // pButtom.setColor(new Color(95,142,214));
      //  p2.setColor(new Color(95,142,214));
        //pmain.setBackground(Color.black);
       
       
        btnclose.addActionListener(this);//实现关闭按钮
        btnsend.addActionListener(this);
        btnconn.addActionListener(this);
        open.addActionListener(this);
       
        //为面版加上边框
        Font font=new Font("Dialog",Font.PLAIN,13);
        Border b1=BorderFactory.createEtchedBorder();
        Border c1=BorderFactory.createTitledBorder(b1,"聊天内容",1,2,font,Color.black);
        Border c2=BorderFactory.createTitledBorder(b1,"个性形象",1,2,font,Color.blue);
        Border c3=BorderFactory.createTitledBorder(b1,"在线人员",1,2,font,Color.blue);
        Border c4=BorderFactory.createTitledBorder(b1,"编辑信息",1,2,font,Color.black);
        txtContent.setBorder(c4);
        list.setBorder(c3);
        p2.setBorder(b1);
        lbl.setBorder(c2);
        txtTag.setBorder(c1);
        
    
        //为文本加上滚动条
        Border b4=BorderFactory.createLoweredBevelBorder();
        JScrollPane sp3=new JScrollPane(txtTag,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        p.add("North",sp3);
        
        Border b=BorderFactory.createLoweredBevelBorder();
        txtContent.setForeground(Color.blue);
        JScrollPane sp1=new JScrollPane(txtContent,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        p.add("Center",sp1);
        
        Border b3=BorderFactory.createLoweredBevelBorder();
        JScrollPane sp2=new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
         p2.add("South",sp2);
         
         p2.setBackground(Color.white);
         
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.pack();                 //窗口自动调整适合的大小
    //    frm.setSize(400,400);
        frm.setLocationRelativeTo(null);
        frm.setResizable(false); //设置主窗口不能改变大小
        frm.setVisible(true);//实现界面能见
        
           
        
    
    }
    
// 构造监听器
    
    public void actionPerformed(ActionEvent e)
    {
        JButton bt=(JButton)e.getSource();//对按钮监听
        
        
        if(bt==btnsend)
           {
            ChatMsg cm=new ChatMsg();
            cm.who="_-+sHaDow.*`";
            cm.content=txtContent.getText();
            
            try
            {
                oop.writeObject(cm);
            }
            catch(Exception ex)
            {
                System.out.println(ex.toString());
            }
        }
        else if(bt==btnclose)
        {
            System.exit(0);
            
        }    
        else if(bt==btnconn)
        {
            try
            {
            //建立客户与服务器的连接
            sktClient=new Socket("127.0.0.1",2008);
            System.out.println("sdasdfsdfsdf");
            
            oop=new ObjectOutputStream(sktClient.getOutputStream());
            //ObjectInputStream abc=new ObjectInputStream(sktClient.getInputStream());
            System.o
搜索更多相关主题的帖子: import java JPanel awt 监听 
2008-06-28 20:02
快速回复:监听器的问题~~麻烦大家纠正一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017149 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved