日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java版QQ 欢迎点评

發布時間:2023/12/8 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java版QQ 欢迎点评 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*
?* To change this template, choose Tools | Templates
?* and open the template in the editor.

剛寫的java版QQ,實現C/S聊天室功能,與在線好友聊天,添加刪除好友。選擇UDP數據報而不是socket,不區分client和sever,直接通信。
?*/
package myqq;

import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;

/**
?*
?* @author honor school libing
?*
?*/
public class MyQQ {

??? /**
???? * @param args the command line arguments
???? */
??? public static void main(String[] args) {
??????? // TODO code application logic here

??????? DrawQFrame frame = new DrawQFrame();
??????? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??????? frame.setVisible(true);

??? }
}

//面板類
class DrawQFrame extends JFrame {
//框架類

??? public DrawQFrame() {
??????? this.setLocation(100, 100);
??????? this.setTitle("360QQ");
??????? this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
??????? this.setLayout(new BorderLayout());

??????? DrawQPanel panel = new DrawQPanel();
??????? this.add(panel, BorderLayout.NORTH);
??????? Border etched = BorderFactory.createEtchedBorder();
??????? Border title = BorderFactory.createTitledBorder(etched, "360QQ");//加入邊框


??????? panel.setBorder(title);

??????? Border lineborder = BorderFactory.createLineBorder(Color.yellow);
??????? panel.setBorder(lineborder);

?

??????? // Border matteBorder = BorderFactory.createMatteBorder(30, 30, 30, 30, Color.BLUE);
??????? // panel.setBorder(matteBorder);


??? }
??? public static final int DEFAULT_WIDTH = 200;
??? public static final int DEFAULT_HEIGHT = 400;
}

//面板類
class DrawQPanel extends JPanel {

??? public DrawQPanel() {
??????? this.setSize(100, 300);
??????? // this.setLayout(new CardLayout(10,1));
??????? this.setLayout(new BorderLayout());
??????? JButton button = new JButton("與神秘陌生人聊天");
??????? this.add(button, new BorderLayout().CENTER);
??????? button.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? System.out.println("你好");
??????????????? DrawFrame frame = new DrawFrame();
??????????????? frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????????? frame.setVisible(true);
??????????? }
??????? });
??????? setLayout(new GridLayout(15, 1));
??????? JTextField textField = new JTextField();
??????? textField.setText("好友昵稱或Q號");
??????? JButton searchButton = new JButton("搜索好友");
??????? this.add(textField);
??????? this.add(searchButton);

??????? searchButton.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent e) {
??????????? }
??????? });

?

??????? friendList.add(bestFriend);
??????? friendList.add(bestFriend2);
??????? // buttonList.add(new JButton(friendList.get(0).name + "(" + friendList.get(0).nickname + ")" + friendList.get(0).autograph));
??????? //? buttonList.add(new JButton(friendList.get(1).name + "(" + friendList.get(1).nickname + ")" + friendList.get(1).autograph));
??????? setLayout(new GridLayout(15, 1));

??????? for (int i = 0; i < friendList.size(); i++) {
??????????? buttonList.add(new JButton(friendList.get(i).name + "(" + friendList.get(i).nickname + ")" + friendList.get(i).autograph));
??????????? this.add(buttonList.get(i));
??????????? final String tempName = friendList.get(i).name;
??????????? final String tempNickname = friendList.get(i).nickname;
??????????? final String tempAutograph = friendList.get(i).autograph;
??????????? buttonList.get(i).addActionListener(new ActionListener() {

??????????????? public void actionPerformed(ActionEvent e) {
??????????????????? DrawFrame talkFrame = new DrawFrame();

??????????????????? talkFrame.setTitle(tempName + "(" + tempNickname + ")" + tempAutograph);
??????????????????? talkFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????????????? talkFrame.setVisible(true);

??????????????? }
??????????? });
??????? }
??????? JButton addFriendButton = new JButton("添加好友");
??????? this.add(addFriendButton);
??????? addFriendButton.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent e) {
??????????????? addFriendFrame addFriendFrame1 = new addFriendFrame();
??????????????? addFriendFrame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????????? addFriendPanel panel = new addFriendPanel();
??????????????? addFriendFrame1.add(panel);

??????????????? final JTextField friendName = new JTextField(10);
??????????????? final JTextField friendNickname = new JTextField(10);
??????????????? final JTextField friendAutograph = new JTextField(10);
??????????????? JLabel nameLabel = new JLabel("好友姓名");
??????????????? JLabel nicknameLabel = new JLabel("好友昵稱");
??????????????? JLabel autographLabel = new JLabel("好友簽名");
??????????????? JButton button1 = new JButton("保存");
??????????????? button1.addActionListener(new ActionListener() {

??????????????????? public void actionPerformed(ActionEvent e) {
??????????????????????? friendList.remove(friendList.size() - 1);
??????????????????????? friendList.remove(friendList.size() - 1);
??????????????????????? buttonList.clear();
??????????????????????? DrawQPanel.friendList.add(new MyFriend(friendName.getText(), friendNickname.getText(), friendAutograph.getText()));


??????????????????????? DrawQFrame frame = new DrawQFrame();
??????????????????????? frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????????????????? frame.setVisible(true);
??????????????????? }
??????????????? });
??????????????? panel.add(friendName);
??????????????? panel.add(nameLabel);
??????????????? panel.add(friendNickname);
??????????????? panel.add(nicknameLabel);
??????????????? panel.add(friendAutograph);
??????????????? panel.add(autographLabel);

??????????????? panel.add(button1);
??????????????? addFriendFrame1.setSize(200, 180);
??????????????? addFriendFrame1.setLocation(500, 400);
??????????????? addFriendFrame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????????? addFriendFrame1.setVisible(true);
??????????? }
??????? });

??? }

??? public class MyFriend {

??????? private MyFriend(String string, String string0, String string1) {
??????????? this.name = string;
??????????? this.nickname = string0;
??????????? this.autograph = string1;
??????? }

??????? public void MyFriend(String name, String nickname, String autograph) {
??????????? this.name = name;
??????????? this.nickname = nickname;
??????????? this.autograph = autograph;
??????? }
??????? private String name;
??????? private String nickname;
??????? private String autograph;
??? }

??? public class addFriendFrame extends JFrame {

??????? public void addFriendFrame() {
??????????? JFrame addFriendFrame = new JFrame("添加好友");
??????????? addFriendFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??????????? JPanel panel = new JPanel();
??????????? addFriendFrame.add(panel);
??????????? JButton button1 = new JButton("保存");
??????????? panel.add(button1);
??????????? addFriendFrame.setSize(200, 180);

??????? }
??? }

??? class addFriendPanel extends JPanel {

??????? public addFriendPanel() {
??????????? JPanel addFriendPanel = new JPanel();

??????? }
??? }

??? public ArrayList getFriendList() {
??????? return this.friendList;
??? }

??? public ArrayList getbuttonList() {
??????? return this.buttonList;
??? }
??? private static ArrayList<MyFriend> friendList = new ArrayList<MyFriend>(10);
??? MyFriend bestFriend = new MyFriend("李兵", "火", "Never Give up!");
??? MyFriend bestFriend2 = new MyFriend("實驗學院", "honor school", "Never Give up!");
??? private static ArrayList<JButton> buttonList = new ArrayList<JButton>(10);
}

class DrawFrame extends JFrame {

??? public DrawFrame() {

??????? setLocation(100, 100);
??????? setTitle("360QQ");
??????? setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//添加菜單項……
??????? JMenuBar menuBar = new JMenuBar();
??????? setJMenuBar(menuBar);
??????? JMenu menu = new JMenu("菜單");
??????? menuBar.add(menu);
??????? JMenuItem sset = new JMenuItem("設置");
??????? JMenuItem eexit = new JMenuItem("退出");
??????? menu.add(sset);
??????? menu.addSeparator();
??????? menu.add(eexit);

??????? JMenu video = new JMenu("視頻");
??????? menuBar.add(video);
??????? JMenuItem startVideo = new JMenuItem("發起視頻通話", new ImageIcon("computer.ico"));
??????? JMenuItem mutiVideo = new JMenuItem("多人視頻聊天");
??????? JMenuItem setVideo = new JMenuItem("視頻設置");
??????? JMenuItem testVideo = new JMenuItem("視頻測試向導");
??????? video.add(startVideo);
??????? video.addSeparator();
??????? video.add(mutiVideo);
??????? video.addSeparator();
??????? video.add(setVideo);
??????? video.addSeparator();
??????? video.add(testVideo);

?

??????? startVideo.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent e) {
??????????????? JFrame littleFrame = new JFrame();
??????????????? JOptionPane.showMessageDialog(littleFrame, "正在連接,請稍后……");
??????????????? littleFrame.dispose();
??????????? }
??????? });
??????? class MyListener implements ActionListener {

??????????? public void actionPerformed(ActionEvent e) {
??????????????? JFrame littleFrame = new JFrame();
??????????????? JOptionPane.showMessageDialog(littleFrame, "正在連接,請稍后……");
??????????????? littleFrame.dispose();
??????????? }
??????? }
??????? mutiVideo.addActionListener(new MyListener());
??????? setVideo.addActionListener(new MyListener());
??????? testVideo.addActionListener(new MyListener());


??????? JMenu voice = new JMenu("語音");
??????? menuBar.add(voice);
??????? JMenuItem startVoice = new JMenuItem("發起語音通話");
??????? JMenuItem mutiVoice = new JMenuItem("多人語音通話");
??????? JMenuItem setVoice = new JMenuItem("語音設置");
??????? JMenuItem testVoice = new JMenuItem("語音測試向導");
??????? voice.add(startVoice);
??????? voice.addSeparator();
??????? voice.add(mutiVoice);
??????? voice.addSeparator();
??????? voice.add(setVoice);
??????? voice.addSeparator();
??????? voice.add(testVoice);
??????? startVoice.addActionListener(new MyListener());
??????? mutiVoice.addActionListener(new MyListener());
??????? setVoice.addActionListener(new MyListener());
??????? testVoice.addActionListener(new MyListener());

??????? JMenu file = new JMenu("文件");
??????? menuBar.add(file);
??????? JMenuItem sendFile = new JMenuItem("發送文件");
??????? JMenuItem sendMutiFile = new JMenuItem("發送文件夾");
??????? JMenuItem sendOfflineFile = new JMenuItem("發送離線文件");
??????? JMenuItem setFile = new JMenuItem("發送設置");
??????? file.add(sendFile);
??????? file.addSeparator();
??????? file.add(sendMutiFile);
??????? file.addSeparator();
??????? file.add(sendOfflineFile);
??????? file.addSeparator();
??????? file.add(setFile);
??????? sendFile.addActionListener(new MyListener());
??????? sendMutiFile.addActionListener(new MyListener());
??????? sendOfflineFile.addActionListener(new MyListener());
??????? setFile.addActionListener(new MyListener());

??????? JMenu SMS = new JMenu("短信");
??????? menuBar.add(SMS);
??????? JMenuItem sendSMS = new JMenuItem("給該好友發短信");
??????? JMenuItem sendMutiSMS = new JMenuItem("群發短信");
??????? JMenuItem sendOfflineSMS = new JMenuItem("發送離線短信");
??????? JMenuItem setSMS = new JMenuItem("短信設置");
??????? SMS.add(sendSMS);
??????? SMS.addSeparator();
??????? SMS.add(sendMutiSMS);
??????? SMS.addSeparator();
??????? SMS.add(sendOfflineSMS);
??????? SMS.addSeparator();
??????? SMS.add(setSMS);
??????? sendSMS.addActionListener(new MyListener());
??????? sendMutiSMS.addActionListener(new MyListener());
??????? sendOfflineSMS.addActionListener(new MyListener());
??????? setSMS.addActionListener(new MyListener());

??????? JMenu sue = new JMenu("舉報");
??????? menuBar.add(sue);
??????? JMenuItem sueFriend = new JMenuItem("舉報該好友");
??????? JMenuItem deleteFriend = new JMenuItem("刪除該好友");
??????? JMenuItem blackListFriend = new JMenuItem("將該好友移至黑名單");
??????? sue.add(sueFriend);
??????? sue.addSeparator();
??????? sue.add(deleteFriend);
??????? sue.addSeparator();
??????? sue.add(blackListFriend);
??????? sueFriend.addActionListener(new MyListener());
??????? deleteFriend.addActionListener(new MyListener());
??????? blackListFriend.addActionListener(new MyListener());

??????? JMenu game = new JMenu("休閑娛樂");
??????? menuBar.add(game);
??????? JMenuItem QQLive = new JMenuItem("QQLive網絡電視");
??????? JMenuItem cellphoneQQ = new JMenuItem("手機聊QQ");
??????? JMenuItem WebGame = new JMenuItem("網絡游戲");
??????? JMenuItem cleverGame = new JMenuItem("益智游戲");
??????? JMenuItem adorn = new JMenuItem("裝扮");
??????? game.add(QQLive);
??????? game.addSeparator();
??????? game.add(cellphoneQQ);
??????? game.addSeparator();
??????? game.add(WebGame);
??????? game.addSeparator();
??????? game.add(cleverGame);
??????? game.addSeparator();
??????? game.add(adorn);
??????? QQLive.addActionListener(new MyListener());
??????? cellphoneQQ.addActionListener(new MyListener());
??????? WebGame.addActionListener(new MyListener());
??????? cleverGame.addActionListener(new MyListener());
??????? adorn.addActionListener(new MyListener());


??????? final DrawPanel pan = new DrawPanel();
??????? //添加設置對話框
??????? sset.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? final JFrame jf = new JFrame("設置");
??????????????? jf.setSize(300, 150);
??????????????? jf.setLocation(200, 200);
??????????????? jf.setResizable(false);
??????????????? jf.setVisible(true);
??????????????? JPanel jp = new JPanel();
??????????????? final JTextField jt1 = new JTextField(20);
??????????????? final JTextField jt2 = new JTextField(20);
??????????????? final JTextField jt3 = new JTextField(20);
??????????????? jp.add(new JLabel("聊天端口:"));
??????????????? jf.add(jp);
??????????????? jp.add(jt1);
??????????????? jp.add(new JLabel("目標地址:"));
??????????????? jp.add(jt2);
??????????????? jp.add(new JLabel("文件端口:"));
??????????????? jp.add(jt3);
??????????????? JButton jb1 = new JButton("確定");
??????????????? JButton jb2 = new JButton("取消");
??????????????? jp.add(jb1);
??????????????? jp.add(jb2);
??????????????? //設置“設置”按鈕
??????????????? jb1.addActionListener(new ActionListener() {

??????????????????? public void actionPerformed(ActionEvent event) {
??????????????????????? try {
??????????????????????????? int cat1 = Integer.parseInt(jt1.getText());
??????????????????????????? pan.setCat1(cat1);//設置對話端口號
??????????????????????????? int cat2 = Integer.parseInt(jt3.getText());
??????????????????????????? pan.setCat2(cat2);//設置文件傳輸端口號
??????????????????????????? String s = jt2.getText();
??????????????????????????? pan.setIP(s);//設置IP地址
??????????????????????????? pan.ls();
??????????????????????? } catch (Exception e) {
??????????????????????????? e.printStackTrace();
??????????????????????? }
??????????????????????? jf.dispose();//關閉對話框
??????????????????? }
??????????????? });
??????????????? jb2.addActionListener(new ActionListener() {

??????????????????? public void actionPerformed(ActionEvent event) {
??????????????????????? jf.dispose();
??????????????????? }
??????????????? });
??????????? }
??????? });
??????? eexit.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? System.exit(0);
??????????? }
??????? });

??????? add(pan);
??????? pack();
??? }
??? public static final int DEFAULT_WIDTH = 400;
??? public static final int DEFAULT_HEIGHT = 400;
}

