经过一个星期的摸索,学习了很多东西,从一开始的JTextField到,JTextare,再到JTextPane;再从jPane到JScrollPane ……,功能是越来越强大,不过其也是各自擅长,并不能一概而论。学无止境!!!慢慢摸索成长吧,实践才是硬道理。
以上就是个人心得体会,并无指导意义。废话不多说
正文**
加粗样式
**
窗口server
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.text.JTextComponent;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ServerWindow {
public static void main(String[] args) {
JFrame win=new JFrame("Tencent QQ");
win.setLayout(null);
win.setBounds(200, 100, 1100, 800);//窗口的大小定义
String name=JOptionPane.showInputDialog("请输入您的名字");
win.setVisible(true);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//输入的面板
JTextPane panel=new JTextPane();
JScrollPane scrollPane=new JScrollPane(panel);
scrollPane.setBounds(0, 0, 1100, 400);
win.add(scrollPane);
//输出的面板
JPanel panel2=new JPanel();
panel2.setLayout(null);
panel2.setBounds(0, 400, 1100, 400);
panel2.setBackground(Color.red);
JTextField a2=new JTextField(60);
a2.setBounds(300, 200, 500, 50);
panel2.add(a2);
//按钮“发送”
JButton Song=new JButton("发送");
panel2.add(Song);
win.add(panel2);
Server b=new Server(a2,name,panel);
}
}
窗口Client
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.Color;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
public class UserWindow {
public static void main(String[] args) {
JFrame win=new JFrame("QQ");
win.setLayout(null);
win.setBounds(200, 100, 1100, 800);//窗口的大小定义
String name=JOptionPane.showInputDialog("请输入您的名字");
win.setVisible(true);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//输入的面板
JTextPane panel=new JTextPane();
JScrollPane scrollPane=new JScrollPane(panel);
scrollPane.setBounds(0, 0, 1100, 400);
win.add(scrollPane);
//输出的面板
JPanel panel2=new JPanel();
panel2.setLayout(null);
panel2.setBounds(0, 400, 1100, 400);
panel2.setBackground(Color.red);
JTextField a2=new JTextField(60);
a2.setBounds(300, 200, 500, 50);
panel2.add(a2);
//按钮“发送”
JButton Song=new JButton("发送");
panel2.add(Song);
win.add(panel2);
Client b=new Client(a2,name,panel);
}
}
后端server(运行时真正作用代码)
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
public class Server {
ServerSocket server;
Socket socket;
JTextField a2;//发送信息文本框
JTextArea area;//接受文本框
String name=null;
int getout;
JTextPane jPanel;
StyledDocument doc;
MessageBox p;
public Server(JTextField a2,String name,JTextPane jPanel) {
this.a2=a2;
this.name=name;
this.jPanel=jPanel;
doc=jPanel.getStyledDocument();
MessageBox p=new MessageBox(doc);
try {
server=new ServerSocket(5858);
socket = server.accept();
In ta=new In(socket,area,p);
Out te=new Out(socket,a2,p);
new Thread(ta).start();
new Thread(te).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
后端Client(运行时真正起作用的代码)
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
public class Client {
Socket socket;
JTextField a2;
JTextArea area;
private String name=null;
private JTextPane jPane;
StyledDocument doc;
MessageBox p;
public Client(JTextField a2,String name,JTextPane jPanel) {
this.a2=a2;
this.name=name;
doc=jPanel.getStyledDocument();
p=new MessageBox(doc);
try {
socket =new Socket("localhost",5858);
System.out.println("连接成功!");
In ta=new In(socket,area,p);
Out te=new Out(socket,a2,p);
new Thread(ta).start();
new Thread(te).start();
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
线程In(消息展示)
import java.awt.Color;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
public class In implements Runnable {
private Socket socket;
private JTextArea area;
private MessageBox p;
public In(Socket socket,JTextArea area,MessageBox p) {
this.socket=socket;
this.area=area;
this.p=p;
}
public void run() {
// TODO 自动生成的方法存根
try {
while(true) {
InputStream is = socket.getInputStream();//
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
p.LeftMessage(br.readLine()+"\n");
} }catch (Exception e) {
e.printStackTrace();
}
}
}
线程Out(消息发送)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.jar.Attributes.Name;
import javax.swing.JTextField;
public class Out implements Runnable{
Scanner sc=new Scanner(System.in);
private Socket socket;
private String text;
JTextField a2;
String name=null;
private MessageBox p;
public Out(Socket socket,JTextField a2,MessageBox p) {
this.socket=socket;
this.a2=a2;
this.p=p;
}
@Override
public void run() {
// TODO 自动生成的方法存根
OutputStream out;
try {
out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
PrintWriter pw = new PrintWriter(osw, true);
a2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String Temptext=a2.getText();
pw.println(Temptext);
a2.setText("");
p.RightMessage(Temptext+"\n");
}
});
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
以下就是重中之重,可以将消息接受展示且对齐的方法
MessageBox
import java.awt.Frame;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.text.*;
public class MessageBox {
private StyledDocument doc;
public MessageBox(StyledDocument doc)
{
this.doc=doc;
}
public void LeftMessage(String message) {
SimpleAttributeSet aSet=new SimpleAttributeSet();
StyleConstants.setAlignment(aSet, StyleConstants.ALIGN_LEFT);
doc.setParagraphAttributes(doc.getLength(),message.length(), aSet, true);
try {
doc.insertString(doc.getLength(), message, aSet);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public void RightMessage(String message) {
SimpleAttributeSet bSet=new SimpleAttributeSet();
StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_RIGHT);//右对齐
doc.setParagraphAttributes(doc.getLength(),message.length(), bSet, false);//内容如何出现,没有这行没有输出!!!
try {
// System.out.println("messstimeR="+doc.getLength());
doc.insertString(doc.getLength(), message, bSet);//内容追加
} catch (BadLocationException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public String Tostring(String p) {
return p;
}
}
运行结果
目前界面还是一个简单的文本对话框,没有将JTextPanel功能开发到最大,之后我会消息以气泡完善代码功能,敬请期待