📄 clientframe.java
字号:
package homework;
import javax.swing.*;
import java.net.Socket;
import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.*;
/**
* <p>Title: java source file filter</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author Jesse
* @version 1.0
*/
public class ClientFrame extends JFrame {
JTextField textField;
JTextArea textArea;
JButton button;
Socket soc;
BufferedReader reader;
PrintWriter writer;
String IP;
public ClientFrame() {
super("Client");
textField = new JTextField(20);
button = new JButton(new sendAction());
textArea = new JTextArea(10,30);
this.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300, 300));
JPanel cp = (JPanel) getContentPane();
cp.add(textField);
cp.add(button);
cp.add(scrollPane);
IP=JOptionPane.showInputDialog(this,"Input host IP","127.0.0.1");
pack();
this.setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
soc = new Socket("127.0.0.1", 888);
reader = new BufferedReader(new InputStreamReader(soc.
getInputStream()));
writer = new PrintWriter(soc.getOutputStream());
JOptionPane.showConfirmDialog(this,
"Link to the server successfully!!",
"Log in", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
JOptionPane.showConfirmDialog(this, "Can not link to the server");
}
}
class sendAction extends AbstractAction {
sendAction() {
super("Send");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
String t = textField.getText();
writer.println(t);
writer.flush();
try {
t = reader.readLine();
textArea.append(t + "\n");
} catch (Exception f) {
System.out.println(f);
}
}
}
public static void main(String[] args) {
ClientFrame clientframe = new ClientFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -