Java編寫山寨QQ之好友界面

    由於是新手,水平有限,項目的開發是邊學邊做。

    參考的視頻:

    

    但並不是完全照搬,裏面也加了自己的一些想法。

    

    好友界面參照的依然是QQ6.3版本:

    

    

    我做出來的界面:

    

    有了前面的登陸界面的經驗,現在寫起好友界面來速度快多了,而且遇到的問題也少了許多。


    同樣,本部分的編寫又遇到了那些問題呢?

    Q1:我添加背景不想用piant方法,還有沒有其他的方法?

    A:查資料如下:

JFrame設置背景圖片

        JFrame是由這麼幾部分組成:最底下一層JRootPane,上面是glassPane(一個JPanel)layeredPane(一個JLayeredPane),而layeredPane又由contentPane(一JPanel)menuBar構成。

        我們一般在JFrame上添加組件往往都是加在contentPane上面:

 

         frame.getContentPane().add(btn);

 

         要在JFrame上添加背景圖片,常見做法是加在layeredPane上面,並將contentPane設置成透明的即可。

    

    因以我這樣來實現:

    1、定義屬性。

	//背景
	ImageIcon background;
	JLabel imgLabel;
	JPanel buttom;
   2、初始化,調用構造背景的方法。

	//處理背景
	String bg = "Images/chatPanel.jpg";
	backGround(bg);
    3、backGround方法體。

        //處理背景方法
	public void backGround(String link)
	{
		background = new ImageIcon(link);
		imgLabel = new JLabel(background);
		imgLabel.setBounds(0, 0,  background.getIconWidth(), background.getIconHeight());
		buttom=(JPanel)this.getContentPane();
		//將contentPane設置爲透明的
		buttom.setOpaque(false);
		this.getLayeredPane().add(imgLabel , new Integer(Integer.MIN_VALUE));
	}
    通過以上個部分就可以實現JFrame的背景設置。

    Q2:JScrollPane和JPanel用法

    本例中是在JScrollPane中放置JPanel,之前我一直搞反了。

    JPanel設置佈局,JSP不需用佈局。


代碼:

package com.qq.client.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

public class FriendView extends JFrame implements ActionListener,MouseListener{
	//背景圖層
	ImageIcon background;
	JPanel buttom;
	JLabel imgLabel;
	JButton jb;
	
	//上層北邊
	JLabel head,name,sign;
	
	//上層南邊
	JPanel jp;//卡片佈局
	CardLayout cl;
	
	//第一張卡片
	JPanel jp1;
	JButton jp1_jb1,jp1_jb2,jp1_jb3;
	
	//第二張卡片
	JPanel jp2;
	JScrollPane jsp;
	JPanel jp_jsp;//用來放jsp
	JButton jp2_jb1,jp2_jb2,jp2_jb3;
	
	//第三張卡片
	JPanel jp3;
	JScrollPane jsp2;
	JPanel jp_jsp2;//用來放jsp2
	JButton jp3_jb1,jp3_jb2,jp3_jb3;

	//第四張卡片
	JPanel jp4;
	JScrollPane jsp3;
	JPanel jp_jsp3;
	JButton jp4_jb1,jp4_jb2,jp4_jb3;

	
	//構造函數
	public FriendView()
	{
		//處理背景
		backGround();
		
		//處理北邊的東西(頭像,暱稱,簽名)
		head = new JLabel(new ImageIcon("Images/qqhead.jpg"));
		head.setBounds(10, 40, 50, 50);
		name = new JLabel("小Q機器人");
		name.setBounds(70, 42, 80, 20);
		name.setFont(new Font("宋體",Font.BOLD, 16));
		name.setForeground(Color.white);
		sign = new JLabel("個性簽名");
		sign.setBounds(70, 70, 80, 20);
		sign.setForeground(Color.white);
		
		//設置好友列表爲卡片佈局
		cl = new CardLayout();
		jp = new JPanel();
		jp.setOpaque(false);
		jp.setBounds(0,	205, background.getIconWidth(), background.getIconHeight());
		
		//處理第一張卡片
		firstCard();	
		//處理第二張卡片
		secondCard();	
		//處理第三張卡片
		thirdCard();
		//處理第四張卡片
		fourthCard();
		
		this.add(head);
		this.add(name);
		this.add(sign);
		jp.setLayout(cl);
		jp.add(jp1,"1");
		jp.add(jp2,"2");
		jp.add(jp3,"3");
		jp.add(jp4,"4");
		this.add(jp);
		this.getLayeredPane().setLayout(null);
		this.setLayout(null);
		this.setSize(283, 720);
		this.setLocation(800, 30);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	}
	
	//處理背景方法
	public void backGround()
	{
		background = new ImageIcon("Images/bg.jpg");
		imgLabel = new JLabel(background);
		imgLabel.setBounds(0, 0,  background.getIconWidth(), background.getIconHeight());
		buttom=(JPanel)this.getContentPane();
		//將contentPane設置爲透明的
		buttom.setOpaque(false);
		this.getLayeredPane().add(imgLabel , new Integer(Integer.MIN_VALUE));
	}
	
	//處理第一張卡片方法
	public void firstCard()
	{
		jp1 = new JPanel();
		
		jp1_jb1 = new JButton("> 我的好友");
		jp1_jb1.addActionListener(this);
		jp1_jb1.setLayout(null);
		jp1_jb1.setSize(277, 35);
		jp1_jb1.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp1_jb2 = new JButton("> 陌生人");
		jp1_jb2.addActionListener(this);
		jp1_jb2.setLayout(null);
		jp1_jb2.setBounds(0, 35, 277, 35);
		jp1_jb2.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp1_jb3 = new JButton("> 黑名單");
		jp1_jb3.addActionListener(this);
		jp1_jb3.setLayout(null);
		jp1_jb3.setBounds(0, 70, 277, 35);
		jp1_jb3.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp1.add(jp1_jb1);
		jp1.add(jp1_jb2);
		jp1.add(jp1_jb3);
		jp1.setLayout(null);
		jp1.setOpaque(false);
	}
	
	//處理第二張卡片方法
	public void secondCard()
	{
		jp2 = new JPanel();
		
		jp2_jb1 = new JButton("↓ 我的好友");
		jp2_jb1.addActionListener(this);
		jp2_jb1.setLayout(null);
		jp2_jb1.setSize(277, 35);
		jp2_jb1.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp2_jb2 = new JButton("> 陌生人");
		jp2_jb2.addActionListener(this);
		jp2_jb2.setLayout(null);
		jp2_jb2.setBounds(0, 354, 277, 35);
		jp2_jb2.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp2_jb3 = new JButton("> 黑名單");
		jp2_jb3.addActionListener(this);
		jp2_jb3.setLayout(null);
		jp2_jb3.setBounds(0, 389, 277, 35);
		jp2_jb3.setHorizontalAlignment(SwingConstants.LEFT );
		
		//假定30個好友
		jp_jsp = new JPanel(new GridLayout(30,1));
		jsp = new JScrollPane(jp_jsp);
		
		//初始化30個好友
		JLabel[] jbls = new JLabel[30];
		for(int i=0; i<jbls.length; i++)
		{
			jbls[i] = new JLabel(i+1+"號機器人", new ImageIcon("Images/qqhead.jpg"), JLabel.LEFT);
			jbls[i].addMouseListener(this);
			jp_jsp.add(jbls[i]);
		}

		jsp.setBounds(1, 35, 275, 319);
		
		//jsp.setLayout(null);錯誤!,jsp本來就沒有佈局
		jp2.add(jsp);
		jp2.add(jp2_jb1);
		jp2.add(jp2_jb2);
		jp2.add(jp2_jb3);
		jp2.setLayout(null);
		jp2.setOpaque(false);
	}
	
	//處理第三張卡片方法
	public void thirdCard()
	{
		jp3 = new JPanel();
		
		jp3_jb1 = new JButton("> 我的好友");
		jp3_jb1.addActionListener(this);
		jp3_jb1.setLayout(null);
		jp3_jb1.setSize(277, 35);
		jp3_jb1.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp3_jb2 = new JButton("↓ 陌生人");
		jp3_jb2.addActionListener(this);
		jp3_jb2.setLayout(null);
		jp3_jb2.setBounds(0, 35, 277, 35);
		jp3_jb2.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp3_jb3 = new JButton("> 黑名單");
		jp3_jb3.addActionListener(this);
		jp3_jb3.setLayout(null);
		jp3_jb3.setBounds(0, 389, 277, 35);
		jp3_jb3.setHorizontalAlignment(SwingConstants.LEFT );
		
		//假定30個好友
		jp_jsp2 = new JPanel(new GridLayout(10,1));
		jsp2 = new JScrollPane(jp_jsp2);
		
		//初始化30個好友
		JLabel[] jbls = new JLabel[10];
		for(int i=0; i<jbls.length; i++)
		{
			jbls[i] = new JLabel(i+1+"號陌生人", new ImageIcon("Images/qqhead.jpg"), JLabel.LEFT);
			jbls[i].addMouseListener(this);
			jp_jsp2.add(jbls[i]);
		}

		jsp2.setBounds(1, 70, 275, 319);
		
		jp3.add(jsp2);
		jp3.add(jp3_jb1);
		jp3.add(jp3_jb2);
		jp3.add(jp3_jb3);
		jp3.setLayout(null);
		jp3.setOpaque(false);
	}
	
	//處理第四張卡片方法
	public void fourthCard()
	{
		jp4 = new JPanel();
		
		jp4_jb1 = new JButton("> 我的好友");
		jp4_jb1.addActionListener(this);
		jp4_jb1.setLayout(null);
		jp4_jb1.setSize(277, 35);
		jp4_jb1.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp4_jb2 = new JButton("> 陌生人");
		jp4_jb2.addActionListener(this);
		jp4_jb2.setLayout(null);
		jp4_jb2.setBounds(0, 35, 277, 35);
		jp4_jb2.setHorizontalAlignment(SwingConstants.LEFT );
		
		jp4_jb3 = new JButton("↓ 黑名單");
		jp4_jb3.addActionListener(this);
		jp4_jb3.setLayout(null);
		jp4_jb3.setBounds(0, 70, 277, 35);
		jp4_jb3.setHorizontalAlignment(SwingConstants.LEFT );
		
		//假定30個好友
		jp_jsp3 = new JPanel(new GridLayout(10,1));
		jsp3 = new JScrollPane(jp_jsp3);
		
		//初始化30個好友
		JLabel[] jbls = new JLabel[5];
		for(int i=0; i<jbls.length; i++)
		{
			jbls[i] = new JLabel(i+1+"號黑名單", new ImageIcon("Images/qqhead.jpg"), JLabel.LEFT);
			jbls[i].addMouseListener(this);
			jp_jsp3.add(jbls[i]);
		}

		jsp3.setBounds(1, 105, 275, 319);
		
		jp4.add(jsp3);
		jp4.add(jp4_jb1);
		jp4.add(jp4_jb2);
		jp4.add(jp4_jb3);
		jp4.setLayout(null);
		jp4.setOpaque(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//第一張卡片的按鈕
		if(e.getSource()==jp1_jb1)
		{
			cl.show(jp, "2");;
		}
		if(e.getSource()==jp1_jb2)
		{
			cl.show(jp, "3");;
		}
		if(e.getSource()==jp1_jb3)
		{
			cl.show(jp, "4");;
		}
		
		//第二張卡片的按鈕
		if(e.getSource()==jp2_jb1)
		{
			cl.show(jp, "1");;
		}
		if(e.getSource()==jp2_jb2)
		{
			cl.show(jp, "3");;
		}
		if(e.getSource()==jp2_jb3)
		{
			cl.show(jp, "4");;
		}
	
		//第三張卡片的按鈕
		if(e.getSource()==jp3_jb1)
		{
			cl.show(jp, "2");;
		}
		if(e.getSource()==jp3_jb2)
		{
			cl.show(jp, "1");;
		}
		if(e.getSource()==jp3_jb3)
		{
			cl.show(jp, "4");;
		}
	
		//第四張卡片的按鈕
		if(e.getSource()==jp4_jb1)
		{
			cl.show(jp, "2");;
		}
		if(e.getSource()==jp4_jb2)
		{
			cl.show(jp, "3");;
		}
		if(e.getSource()==jp4_jb3)
		{
			cl.show(jp, "1");;
		}
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(e.getClickCount()==2)
		{
			String str = ((JLabel)e.getSource()).getText();
			System.out.println("你希望和"+str+"聊天。");
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		JLabel jl =(JLabel)e.getSource();
		jl.setForeground(Color.red);
			
	
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		JLabel jl =(JLabel)e.getSource();
		jl.setForeground(Color.black);
	}
	
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章