用Java编写类似QQ对话框程序
3个回答
展开全部
给你个Socket/ServerSocket写的小例子,看看能不能帮到你哦:
先运行ServerGUI,启动服务器端,再运行ClientGUI,双方就可以发送字符串了...
ServerGUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
@SuppressWarnings("serial")
public class ServerGUI extends JFrame {
private JTextArea jta, jtaInput;
private JPanel jtaAreaPane;
private JPanel j;
private JButton buttonSubmit, buttonExit;
private String stringGet = null;
private OurServer os = null;
public ServerGUI(String s) {
super(s);
jtaAreaPane = new JPanel();
jtaAreaPane.setLayout(new GridLayout(2, 1));
jta = new JTextArea(7, 35);
jta.setLineWrap(true);
jta.setBackground(new Color(169, 255, 128));
JScrollPane jsp = new JScrollPane(jta);
jtaInput = new JTextArea(7, 35);
jtaInput.setLineWrap(true);
jtaInput.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
os.getOsw().write(stringGet + "\n");
os.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
jtaInput.setBackground(new Color(133, 168, 250));
JScrollPane jspIn = new JScrollPane(jtaInput);
jtaAreaPane.add(jsp);
jtaAreaPane.add(jspIn);
j = new JPanel();
buttonSubmit = new JButton("提交");
buttonSubmit.addActionListener(new SetButtonSubmit());
buttonExit = new JButton("关闭");
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
j.add(buttonSubmit);
j.add(buttonExit);
setLayout(new BorderLayout());
add(jtaAreaPane, BorderLayout.CENTER);
add(j, BorderLayout.SOUTH);
setSize(new Dimension(400, 500));
setLocation(500, 300);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
new Thread(new ServerOurRead()).start();
os = new OurServer();
}
public String GetString() {
return stringGet;
}
public void setStringGet(String s) {
this.stringGet = s;
}
class SetButtonSubmit implements ActionListener {
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e) {
String s = jtaInput.getText();
jtaInput.setText(null);
jta.append("\n" + "你发送了:" + s.trim());// jta.setText("你发送了:"+oc.s);
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
os.getOsw().write(stringGet + "\n");
os.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 接收来自客户端的线程
class ServerOurRead implements Runnable {
public void run() {
while (true) {
try {
jta.append("\n来自客户端:" + os.getBr().readLine());
} catch (Exception e1) {
e1.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new ServerGUI("服务器对话框");
}
}
OurServer类:
import java.util.*;
import java.io.*;
import java.net.*;
public class OurServer {
private ServerSocket serverSocket = null;
private OutputStream os = null;
private InputStream is = null;
private OutputStreamWriter osw = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private ArrayList<Socket> socketList = null;
private Scanner console = new Scanner(System.in);
public OurServer() {
try {
serverSocket = new ServerSocket(22222);
System.out.println("serverSocket is waiting...");
Socket soc = serverSocket.accept();
os = soc.getOutputStream();
is = soc.getInputStream();
osw = new OutputStreamWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (IOException e) {
e.printStackTrace();
}
}
// 从客户端读信息的线程
public static void main(String args[]) {
}
public BufferedReader getBr() {
return br;
}
public void setBr(BufferedReader br) {
this.br = br;
}
public OutputStreamWriter getOsw() {
return osw;
}
public void setOsw(OutputStreamWriter osw) {
this.osw = osw;
}
}
ClientGUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
@SuppressWarnings("serial")
public class ClientGUI extends JFrame {
private JTextArea jta, jtaInput;
private JPanel jtaAreaPanel;
private JPanel j;
private JButton buttonSubmit, buttonExit;
private String stringGet = null;
private OurClient oc = null;
public ClientGUI(String s) {
super(s);
jtaAreaPanel = new JPanel();
jtaAreaPanel.setLayout(new GridLayout(2, 1));
jta = new JTextArea(7, 35);
jta.setLineWrap(true);
jta.setBackground(new Color(169, 255, 128));
JScrollPane jsp = new JScrollPane(jta);
jtaInput = new JTextArea(7, 35);
jtaInput.setLineWrap(true);
jtaInput.setBackground(new Color(133, 168, 250));
jtaInput.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
oc.getOsw().write(stringGet + "\n");
oc.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
JScrollPane jspIn = new JScrollPane(jtaInput);
jtaAreaPanel.add(jsp);
jtaAreaPanel.add(jspIn);
j = new JPanel();
buttonSubmit = new JButton("提交");
buttonSubmit.addActionListener(new SetButtonSubmit());
buttonExit = new JButton("关闭");
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
j.add(buttonSubmit);
j.add(buttonExit);
setLayout(new BorderLayout());
add(jtaAreaPanel, BorderLayout.CENTER);
add(j, BorderLayout.SOUTH);
setSize(new Dimension(400, 500));
setLocation(500, 300);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
// 启动读取服务端信息线程
new Thread(new ServerOurRead()).start();
oc = new OurClient();
}
public String GetString() {
return stringGet;
}
public void setStringGet(String s) {
this.stringGet = s;
}
class SetButtonSubmit implements ActionListener {
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
oc.getOsw().write(stringGet + "\n");
oc.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 接收来自客户端的线程
class ServerOurRead implements Runnable {
public void run() {
while (true) {
try {
// oc.getBr().readLine()此方法一直在读,直到流中有数据
jta.append("\n来自客户端:" + oc.getBr().readLine());// readLine()
} catch (Exception e1) {
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new ClientGUI("对话框");
}
}
OurClient 类:
import java.io.*;
import java.net.*;
import java.util.*;
public class OurClient {
private Socket socket = null;
private OutputStream os = null;
private InputStream is = null;
private OutputStreamWriter osw = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private Scanner console = null;
private static String s = null;
private static String In = null;
public OurClient() {
console = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1", 22222);
os = socket.getOutputStream();
is = socket.getInputStream();
osw = new OutputStreamWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 从服务端读信息的线程
public BufferedReader getBr() {
return br;
}
public void setBr(BufferedReader br) {
this.br = br;
}
public OutputStreamWriter getOsw() {
return osw;
}
public void setOsw(OutputStreamWriter osw) {
this.osw = osw;
}
public static void main(String args[]) {
}
}
先运行ServerGUI,启动服务器端,再运行ClientGUI,双方就可以发送字符串了...
ServerGUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
@SuppressWarnings("serial")
public class ServerGUI extends JFrame {
private JTextArea jta, jtaInput;
private JPanel jtaAreaPane;
private JPanel j;
private JButton buttonSubmit, buttonExit;
private String stringGet = null;
private OurServer os = null;
public ServerGUI(String s) {
super(s);
jtaAreaPane = new JPanel();
jtaAreaPane.setLayout(new GridLayout(2, 1));
jta = new JTextArea(7, 35);
jta.setLineWrap(true);
jta.setBackground(new Color(169, 255, 128));
JScrollPane jsp = new JScrollPane(jta);
jtaInput = new JTextArea(7, 35);
jtaInput.setLineWrap(true);
jtaInput.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
os.getOsw().write(stringGet + "\n");
os.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
jtaInput.setBackground(new Color(133, 168, 250));
JScrollPane jspIn = new JScrollPane(jtaInput);
jtaAreaPane.add(jsp);
jtaAreaPane.add(jspIn);
j = new JPanel();
buttonSubmit = new JButton("提交");
buttonSubmit.addActionListener(new SetButtonSubmit());
buttonExit = new JButton("关闭");
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
j.add(buttonSubmit);
j.add(buttonExit);
setLayout(new BorderLayout());
add(jtaAreaPane, BorderLayout.CENTER);
add(j, BorderLayout.SOUTH);
setSize(new Dimension(400, 500));
setLocation(500, 300);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
new Thread(new ServerOurRead()).start();
os = new OurServer();
}
public String GetString() {
return stringGet;
}
public void setStringGet(String s) {
this.stringGet = s;
}
class SetButtonSubmit implements ActionListener {
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e) {
String s = jtaInput.getText();
jtaInput.setText(null);
jta.append("\n" + "你发送了:" + s.trim());// jta.setText("你发送了:"+oc.s);
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
os.getOsw().write(stringGet + "\n");
os.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 接收来自客户端的线程
class ServerOurRead implements Runnable {
public void run() {
while (true) {
try {
jta.append("\n来自客户端:" + os.getBr().readLine());
} catch (Exception e1) {
e1.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new ServerGUI("服务器对话框");
}
}
OurServer类:
import java.util.*;
import java.io.*;
import java.net.*;
public class OurServer {
private ServerSocket serverSocket = null;
private OutputStream os = null;
private InputStream is = null;
private OutputStreamWriter osw = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private ArrayList<Socket> socketList = null;
private Scanner console = new Scanner(System.in);
public OurServer() {
try {
serverSocket = new ServerSocket(22222);
System.out.println("serverSocket is waiting...");
Socket soc = serverSocket.accept();
os = soc.getOutputStream();
is = soc.getInputStream();
osw = new OutputStreamWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (IOException e) {
e.printStackTrace();
}
}
// 从客户端读信息的线程
public static void main(String args[]) {
}
public BufferedReader getBr() {
return br;
}
public void setBr(BufferedReader br) {
this.br = br;
}
public OutputStreamWriter getOsw() {
return osw;
}
public void setOsw(OutputStreamWriter osw) {
this.osw = osw;
}
}
ClientGUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
@SuppressWarnings("serial")
public class ClientGUI extends JFrame {
private JTextArea jta, jtaInput;
private JPanel jtaAreaPanel;
private JPanel j;
private JButton buttonSubmit, buttonExit;
private String stringGet = null;
private OurClient oc = null;
public ClientGUI(String s) {
super(s);
jtaAreaPanel = new JPanel();
jtaAreaPanel.setLayout(new GridLayout(2, 1));
jta = new JTextArea(7, 35);
jta.setLineWrap(true);
jta.setBackground(new Color(169, 255, 128));
JScrollPane jsp = new JScrollPane(jta);
jtaInput = new JTextArea(7, 35);
jtaInput.setLineWrap(true);
jtaInput.setBackground(new Color(133, 168, 250));
jtaInput.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
oc.getOsw().write(stringGet + "\n");
oc.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
JScrollPane jspIn = new JScrollPane(jtaInput);
jtaAreaPanel.add(jsp);
jtaAreaPanel.add(jspIn);
j = new JPanel();
buttonSubmit = new JButton("提交");
buttonSubmit.addActionListener(new SetButtonSubmit());
buttonExit = new JButton("关闭");
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
j.add(buttonSubmit);
j.add(buttonExit);
setLayout(new BorderLayout());
add(jtaAreaPanel, BorderLayout.CENTER);
add(j, BorderLayout.SOUTH);
setSize(new Dimension(400, 500));
setLocation(500, 300);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
// 启动读取服务端信息线程
new Thread(new ServerOurRead()).start();
oc = new OurClient();
}
public String GetString() {
return stringGet;
}
public void setStringGet(String s) {
this.stringGet = s;
}
class SetButtonSubmit implements ActionListener {
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e) {
String s = jtaInput.getText();
jtaInput.setText(null); // 输入框重新设为空
jta.append("\n" + "你发送了:" + s.trim());
stringGet = s;
if (stringGet != null) {
System.out.println("stringGet:" + stringGet);
try {
oc.getOsw().write(stringGet + "\n");
oc.getOsw().flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 接收来自客户端的线程
class ServerOurRead implements Runnable {
public void run() {
while (true) {
try {
// oc.getBr().readLine()此方法一直在读,直到流中有数据
jta.append("\n来自客户端:" + oc.getBr().readLine());// readLine()
} catch (Exception e1) {
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
new ClientGUI("对话框");
}
}
OurClient 类:
import java.io.*;
import java.net.*;
import java.util.*;
public class OurClient {
private Socket socket = null;
private OutputStream os = null;
private InputStream is = null;
private OutputStreamWriter osw = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private Scanner console = null;
private static String s = null;
private static String In = null;
public OurClient() {
console = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1", 22222);
os = socket.getOutputStream();
is = socket.getInputStream();
osw = new OutputStreamWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 从服务端读信息的线程
public BufferedReader getBr() {
return br;
}
public void setBr(BufferedReader br) {
this.br = br;
}
public OutputStreamWriter getOsw() {
return osw;
}
public void setOsw(OutputStreamWriter osw) {
this.osw = osw;
}
public static void main(String args[]) {
}
}
展开全部
呵呵,只能告诉你思路吧
如果效果好点的话,可以考虑使用SWT他可以基于任何系统中的窗口模式
这种功能呢,socket可以实现,写两个socket程序,互相接受并发送
如果效果好点的话,可以考虑使用SWT他可以基于任何系统中的窗口模式
这种功能呢,socket可以实现,写两个socket程序,互相接受并发送
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这样有详细的说明
参考资料: http://www.weamax.com/xinjingji/xinjishu/Webyanfa/2009/0804/21876.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询