?? chaty.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ChatY
{
static JFrame jf=new JFrame("聊天中Y");
static final JTextField jtf=new JTextField(30);
static JButton jb=new JButton("發送");
static JTextArea jta=new JTextArea(10,30);
public static void main(String[] args) throws Exception
{
setFrame();
chat();
}
public static void setFrame()
{
jf.getContentPane().setLayout(new FlowLayout());
jf.setSize(500,300);
jf.setLocation(100,100);
jf.getContentPane().add(jtf);
jf.getContentPane().add(jb);
jf.getContentPane().add(jta);
jf.show();
jf.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void chat() throws Exception
{
Socket s=new Socket("Localhost",6000);
final InputStream is=s.getInputStream();
final OutputStream os=s.getOutputStream();
byte[] buf=new byte[100];
int len=is.read(buf);
String strFromX=new String(buf,0,len);
jta.setText(strFromX);
//System.out.println(new String(buf,0,len));
jb.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String strChat=jtf.getText();
try{
os.write(strChat.getBytes());
}catch(IOException ex)
{
ex.printStackTrace();
}
try{
byte[] buf=new byte[100];
int len=is.read(buf);
String strFromX=new String(buf,0,len);
jta.setText(strFromX);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -