不喜欢花的花猪 发表于 2012-12-27 17:58

JAVA新手关于下拉框的使用

图片是我实现的一个聊天界面但是 我的下拉选框是没有用的 我想问下怎样才能下拉表情选框的时候 选择表情 发送后 聊天界面可以显示选择的表情

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.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("返回", 8);
               
               //设置背景颜色和大小
               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();
               
        }
}

无聊大少 发表于 2012-12-27 17:59

哈哈能否:kiss::shutup::dizzy:

无聊大少 发表于 2012-12-27 18:00

环境法:Q:dizzy::kiss:

不喜欢花的花猪 发表于 2012-12-27 18:25

环境法??

方兆国 发表于 2013-01-13 14:54

回复 1# 不喜欢花的花猪


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

方兆国 发表于 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选中的图片放置在聊天区域
页: [1]
查看完整版本: JAVA新手关于下拉框的使用