Ⅰ 急求java 编辑类似QQ样子的聊天窗口代码!!
/**
*网络聊天工具
*左下角输入框输入对方的ip地址
*右下角输入框输入要发送的消息
*端口号:3000
*接收的消息在上方对话框中显示,新消息在上面
*/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
class Chat extends Frame
{
List lst = new List(8); //最多显示六条
TextField tfIP = new TextField(13); //IP地址输入文本框
TextField tfData = new TextField(50); //定义输入消息文本框
DatagramSocket ds = null;
public Chat() {
try
{
ds = new DatagramSocket(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
this.add(lst,"Center"); //增加列表框
Panel p = new Panel();
this.add(p,"South");
p.setLayout(new BorderLayout());
p.add(tfIP,"West"); //IP输入
p.add(tfData,"East"); //消息输入
new Thread(new Runnable()
{
public void run()
{
byte buf [] =new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,1024);
//显示消息
while(true)
{
try
{
ds.receive(dp);
lst.add(new String(buf,0,dp.getLength())+
" -->IP:"+dp.getAddress().getHostAddress()+" Port:"+dp.getPort(),0);
//新消息指定显示在第一行
//显示格式:消息from ip地址:端口号
}
catch (Exception e)
{
if(!ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
tfData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//取出消息及ip文本框中的内容
byte [] buf;
buf =tfData.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,
InetAddress.getByName(tfIP.getText()),3000);
ds.send(dp);
}
catch (Exception ex)
{
ex.printStackTrace();
}
//清空
tfData.setText("");
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
ds.close();
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting Chat……");
Chat mainFrame = new Chat();
mainFrame.setSize(500,500);
mainFrame.setTitle("迷你聊天工具");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
}
Ⅱ 急!java swing仿QQ聊天窗体折叠右边栏的实现
直接隐藏,就行!JPanel对象调用setVisible方法实现!
下面给你写了个例子,你变通一下就行了!
有问题再追问吧,good luck!~
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class HideJPanel extends JFrame implements ActionListener {
/**
* @Fields serialVersionUID : Description
*/
private static final long serialVersionUID = 1L;
private JButton hideBut = new JButton("隐藏");
private JButton showBut = new JButton("显示");
private JPanel panel = new JPanel();
/**
* 创建一个新的实例 HideJPanel.
*/
public HideJPanel() {
// TODO Auto-generated constructor stub
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
panel.add(new JLabel("基本信息"));
c.add(panel, BorderLayout.EAST);
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
//为按钮添加事件
hideBut.addActionListener(this);
showBut.addActionListener(this);
p1.add(showBut);
p1.add(hideBut);
c.add(p1, BorderLayout.SOUTH);
c.add(new JTextArea(), BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 500);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == showBut) {
panel.setVisible(true);
} else if (e.getSource() == hideBut) {
panel.setVisible(false);
}
}
public static void main(String[] args) {
new HideJPanel();
}
}
Ⅲ 用java实现QQ登录界面怎么写
用java做QQ登录界面的写法如下:
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1、//定义该类继承自JFrame,实现ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
2、//创建JPanel对象
private JPanel jp=new JPanel();
3、//创建3个标并加入数组
JLabel name = new JLabel("请输入用户名");
JLabel password = new JLabel("请输入密码");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
4、//创建登陆和重置按扭并加入数组
JButton login = new JButton("登陆");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
5、//创建文本框以及密码框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
6、//设置布局管理器为空布局,这里自己摆放按钮、标签和文本框
jp.setLayout(null);
for(int i=0;i<2;i++)
{
7、//设置标签和按扭的位置与大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
8、//添加标签和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//为2个按钮注册动作事件监听器
jb[i].addActionListener(this);
}
9、//设置文本框的位置和大小,注意满足美观并足够用户名的长度
jName.setBounds(130,15,100,20);
10、//添加文本框到JPanel容器中
jp.add(jName);
11、//为文本框注册动作事件监听器
jName.addActionListener(this);
12、//设置密码框的位置和大小,注意满足美观和足够密码的长度
jPassword.setBounds(130,60,100,20);
13、//添加密码框到JPanel容器中
jp.add(jPassword);
14、//设置密码框中的回显字符,这里设置美元符号
jPassword.setEchoChar('$');
15、//为密码框注册动作事件监听器
jPassword.addActionListener(this);
16、//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
17、//添加JPanel容器到窗体中
this.add(jp);
18、//设置窗体的标题、位置、大小、可见性及关闭动作
this.setTitle("登陆窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
19、//实现动作监听器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
20、//如果事件源为文本框
if(e.getSource()==jName)
{
21、//切换输入焦点到密码框
jPassword.requestFocus();
}
22、//如果事件源为重置按扭
else if(e.getSource()==jb[1])
{
23、//清空姓名文本框、密码框和show标签中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
24、//让输入焦点回到文本框
jName.requestFocus();
}
25、//如果事件源为登陆按钮,则判断登录名和密码是否正确
else
{
26、//判断用户名和密码是否匹配
if(jName.getText().equals("lixiangguo")&&
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
27、jl[2].setText("登陆成功,欢迎您的到来!");
}
else
{
28、jl[2].setText("对不起,您的用户名或密码错误!");
}
}
}
public static void main(String[] args)
{
29、//创建LoginTest窗体对象
new LoginTest();
}
}
Ⅳ 关于仿QQ聊天对话框的JAVA代码
1、swing的界面可以直接用netbeans画出来嘛。
2、可以把输出的聊天内容都放在一个StringBuffer里,每打出一句话,就把这句话追加在StringBuffer,然后把StringBuffer里的内容输出到Textarea中。
3、好友列表可以用JList
Ⅳ 用java做QQ的界面
基本的方法 当然是用个树控件..然后你自己设置下外观
不要windows窗口的办法就是 不要窗口 用JWindow就行了
然后自己设计工具栏 透明度 那个复杂点 要得到当前背景色像素
的三位颜色值 然后得到你想描的这个点的颜色值 然后根据透明参数 进行合成 然后再描点 反正就是精确到像素级别的描点...java有处理类似功能的包
java.awt.image.*
Ⅵ 你好,请问怎么用Java做一个简单类似qq的聊天界面.
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
{
/**
*
*/
=1L;
publicstaticvoidmain(String[]args){
newChatRoom();
}
privateJFrameframe;
privateJTextAreaviewArea;
privateJTextFieldviewField;
privateJButtonbutton1;
privateJButtonbutton2;
privateJLabeljlable;
privateJTextFieldMyName;
publicChatRoom(){
frame=newJFrame("ChatRoom");
viewArea=newJTextArea(10,50);
viewField=newJTextField(50);
jlable=newJLabel();
jlable.setText("在线");
button1=newJButton("Send");
button2=newJButton("Quit");
MyName=newJTextField();
MyName.setColumns(9);
MyName.setText("飞翔的企鹅");
JPanelpanel=newJPanel();
panel.setLayout(newGridLayout(8,1));
panel.add(jlable);
panel.add(MyName);
panel.add(button1);
panel.add(button2);
JScrollPanesp=newJScrollPane(viewArea);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add("Center",sp);
frame.add("East",panel);
frame.add("South",viewField);
frame.setSize(500,250);
frame.setVisible(true);
button1.addMouseListener((MouseListener)this);
button2.addMouseListener((MouseListener)this);
}
publicvoidmouseClicked(MouseEventevt){
Stringmessage="";
message=MyName.getText()+viewField.getText();
if(evt.getSource()==button1){
viewArea.setText(viewArea.getText()+message+" ");
}
if(evt.getSource()==button2){
message="退出";
viewArea.setText(message);
viewField.setText("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
publicvoidmousePressed(MouseEventevt){}
publicvoidmouseReleased(MouseEventevt){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
}
Ⅶ 用Java编写类似QQ对话框程序
给你个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[]) {
}
}
Ⅷ 用java制作qq登录界面,只要界面,不要事件处理
package ibees.qq;
import java.awt.BorderLayout;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* 仿QQ登录界面,仅供学习参考,涉及到的有窗口居中、JPanel、LayoutManager的使用
* @author hhzxj2008
* */
public class QQLoginView extends JFrame {
/**
*
*/
private static final long serialVersionUID = -5665975170821790753L;
public QQLoginView() {
initComponent();
}
private void initComponent() {
setTitle("用户登录");
//设置LOGO
URL image = QQLoginView.class.getClassLoader().getResource("ibees/qq/images/year.jpg");//图片的位置
JLabel imageLogo = new JLabel(new ImageIcon(image));
add(imageLogo,BorderLayout.NORTH);
//QQ号和密码
JPanel jp = new JPanel();
JPanel jpAccount = new JPanel();
jpAccount.add(new JLabel("帐号"));
JTextField userTextField = new JTextField(15);
jpAccount.add(userTextField);
jpAccount.add(new JLabel("用户注册"));
jp.add(jpAccount);
JPanel jpPass = new JPanel();
jpPass.add(new JLabel("密码"));
JPasswordField passTextField = new JPasswordField(15);
jpPass.add(passTextField);
jpPass.add(new JLabel("找回密码"));
jp.add(jpPass);
//登录设置
JPanel jpstatus = new JPanel();
jpstatus.add(new JLabel("状态"));
JComboBox statusComboBox = new JComboBox();
statusComboBox.addItem("Q我");
statusComboBox.addItem("在线");
statusComboBox.addItem("隐身");
statusComboBox.addItem("离线");
jpstatus.add(statusComboBox);
jpstatus.add(new JCheckBox("记住密码"));
jpstatus.add(new JCheckBox("自动登录"));
jp.add(jpstatus);
add(jp);
//底部登录按钮
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(new JButton("设置"),BorderLayout.WEST);
bottomPanel.add(new JButton("登录"),BorderLayout.EAST);
add(bottomPanel,BorderLayout.SOUTH);
setSize(324,230);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
* @param args
*/
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
new QQLoginView().setVisible(true);
}
});
}
}
Ⅸ java做QQ
主要我想是用到两大块一个用到java.swing里面的类,一块是java.net的类,服务器端应该要有数据库存储用户信息,对java做桌面软件了解不多不多说了,我记得看大电驴上有做山寨QQ的教程,楼主可以去下来看看学习一下