?? frame3.java
字號:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Frame3 extends JFrame {
Socket socket;
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField loginname = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField password = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
DataOutputStream outstream;
DataInputStream instream;
String str;
int cout;
String get,send;
byte[] buf=new byte[2555];
public Frame3() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(230, 200));
setTitle("登錄窗口");
jLabel1.setText("姓名");
jLabel1.setBounds(new Rectangle(23, 7, 124, 21));
loginname.setText("");
loginname.setBounds(new Rectangle(21, 32, 177, 22));
jLabel2.setText("密碼");
jLabel2.setBounds(new Rectangle(21, 56, 175, 21));
password.setText("");
password.setBounds(new Rectangle(20, 81, 181, 30));
jButton1.setBounds(new Rectangle(18, 122, 69, 23));
jButton1.setText("登陸");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setBounds(new Rectangle(138, 124, 69, 23));
jButton2.setText("取消");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
contentPane.add(jLabel1);
contentPane.add(loginname);
contentPane.add(jLabel2);
contentPane.add(password);
contentPane.add(jButton1);
contentPane.add(jButton2);
setVisible(true);
}
public void jButton1_actionPerformed(ActionEvent e) {
try
{
socket=new Socket("localhost",9999);
}
catch(IOException a)
{
System.out.println(a);
}
Client client=new Client();
client.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
public static void main(String args[])
{new Frame3();}
class Client extends JFrame implements Runnable
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextArea chat = new JTextArea();
JButton sendmessage = new JButton();
JLabel jLabel3 = new JLabel();
TextArea message = new TextArea();
TextArea people = new TextArea();
JTextArea jTextArea4 = new JTextArea();
Thread thread;
public Client()
{
addWindowListener(
new WindowAdapter()
{
public void Stop()
{
try{
outstream.close();
instream.close();
socket.close();}
catch(IOException e)
{
System.out.println(e);
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);}
}
);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(420, 330));
setTitle("Frame Title");
jLabel1.setToolTipText("");
jLabel1.setText(" 歡迎使用!");
jLabel1.setBounds(new Rectangle(166, 12, 79, 26));
jLabel2.setText("上線人:");
jLabel2.setBounds(new Rectangle(281, 8, 106, 30));
chat.setBounds(new Rectangle(127, 247, 172, 28));
sendmessage.setBounds(new Rectangle(315, 247, 78, 31));
sendmessage.setText("發送");
sendmessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Sendmessage();
}
});
jLabel3.setText(" 消息:");
jLabel3.setBounds(new Rectangle(76, 245, 39, 34));
message.setBounds(new Rectangle(16, 49, 241, 171));
people.setBounds(new Rectangle(273, 48, 108, 169));
jTextArea4.setBounds(new Rectangle(19, 13, 135, 23));
contentPane.add(jLabel1);
contentPane.add(chat);
contentPane.add(sendmessage);
contentPane.add(jLabel3);
contentPane.add(message);
contentPane.add(people);
contentPane.add(jTextArea4);
contentPane.add(jLabel2);
setVisible(true);
jTextArea4.setText(loginname.getText());
try
{
outstream=new DataOutputStream(socket.getOutputStream());
str=loginname.getText();
outstream.writeUTF(str+" 歡迎光臨");
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Sendmessage()
{
try
{
//socket=new Socket("localhost",9999);
outstream=new DataOutputStream(socket.getOutputStream());
instream=new DataInputStream(socket.getInputStream());
send=chat.getText();
outstream.writeUTF(str+"說:>"+send);
chat.setText("");
message.setText("");
get=instream.readUTF();
if(get.substring(get.length()-6).equals(" 歡迎光臨"))
{
people.append(get);
}
else
{
message.append(get);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
public void run()
{
}
void start()
{
thread=new Thread(this);
thread.start();
}
}
}
/*class Messagebox extends JFrame {
JPanel notePane;
JLabel notice = new JLabel();
public Messagebox(String s)
{
notePane = (JPanel) getContentPane();
notePane.setLayout(null);
setSize(new Dimension(350, 200));
setTitle("提示");
notice.setFont(new java.awt.Font("宋體-方正超大字符集", Font.PLAIN, 35));
notice.setText("請注意:"+s);
notice.setBounds(new Rectangle(9, 10, 350, 154));
notePane.setBackground(new Color(255, 224, 203));
notePane.add(notice);
setVisible(true);
new Client();
}
}*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -