📄 chattingroomframe.java~47~
字号:
package chattingroom;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import chattingroom.ClientChat;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class chattingRoomFrame extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jsendInfo = new JTextField();
TitledBorder titledBorder1;
JLabel jLabel2 = new JLabel();
JTextField jport = new JTextField();
JLabel jLabel3 = new JLabel();
JButton jButton3 = new JButton();
ClientChat clientchat;
JTextField jServerIP = new JTextField();
TextArea ResponseList = new TextArea();
Label label1 = new Label();
JLabel jLabel4 = new JLabel();
//Construct the frame
public chattingRoomFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
jLabel1.setForeground(UIManager.getColor("Menu.selectionBackground"));
jLabel1.setText("聊天室客户端程序");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(446, 429));
this.setTitle("聊天室客户端程序");
jLabel1.setText(" 聊天室客户端程序");
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setForeground(Color.blue);
jButton1.setText("发言");
jButton1.addActionListener(new chattingRoomFrame_jButton1_actionAdapter(this));
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setForeground(Color.blue);
jButton2.setText("退出");
jButton2.addActionListener(new chattingRoomFrame_jButton2_actionAdapter(this));
jsendInfo.setBackground(new Color(202, 230, 250));
jsendInfo.setFont(new java.awt.Font("Dialog", 0, 14));
jsendInfo.setForeground(Color.black);
jsendInfo.setBorder(BorderFactory.createLineBorder(Color.black));
jsendInfo.setText("");
jsendInfo.addFocusListener(new chattingRoomFrame_jsendInfo_focusAdapter(this));
contentPane.setBackground(SystemColor.control);
jLabel2.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel2.setForeground(SystemColor.windowBorder);
jLabel2.setRequestFocusEnabled(true);
jLabel2.setText("指定服务器主机名或IP:");
jport.setBackground(new Color(202, 230, 250));
jport.setFont(new java.awt.Font("Dialog", 0, 14));
jport.setText("8162");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 14));
jLabel3.setForeground(SystemColor.windowBorder);
jLabel3.setText("指定服务器端口号:");
jButton3.setText("连接服务器 ");
jButton3.addActionListener(new chattingRoomFrame_jButton3_actionAdapter(this));
jButton3.setForeground(Color.blue);
jButton3.setFont(new java.awt.Font("Dialog", 0, 14));
jServerIP.setBackground(new Color(202, 230, 250));
jServerIP.setFont(new java.awt.Font("Dialog", 0, 14));
jServerIP.setText("LocalHost");
ResponseList.setBackground(new Color(202, 230, 250));
ResponseList.setEditable(false);
ResponseList.setFont(new java.awt.Font("Dialog", 0, 14));
ResponseList.setForeground(Color.red);
ResponseList.setText("服务器返回记录:");
label1.setFont(new java.awt.Font("Dialog", 0, 14));
label1.setForeground(Color.red);
label1.setLocale(java.util.Locale.getDefault());
label1.setText("(从0~65536)");
jLabel4.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel4.setForeground(Color.blue);
jLabel4.setIconTextGap(4);
jLabel4.setText("请发言:");
contentPane.add(jLabel1, new XYConstraints(127, 10, 165, 17));
contentPane.add(jButton2, new XYConstraints(296, 363, -1, -1));
contentPane.add(jButton3, new XYConstraints(70, 363, -1, -1));
contentPane.add(jButton1, new XYConstraints(206, 363, -1, -1));
contentPane.add(jLabel3, new XYConstraints(37, 327, -1, -1));
contentPane.add(jLabel2, new XYConstraints(37, 289, -1, -1));
contentPane.add(ResponseList, new XYConstraints(64, 75, 367, 181));
contentPane.add(jsendInfo, new XYConstraints(65, 41, 366, 36));
contentPane.add(jLabel4, new XYConstraints(7, 44, 65, 28));
contentPane.add(jport, new XYConstraints(188, 321, 53, -1));
contentPane.add(jServerIP, new XYConstraints(187, 289, 112, -1));
contentPane.add(label1, new XYConstraints(242, 325, 87, -1));
}
//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 jButton2_actionPerformed(ActionEvent e) {
//退出程序
int result=JOptionPane.showConfirmDialog(null,"你真的要退出吗?","确认信息",0,2);
if (result==0)
{
clientchat.close();//退出前先关闭连接
System.exit(0);
}
}
void jButton3_actionPerformed(ActionEvent e) {
//连接服务器
String serverIP=jServerIP.getText();
int port=Integer.parseInt(jport.getText());
clientchat=new ClientChat(serverIP,port);
if (clientchat.connected)//如果连接成功,则将此按扭设为不可用
{jButton3.setEnabled(false);}
else
{jButton3.setEnabled(true);}
ResponseList.append("\n"+clientchat.getResponse()+"\n");//将收到的信息加到文本域中
if(ResponseList.size()==ResponseList.getSize())//如果文本域已写满,则全部清空
ResponseList.setText("");
}
void jButton1_actionPerformed(ActionEvent e) {
//收发消息
if (clientchat.connected)
{
clientchat.sendRequest(jsendInfo.getText());
ResponseList.append(clientchat.getResponse()+"\n");//将收到的信息加到文本域中
}
}
void jsendInfo_focusGained(FocusEvent e) {
jsendInfo.setText("");
}
}
class chattingRoomFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
chattingRoomFrame adaptee;
chattingRoomFrame_jButton2_actionAdapter(chattingRoomFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class chattingRoomFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
chattingRoomFrame adaptee;
chattingRoomFrame_jButton3_actionAdapter(chattingRoomFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class chattingRoomFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
chattingRoomFrame adaptee;
chattingRoomFrame_jButton1_actionAdapter(chattingRoomFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class chattingRoomFrame_jsendInfo_focusAdapter extends java.awt.event.FocusAdapter {
chattingRoomFrame adaptee;
chattingRoomFrame_jsendInfo_focusAdapter(chattingRoomFrame adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent e) {
adaptee.jsendInfo_focusGained(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -