免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2544 | 回复: 5
打印 上一主题 下一主题

JAVA新手关于下拉框的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-27 17:58 |只看该作者 |倒序浏览
图片是我实现的一个聊天界面但是 我的下拉选框是没有用的 我想问下怎样才能下拉表情选框的时候 选择表情 发送后 聊天界面可以显示选择的表情  

import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class chat extends JFrame implements ActionListener{
       
        Container container;
       
        JButton send;//发送按钮
        JButton canel;//取消按钮
       
        JTextArea chatContent;//聊天内容显示窗口
        JTextField sentence;//聊天信 息栏
       
       
        JMenuBar menuBar ;
        JMenu operate ;
            JMenuItem option ;
        JMenu help;
            JMenu info;
                JMenuItem infomation;
                JMenuItem about;
       
        JDialog helpDia;
        JLabel helpLab;
        /**
         *
         */
        public chat(){
       
       
                menuBar = new JMenuBar();
                operate = new JMenu("operate";
                        option = new JMenuItem("quit";
                       
                operate.add(option);
                       
                        help = new JMenu("help";
                        info = new JMenu("info";
                                infomation = new JMenuItem("infomation";
                                info.add(infomation);
                        about = new JMenuItem("about";
                        help.add(info);
                        help.add(about);
                menuBar.add(operate);
                menuBar.add(help);
                this.setJMenuBar(menuBar);
               
                events();

               
                //创建表情下拉框
                Choice myChoice = new Choice();
                myChoice.add("";
                myChoice.addItem("^-^";
                myChoice.addItem("*_*";
                myChoice.addItem("T_T";
                myChoice.addItem("@_@");
                myChoice.addItem(">3<");
                myChoice.addItem("0_o");
                myChoice.addItem("- -!");
                myChoice.addItem("(>o<)");
                myChoice.addItem("囧");
                myChoice.addItem("+,-");
                myChoice.addItem("+_+");
                myChoice.addItem("o_0");
                myChoice.addItem("$_$");
                myChoice.addItem("*^_^*");
                myChoice.addItem("R-R");
                myChoice.addItem("囧");
                myChoice.addItem(")——(");
                myChoice.addItem("O>O");
                myChoice.addItem(".P");
                myChoice.insert("ZZZ~~~",2);
                myChoice.select(3);
               
                //设置背景颜色
                myChoice.setBackground(Color.blue);
                setVisible(true);
                validate();
               
                //创建下拉框
                 Choice choose = new Choice();
                 choose.add("购物逛街广场街");
                 choose.addItem("心灵花园聊天屋");
                 choose.addItem("游戏天地交流区");
                 choose.addItem("舞文弄墨小说区");
                 choose.addItem("无线山河区");
                 choose.addItem("水墨梁山区");
                 choose.addItem("心灵园地区");
                 choose.addItem("开心天地聊天区");
                 choose.addItem("跨国交友区");
                 choose.addItem("同城交友区");
                 choose.addItem("耽美美文天下区");
                 choose.insert("返回", ;
                 
                 //设置背景颜色和大小
                 choose.setBackground(Color.RED);
                 choose.setSize(100,50);
                 setVisible(true);
                 validate();
       
                //设置窗口标题
                super.setTitle("聊天窗口");
                //设置窗口大小
                this.setSize(600,400);
               
                //获得主窗口的内容面版
                container = this.getContentPane();
               
                //布局方式为BORDERLAYOUT
                container.setLayout(new BorderLayout());
               
                //创建存放聊天记录的滚动面板
                JScrollPane centerPanel = new JScrollPane();
               
                //放聊天记录的多行文本输入区
                chatContent = new JTextArea();
                //将文本放入滚动面板
                centerPanel.setViewportView(chatContent);
               
                //滚动面板放在文本的中央
                container.add(centerPanel,BorderLayout.CENTER);
               
                //文本区域不可编辑
                chatContent.setEditable(false);
               
               
                //创建底部面板对象,存放聊天标签、聊天栏、发送按钮 取消按钮
                JPanel bottomPanel = new JPanel();
                bottomPanel.setBackground(Color.gray);

                //创建顶部面板
                JPanel topPanel = new JPanel();
                topPanel.setBackground(Color.PINK);
                 
                //创建中间面板
                JPanel pl = new JPanel();
                pl.setLayout(new FlowLayout(FlowLayout.LEFT));
                pl.setBackground(Color.orange);
               
                //中间面板加入下拉框,顶部面板加入新建标签
                pl.add(choose);
               
                topPanel.add(new JLabel("欢迎来到开心天地聊天室!"));
               
                                       
                sentence = new JTextField(20);
                send = new JButton("发送");
                canel= new JButton("取消");
                bottomPanel.add(new JLabel("聊天消息"));
               
                bottomPanel.add(myChoice);
               
                //聊天文本输入框,发送按钮,添加到底部面板
                bottomPanel.add(sentence);
                bottomPanel.add(send);
                bottomPanel.add(canel);
               
               
                //底部面板,顶部面板,中间面板放在主窗口的底部                              
                container.add(bottomPanel,BorderLayout.SOUTH);
                container.add(topPanel,BorderLayout.NORTH);
                container.add(pl,BorderLayout.WEST);
               
                //初始化菜单栏需要用到的对话框和标签
                                helpLab = new JLabel();
                                helpLab.setHorizontalAlignment(SwingConstants.CENTER);
                                helpDia = new JDialog(this,true);
                                helpDia.setBounds(380, 250, 400, 110);
                                helpDia.add(helpLab);
               
                //注册发送按钮,聊天栏的动作事件
                send.addActionListener(this);
                sentence.addActionListener(this);
                canel.addActionListener(this);
               
                //显示主窗口
                this.setVisible(true);
               
                //关闭窗口即是退出程序
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
       

        public void actionPerformed(ActionEvent e){
               
                //获取聊天信息
                String str =sentence.getText();

                        if (str != null && ! str.equals(""))
                                chatContent.append("本人:" + str + "\n");                       
                        else
                                chatContent.append("聊天信息不能为空!\n");
               
                //清空聊天栏里的信息
                sentence.setText("");
        }

       
        public void events(){
                //菜单栏事件--退出
                                option.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                System.exit(0);
                                        }
                                });
                //菜单栏事件--信息
                                infomation.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                helpDia.setTitle("Infomation");
                                                helpLab.setText("this is the infomation you may want...");
                                                helpDia.setVisible(true);
                                        }
                                });
        }
       
        public static void main(String[] arg){
                new chat();
               
        }
}

未命名.jpg (36.64 KB, 下载次数: 18)

未命名.jpg

论坛徽章:
0
2 [报告]
发表于 2012-12-27 17:59 来自手机 |只看该作者
哈哈能否

论坛徽章:
0
3 [报告]
发表于 2012-12-27 18:00 来自手机 |只看该作者
环境法

论坛徽章:
0
4 [报告]
发表于 2012-12-27 18:25 |只看该作者
环境法??

论坛徽章:
19
CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-09-18 15:15:15CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大牛徽章
日期:2013-04-17 11:19:32CU大牛徽章
日期:2013-04-17 11:19:28
5 [报告]
发表于 2013-01-13 14:54 |只看该作者
回复 1# 不喜欢花的花猪


    操作系统是Linux,鉴定完毕,其它的内容稍等……

论坛徽章:
19
CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-09-18 15:15:15CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大牛徽章
日期:2013-04-17 11:19:32CU大牛徽章
日期:2013-04-17 11:19:28
6 [报告]
发表于 2013-01-13 14:59 |只看该作者
本帖最后由 方兆国 于 2013-01-13 15:05 编辑

回复 1# 不喜欢花的花猪


    大概原理是要让你的聊天界面类实用ActionListener接口,即在类名后面加上implements ActionListener,然后他会自动给你增加一个public void actionPerformed(ActionEvent e) 函数,然后你把相应的动作加上去就可以了。
貌似你目前的那个程序只能说是画了一幅画儿,逗你玩呢,别生气!


很抱歉,你贴的代码略多,刚才没有看清楚。
首先,不建议使用类中类;
其次,你应该对Choice choose 增加addActionListener(this);我不确定能不能给它加这个触发器,感觉可以的
最后,你应该在actionPerformed(ActionEvent e)加入if判断语句,判断如果触发源是choose的话,把choose选中的图片放置在聊天区域
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP