?? chatclientframe.java
字號:
package simplechatclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.net.*;/** * Title: 聊天小程序 * Description: 這是一個使用SOCKET制作的網絡聊天程序 * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */public class ChatClientFrame extends JFrame { JPanel contentPane; JLabel statusBar = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanelHost = new JPanel(); JLabel jLabel1 = new JLabel(); JTextField txtHostAdd = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField txtSerPort = new JTextField(); JButton cmdCon = new JButton(); JPanel jPanelMain = new JPanel(); GridLayout gridLayout1 = new GridLayout(); JPanel jPanelInput = new JPanel(); BorderLayout borderLayout2 = new BorderLayout(); JScrollPane jScrollPaneInput = new JScrollPane(); TitledBorder titledBorder1; JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JTextArea txtInput = new JTextArea(); JPanel jPanelSend = new JPanel(); JButton cmdSend = new JButton(); JTextArea txtMsg = new JTextArea(); JScrollPane jScrollPaneMsg = new JScrollPane(); ChatSocket currencyChat; Border border1; TitledBorder titledBorder2; /**Construct the frame*/ public ChatClientFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(ChatFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"需要發送的信息"); border1 = BorderFactory.createEmptyBorder(); titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"日志"); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(421, 399)); this.setTitle("網絡聊天客戶端程序"); statusBar.setText(" "); jLabel1.setText("請輸入服務器地址:"); txtHostAdd.setColumns(10); jLabel2.setToolTipText(""); jLabel2.setText("端口:"); txtSerPort.setColumns(5); cmdCon.setText("Connect"); cmdCon.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cmdCon_actionPerformed(e); } }); jPanelMain.setLayout(gridLayout1); gridLayout1.setRows(2); gridLayout1.setColumns(1); gridLayout1.setHgap(1); jPanelInput.setLayout(borderLayout2); jScrollPaneInput.setBorder(titledBorder1); jLabel3.setText(" "); jLabel4.setText(" "); cmdSend.setText("發送"); cmdSend.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cmdSend_actionPerformed(e); } }); jScrollPaneMsg.setBorder(titledBorder2); txtMsg.setEditable(false); contentPane.add(statusBar, BorderLayout.SOUTH); contentPane.add(jPanelHost, BorderLayout.NORTH); jPanelHost.add(jLabel1, null); jPanelHost.add(txtHostAdd, null); jPanelHost.add(jLabel2, null); jPanelHost.add(txtSerPort, null); jPanelHost.add(cmdCon, null); contentPane.add(jPanelMain, BorderLayout.CENTER); jPanelMain.add(jPanelInput, null); jPanelInput.add(jScrollPaneInput, BorderLayout.CENTER); jPanelInput.add(jLabel3, BorderLayout.WEST); jPanelInput.add(jLabel4, BorderLayout.EAST); jPanelInput.add(jPanelSend, BorderLayout.SOUTH); jScrollPaneInput.getViewport().add(txtInput, null); jPanelSend.add(cmdSend, null); jPanelMain.add(jScrollPaneMsg, null); jScrollPaneMsg.getViewport().add(txtMsg,null); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void cmdCon_actionPerformed(ActionEvent e) { if(cmdCon.getText().equals("Connect")){ if(currencyChat!=null) { currencyChat.disConnect(); currencyChat=null; } int port=Integer.parseInt(txtSerPort.getText().trim()); currencyChat=new ChatSocket(txtHostAdd.getText().trim(),port); currencyChat.setOutTextComponent(txtMsg); if(currencyChat.connect()){ statusBar.setText("連接服務器"+txtHostAdd.getText().trim()+":"+port+"成功!") ; } else{ statusBar.setText("連接服務器"+txtHostAdd.getText().trim()+":"+port+"成功!") ; } txtMsg.setText(txtMsg.getText()+statusBar.getText()+"\n"); cmdCon.setText("Disconnect"); }else{ if(currencyChat!=null) { currencyChat.disConnect(); currencyChat=null; } statusBar.setText("關閉連接") ; txtMsg.setText(txtMsg.getText()+"關閉連接\n"); cmdCon.setText("Connect"); } } void cmdSend_actionPerformed(ActionEvent e) { if(currencyChat!=null) { synchronized(this){ String result=(currencyChat.send(txtInput.getText().trim())?"成功":"失敗"); txtMsg.setText(txtMsg.getText()+"發送 :"+txtInput.getText().trim()+"\n"+result+"\n"); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -