📄 jchat.java.bak
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.net.*;import java.io.*;public class JChat extends JFrame implements ActionListener, KeyListener{ private JEditorPane txtBroadcast = new JEditorPane(); // to be set on the JScrollPane private JTextArea txtSend = new JTextArea(); private JButton btnSend = new JButton("SEND"); private JScrollPane scrlConversation, scrlSend, scrlList; // appear in right part private ChatListener listener; // used to detect the private messenger private JLabel lbConversation = new JLabel("Conversation Record"); private JPanel rightUpperPane = new JPanel(); // used to include the lbConversation & scrlConversation private BoxLayout rightUpperBox = new BoxLayout(rightUpperPane, BoxLayout.Y_AXIS); private JLabel lbSend = new JLabel("Message to Send"); private JPanel rightLowerPane = new JPanel(); private JPanel sendPanel = new JPanel(); private BoxLayout rightLowerBox = new BoxLayout(rightLowerPane, BoxLayout.Y_AXIS); private BoxLayout sendBox = new BoxLayout(sendPanel, BoxLayout.X_AXIS); private JSplitPane splitV; private JSplitPane splitH; private JList lstUser; // appear in left part private DefaultListModel lstModel = new DefaultListModel(); // javax.swing.DefaultListModel --> including many function about the list // 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 bgcolor=\"#FFFFFF\" text=\"#000000\" " + "leftmargin=\"5\" topmargin=\"5\" marginwidth=\"5\" marginheight=\"5\">"; private String msgEnding = "</body></html>"; private String msg = ""; public JChat(ChatListener l) { super("Messenger"); listener = l; // Configuration setup of the right upper part (conversation panel) txtBroadcast.setEditable(false); // Not allowed to edit the broadcast text area txtBroadcast.setContentType("text/html"); // using html format txtBroadcast.setText("<html><p style=\"font-family:verdana\">Welcome</p></html>"); scrlConversation = new JScrollPane(txtBroadcast); // set the JEditorPane in scrlConversation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrlConversation.setPreferredSize(new Dimension(400,300)); scrlConversation.setMinimumSize(new Dimension(10,10)); rightUpperPane.setLayout(rightUpperBox); rightUpperPane.add(lbConversation); rightUpperPane.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)); btnSend.addActionListener(this); // implements ActionListener btnSave.setPreferredSize(new Dimension(70,30)); btnSave.setMinimumSize(new Dimension(10,10)); btnSave.addActionListener(this); btnPane.setLayout(btnBox); btnPane.add(btnSend); btnPane.add(btnSave); sendPanel.setLayout(sendBox); sendPanel.add(scrlSend); sendPanel.add(btnPane); rightLowerPane.setLayout(rightLowerBox); rightLowerPane.add(lbSend); // JLabel rightLowerPane.add(sendPanel); splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, rightUpperPane, rightLowerPane); splitV.setOneTouchExpandable(false); splitV.setDividerLocation(300); splitV.setPreferredSize(new Dimension(400,400)); lstUser = new JList(lstModel); lstUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrlList = new JScrollPane(lstUser); scrlList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrlList.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrlList.setPreferredSize(new Dimension(150,400)); scrlList.setMinimumSize(new Dimension(10,10)); splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrlList, splitV); splitH.setOneTouchExpandable(true); splitH.setDividerLocation(150); splitH.setPreferredSize(new Dimension(550,400)); setContentPane(splitH); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent me) { if (me.getClickCount() == 2) { int index = lstUser.locationToIndex(me.getPoint()); listener.onInitiatePrivateMessage((String)lstModel.get(index)); } } }; txtSend.addKeyListener(this); lstUser.addMouseListener(mouseListener); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); pack(); setSize(550,500); //setVisible(true); } public void keyTyped(KeyEvent k){} public void keyReleased(KeyEvent k){} public void keyPressed(KeyEvent k) { if (k.getKeyCode() == KeyEvent.VK_ENTER) { listener.onSendMessageToAll(txtSend.getText().replaceAll("\n"," ")); txtSend.setText(""); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == btnSend) { listener.onSendMessageToAll(txtSend.getText().replaceAll("\n"," ")); // Broadcasting txtSend.setText(""); } else if (e.getSource() == btnSave) { if (txtBroadcast.getText().length() > 100) { new JMessageSaveChat(this, txtBroadcast); } else JOptionPane.showMessageDialog(null, "No Conversation!", "Save Error", JOptionPane.ERROR_MESSAGE); */ } } public void showChat() // in the beginning after logging in { lstModel.clear(); msg=""; txtBroadcast.setText(msgHeader + msgEnding); txtSend.setText(""); setVisible(true); // make the chatting frame visible } private void addMessage(String message) { msg += message; txtBroadcast.setText(msgHeader + msg + msgEnding); txtBroadcast.selectAll(); txtBroadcast.setCaretPosition(txtBroadcast.getSelectedText().length()); } public void revMessage(String from, String message) { addMessage("<b><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000066\">" + from + ": </font></b>" + "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\" color=\"#006699\">" + message + "</font><br>"); } public void userJoin(String name) { lstModel.addElement(name); addMessage("<b><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\" color=\"#990000\">" + "User " + name + " has joined !</font></b><br>"); } public void userLeft(String name) { for (int i=0; i<lstModel.getSize(); i++) { if (((String)lstModel.get(i)).equalsIgnoreCase(name)) { lstModel.remove(i); break; } } addMessage("<b><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\" color=\"#990000\">" + "User " + name + " has left !</font></b><br>"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -