⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgwindow.java

📁 局域网聊天工具,学习java桌面应用程序的好例子,(代传)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    .addComponent(guiTips))                .addContainerGap(12, Short.MAX_VALUE))        );        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);        jMenu1.setText("File");        jMenuBar1.add(jMenu1);        jMenu2.setText("Edit");        jMenuBar1.add(jMenu2);        setJMenuBar(jMenuBar1);        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)        );        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();        setBounds((screenSize.width-441)/2, (screenSize.height-485)/2, 441, 485);    }// </editor-fold>//GEN-END:initComponentsprivate void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing// TODO add your handling code here:    this.exit();}//GEN-LAST:event_formWindowClosingprivate void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed// TODO add your handling code here:    this.exit();}//GEN-LAST:event_jButton2ActionPerformedprivate void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed// TODO add your handling code here:    this.sendMsg();}//GEN-LAST:event_jButton1ActionPerformed    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JComboBox GUI_fontColor;    private javax.swing.JComboBox GUI_fontName;    private javax.swing.JComboBox GUI_fontSize;    private javax.swing.JComboBox GUI_fontStyle;    private javax.swing.JTextPane guiContent;    private javax.swing.JLabel guiTips;    private javax.swing.JTextArea guiWtsay;    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JButton jButton3;    private javax.swing.JMenu jMenu1;    private javax.swing.JMenu jMenu2;    private javax.swing.JMenuBar jMenuBar1;    private javax.swing.JPanel jPanel1;    private javax.swing.JPanel jPanel2;    private javax.swing.JPanel jPanel3;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JScrollPane jScrollPane2;    // End of variables declaration//GEN-END:variablesprivate void init(ArrayList<String> contacts){                initComponents();                ift=new InsertFormatText(this.guiContent);        //绑定按键        this.guiWtsay.addKeyListener(this);        this.guiContent.addKeyListener(this);        this.GUI_fontColor.addKeyListener(this);        this.GUI_fontName.addKeyListener(this);        this.GUI_fontSize.addKeyListener(this);        this.GUI_fontStyle.addKeyListener(this);                this.contacts=contacts;        if(contacts==null){            this.key="broadcast";            this.setTitle("广播");        }else if(contacts.size()>1){            this.key=null;            this.setTitle("群发 ( "+this.contacts.size()+" 个用户 )");        }else {            this.key=contacts.get(0);            //this.setTitle(GlobalVar.getUserName(this.key)+"( "+this.key+" )");            this.setTitle(GlobalVar.getUser(this.key).getName()                    +"( "+this.key+" )");        }        //注册        if(key!=null)GlobalVar.addWindow(key, this);        this.guiWtsay.requestFocus();    }    public void keyTyped(KeyEvent e) {    }    public void keyPressed(KeyEvent e) {    }    public void keyReleased(KeyEvent e) {        char c = e.getKeyChar();        int modifiers = e.getModifiers();         if ((GlobalVar.SEND_MSG_STYLE==GlobalConstant.CTRL_ENTER                &&modifiers==2 & c=='\n')||                (GlobalVar.SEND_MSG_STYLE==GlobalConstant.ENTER                &&modifiers==0 & c=='\n')) {          this.sendMsg();        }    }        private void sendMsg(){        String str=this.guiWtsay.getText().trim();        if(str.isEmpty()){            this.guiTips.setText("请输入消息内容!");            this.guiWtsay.requestFocus();            return;        }        //是否超过长度限制。        if(str.length()>GlobalConstant.MSG_LEN)        if(str.length()>1000-                GlobalVar.USER_NAME.length()-GlobalVar.HOST_NAME.length()){                        this.guiTips.setText("一次性发送字符太多!");            this.guiWtsay.requestFocus();            return;        }                            Command tmpCom = new Command(                GlobalConstant.IPMSG_SENDMSG|GlobalConstant.IPMSG_SENDCHECKOPT);        tmpCom.setAdditional(str);                  //如果是广播,则直接广播        if(this.key!=null&&this.key.equals("broadcast")){            UtilityNet.broadcastUdpPacket(tmpCom);        }        //如果是群,则使用点对点方式给群里面的每一位用户发送消息        else {            this.appendNewMsg(str, null);            for(int count=0;count<this.contacts.size();count++){                if(!GlobalVar.ALL_IP_ADDRESS.contains(this.contacts.get(count))){                    tmpCom.setIp(this.contacts.get(count));                    UtilityNet.sendUdpPacket(tmpCom);                }            }        }        //清空发送版内容        this.guiWtsay.setText("");        this.guiWtsay.requestFocus();            }        /**     * 在后面添加消息。     * @param msgContent 消息内容     * @param fromIp 来自。如果为空。则为自己自行添加。     */    public void appendNewMsg(String msgContent,String from){        //确定昵称        String name=(from==null)?"自己":GlobalVar.getUser(from).getName();        //插入标题        ift.insertFormatText(UtilityGlobal.getTime()+" "+name+GlobalVar.NEW_LINE                , "黑体", "粗体", 14, "蓝色");                //确定字体格式        String fonts = (String) this.GUI_fontName.getSelectedItem();        String style = (String) this.GUI_fontStyle.getSelectedItem();        int size = Integer.parseInt((String)                 this.GUI_fontSize.getSelectedItem());        String color = (String) this.GUI_fontColor.getSelectedItem();        //插入内容        ift.insertFormatText(msgContent+                GlobalVar.NEW_LINE+GlobalVar.NEW_LINE                , fonts, style, size, color);                JScrollBar jb = this.jScrollPane1.getVerticalScrollBar();        jb.setValue(jb.getMaximum());    }        private void exit(){        if(this.key!=null)GlobalVar.delWindow(key);        this.dispose();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -