Ⅰ 急求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的教程,樓主可以去下來看看學習一下