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

📄 jprivatemessage.java

📁 an instant messenger written in java (client)
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.net.*;import java.io.*;public class JPrivateMessage extends JFrame implements ActionListener, KeyListener{    public String localname;    public String remotename;        private JEditorPane txtConversation = new JEditorPane();    // to be set on the JScrollPane    private JTextArea txtSend = new JTextArea();    private JButton btnSend = new JButton("SEND");    private JScrollPane scrlConversation, scrlSend;      private PrivateMessageListener listener;  // used to detect the private messenger        private JLabel lbConversation = new JLabel("Conversation Record");    private JPanel upperPane = new JPanel();   // used to include the lbConversation & scrlConversation    private BoxLayout upperBox = new BoxLayout(upperPane, BoxLayout.Y_AXIS);        private JLabel lbSend = new JLabel("Message to Send");    private JPanel sendPanel = new JPanel();    private JPanel lowerPane = new JPanel();    private BoxLayout sendBox = new BoxLayout(sendPanel, BoxLayout.X_AXIS);    private BoxLayout lowerBox = new BoxLayout(lowerPane, BoxLayout.Y_AXIS);    private JSplitPane splitV;        // Components of save message function    private JButton btnSave = new JButton("SAVE");    private JPanel btnPane = new JPanel();    private BoxLayout btnBox = new BoxLayout(btnPane, BoxLayout.Y_AXIS);        // Prepare the script    private String msgHeader ="<html><body>";    private String msgEnding = "</body></html>";    private String msg = "";       public JPrivateMessage(PrivateMessageListener l, String localUser, String remoteUser)    {        super("Private Messenger with "+remoteUser);                listener = l;           localname = localUser;        remotename = remoteUser;                // Configuration setup of the right upper part (conversation panel)        txtConversation.setEditable(false);    // Not allowed to edit the broadcast text area        txtConversation.setContentType("text/html");   // using html format        txtConversation.setText("<html><p style=\"font-family:verdana\">Welcome</p></html>");                scrlConversation = new JScrollPane(txtConversation);   // set the JEditorPane in        scrlConversation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        scrlConversation.setPreferredSize(new Dimension(400,300));        scrlConversation.setMinimumSize(new Dimension(10,10));                upperPane.setLayout(upperBox);        upperPane.add(lbConversation);        upperPane.add(scrlConversation);                // Configuration setup of the right lower part (text input area)        scrlSend = new JScrollPane(txtSend);   // set the TextArea in        scrlSend.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        scrlSend.setPreferredSize(new Dimension(330,30));        scrlSend.setMinimumSize(new Dimension(10,10));        btnSend.setPreferredSize(new Dimension(70,30));        btnSend.setMinimumSize(new Dimension(10,10));        btnSave.setPreferredSize(new Dimension(70,30));        btnSave.setMinimumSize(new Dimension(10,10));        btnSend.addActionListener(this);   // implements ActionListener 	btnSave.addActionListener(this);   // to be implemented 	txtSend.addKeyListener(this);                btnPane.setLayout(btnBox);        btnPane.add(btnSend);        btnPane.add(btnSave);                sendPanel.setLayout(sendBox);        sendPanel.add(scrlSend);        sendPanel.add(btnPane);                lowerPane.setLayout(lowerBox);        lowerPane.add(lbSend);     // JLabel        lowerPane.add(sendPanel);                   splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPane, lowerPane);        splitV.setOneTouchExpandable(false);        splitV.setDividerLocation(300);        splitV.setPreferredSize(new Dimension(400,400));                setContentPane(splitV);               pack();                    this.addWindowListener(new WindowAdapter()        {           public void windowClosing(WindowEvent e)           {           	    listener.onClosePrivateMessage(remotename);           }        });                setVisible(true);             }    public void keyTyped(KeyEvent k){}    public void keyReleased(KeyEvent k){}    public void keyPressed(KeyEvent k)    {    	if (k.getKeyCode() == KeyEvent.VK_ENTER)        {            if (txtSend.getText().length() > 0)  // message cannot be sent without anything              {            	String message = txtSend.getText().replaceAll("\n","");            	listener.onSendMessage(remotename, message);             	if (message != "\n")            	  addMessage("<p><b>" + localname + ": </b>" + message + "</p>");            	txtSend.setText("");   // clear text area            }        }    }        public void actionPerformed(ActionEvent e)    {        if (e.getSource() == btnSend)        {                      if (txtSend.getText().length() > 0)   // message cannot be sent without anything             {	            	String message = txtSend.getText().replaceAll("\n","");            	listener.onSendMessage(remotename, message);  // Broadcasting              	if (message != "\n")            	  addMessage("<p><b>" + localname + ": </b>" + message + "</p>");  // have problem?            	txtSend.setText("");            }        }        else if (e.getSource() == btnSave)        {        	// testing        	//int len = txtConversation.getText().length();        	//JOptionPane.showMessageDialog(null, len, "ER", JOptionPane.ERROR_MESSAGE);                        if (txtConversation.getText().length() > 92)            {        	new JMessageSave(this, txtConversation);	            }            else JOptionPane.showMessageDialog(null, "No Conversation!", "Save Error", JOptionPane.ERROR_MESSAGE);                }    }           private void addMessage(String message)    {        msg += message;        txtConversation.setText(msgHeader + msg + msgEnding);        txtConversation.selectAll();        txtConversation.setCaretPosition(txtConversation.getSelectedText().length());    }    public void revMessage(String message)    {        addMessage("<p><b>" + remotename + ": </b>" + message + "</p>");    }    public void userLeft()    {                addMessage("<p><b>" + remotename + " has disconnected</b></p>");        btnSend.setEnabled(false);    }  }

⌨️ 快捷键说明

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