class DrawPanel extends JPanel {

??? DatagramSocket ds;
??? DatagramPacket dp;
??? DatagramSocket ds1;
??? DatagramPacket dp1;
??? Thread thread;
??? Thread thread1;
??? TextArea textArea1 = new TextArea(10, 50);
??? TextArea textArea2 = new TextArea(10, 50);
??? JTextField textField = new JTextField(50);
??? static int cat1, cat2;
??? static String IP;

??? public void setCat1(int cat) {
??????? cat1 = cat;
??? }

??? public void setCat2(int cat) {
??????? cat2 = cat;
??? }

??? public void setIP(String ip) {
??????? IP = ip;
??? }
??? //重置端口號

??? public void ls() {
??????? try {
??????????? ds = new DatagramSocket(cat1);
??????????? ds1 = new DatagramSocket(cat2);
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? thread = new Thread(new Runnable() {

??????????? public void run() {
??????????????? byte buf[] = new byte[1024];//聊天每次發送文字不能超過1024b
??????????????? DatagramPacket dp = new DatagramPacket(buf, buf.length);

??????????????? while (true) {
??????????????????? try {
??????????????????????? ds.receive(dp);
??????????????????????? textArea1.setText(textArea1.getText() + new String(buf, 0, dp.getLength()) + "/r/n");
??????????????????? } catch (Exception e) {
??????????????????????? e.printStackTrace();
??????????????????? }
??????????????? }
??????????? }
??????? });
??????? thread.start();
??? }

??? public DrawPanel() {


??????? setLayout(new BorderLayout());
??????? JTabbedPane card = new JTabbedPane();

??????? textArea1.setEditable(false);
??????? card.add("聊天", textArea1);
??????? add(card, BorderLayout.NORTH);

??????? JPanel center = new JPanel();
??????? center.setLayout(new FlowLayout());
??????? center.add(new JLabel("昵稱:"));

??????? center.add(textField);
??????? add(center, BorderLayout.CENTER);

??????? JPanel south = new JPanel();
??????? south.setLayout(new BorderLayout());


??????? south.add(textArea2, BorderLayout.NORTH);

??????? JPanel south2 = new JPanel();
??????? south2.setLayout(new FlowLayout());
??????? JButton sent = new JButton("傳送文件");
??????? JButton receive = new JButton("接收文件");
??????? //receive.setEnabled(false);
??????? JButton sentMessage = new JButton("發送");
??????? south2.add(sent);
??????? south2.add(receive);

??????? south2.add(sentMessage);
??????? add(south, BorderLayout.SOUTH);
??????? south.add(south2, BorderLayout.SOUTH);
??????? //發送信息的按鈕的監聽器:
??????? sentMessage.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? textArea1.append(textField.getText() + ":" + textArea2.getText() + "/n");
??????????????? String s;
??????????????? try {
??????????????????? s = textField.getText() + ":" + textArea2.getText();
??????????????????? byte[] buf = s.getBytes();
??????????????????? dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(IP), cat1);
??????????????????? ds.send(dp);
??????????????????? textArea2.setText("");
??????????????? } catch (Exception ex) {
??????????????????? ex.printStackTrace();
??????????????? }
??????????? }
??????? });
??????? //發送文件的按鈕的監聽器
??????? sent.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? JFrame f = new JFrame("打開");
??????????????? f.setSize(400, 300);
??????????????? JFileChooser chooser = new JFileChooser();
??????????????? f.add(chooser);
??????????????? int returnVal = chooser.showOpenDialog(f);
??????????????? if (returnVal == JFileChooser.APPROVE_OPTION) {
??????????????????? textArea1.append("正在傳送文件:" + chooser.getCurrentDirectory() + "//"
??????????????????????????? + chooser.getSelectedFile().getName() + "/n");
??????????????????? try {
??????????????????????? byte[] b = new byte[10248];
??????????????????????? FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
??????????????????????? DataInputStream dos = new DataInputStream(fis);
??????????????????????? dos.read(b);
??????????????????????? dp1 = new DatagramPacket(b, b.length, InetAddress.getByName(IP), cat2);
??????????????????????? ds1.send(dp1);
??????????????????? } catch (Exception e) {
??????????????????????? e.printStackTrace();
??????????????????? }
??????????????? }
??????????? }
??????? });
??????? //接收文件的按鈕的監聽器:
??????? receive.addActionListener(new ActionListener() {

??????????? public void actionPerformed(ActionEvent event) {
??????????????? JFrame f1 = new JFrame("保存");
??????????????? f1.setSize(400, 300);
??????????????? byte buf[] = new byte[10248];
??????????????? DatagramPacket dp1 = new DatagramPacket(buf, buf.length);
??????????????? JFileChooser ch = new JFileChooser();
??????????????? f1.add(ch);
??????????????? int a = ch.showSaveDialog(f1);
??????????????? if (a == JFileChooser.APPROVE_OPTION) {
??????????????????? String fileName = ch.getSelectedFile().getPath();
??????????????????? try {
??????????????????????? ds1.receive(dp1);
??????????????????????? FileOutputStream fos = new FileOutputStream(fileName);
??????????????????????? fos.write(buf);
??????????????????? } catch (Exception e) {
??????????????????????? e.printStackTrace();
??????????????????? }
??????????????? }
??????????? }
??????? });
??? }
}

總結

以上是生活随笔為你收集整理的java版QQ 欢迎点评的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。