⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppqqframe.java

📁 利用JAVA编写的类似QQ的程序代码,基本实现了QQ的基本功能,界面并非QQ的样子,只是做为教学和大家研究使用
💻 JAVA
字号:
/**
 * @(#)TJPasswordField.java
 *
 * Sample Applet application
 *
 * @author 
 * @version 1.00 08/06/14
 */
 //现在就是好友列表那块,可以增加一个好友,并把它存在haomaA.txt中。并显示在item里。还要可以查看好友资料。最后就是网络问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.text.*;
import java.io.*;
import java.net.*;

//主类,登陆界面
class TJPasswordField extends JFrame implements ActionListener
{
  JLabel jl1,jl2;
  JButton jb,jb1,jb2;
  JTextField jp1;
  JPasswordField jp2;
   JButton commit,cancel,jb3;
   readFile rdt;
   String str3,str4;
   File f;
  MyDialogFrame md; 
  Zhuce zc;
 // haoyou hy;

   TJPasswordField()
   {
   	super("登陆界面");
   	super.setSize(400,400);
     Container c= getContentPane();
     jl1 = new JLabel("帐号:");
     jl2 = new JLabel("密码:");
     jb = new JButton("注册新帐号");
     jb3 = new JButton("退出");
     jp1 = new JTextField(10); 
     jp2 = new JPasswordField(10);
     commit =  new JButton("登录");
      cancel =  new JButton("重新输入");
      c.setLayout(new GridLayout(4,2,10,10));
         c.add(jl1);
         c.add(jp1);
         c.add(jl2);
         c.add(jp2);
         c.add(commit);
         c.add(cancel);
         c.add(jb);
         c.add(jb3);
         c.setSize(5,25);

         commit.addActionListener(this);
         cancel.addActionListener(this);
         jb.addActionListener(this);
         jb3.addActionListener(this);
         
         rdt = new readFile();
   }

  public void actionPerformed(ActionEvent e)
  {
  	String thisLinen,retn = "";
    if(e.getSource() == commit)
    {
    	 String str1 = new String(jp1.getText());
    	 String str2 = new String(jp2.getText());
    	 boolean bool = rdt.readFile(str1+str2,"haha.txt");
    	 if(bool){    	
    	      md = new MyDialogFrame(str1);   
    	 }
    	 else{
    	 	JOptionPane jop = new JOptionPane();
    	 	jop.showMessageDialog(null,"密码错误请重新输入!","result",jop.ERROR_MESSAGE);
    	 e.setSource(cancel);
    	 }      
    }
    if(e.getSource() == cancel)
    {
      jp1.setText("");
      jp2.setText("");
    }
    if(e.getSource() == jb)
    {
    	zc = new Zhuce();
    
    }
    if(e.getSource() == jb3)
    {
        this.hide();
        System.exit(0);
    }
    
  }
}
//注册对话框
class Zhuce extends JFrame implements ActionListener
{
	JTextField jpa,jpb;
	JTextArea jaz;
	JButton jb,jb1,jb2;
	String str3,str4,str5;
	 File f,ff;
	 readFile rfz;
	 writeFile wfz;
	Zhuce()
	{
		super("注册");
		super.setSize(400,400);
		Container cz = getContentPane();
		cz.setLayout(null);
		cz.setBounds(0,0,400,400);
		cz.setBackground(Color.cyan);
		JLabel jla = new JLabel("请输入号码");
		jla.setBounds(0,0,70,30);
    	jpa = new JTextField(10);
    	jpa.setBounds(90,0,300,30);
    	JLabel jlb = new JLabel("请输入密码");
    	jlb.setBounds(0,35,70,40);
    	jpb = new JTextField(10);
    	jpb.setBounds(90,35,300,40);
    	JLabel jlz = new JLabel("请输入个人资料");
    	jlz.setBounds(0,70,70,75);
    	jaz = new JTextArea(10,10);
    	jaz.setBounds(90,85,300,180);	
    	jb1 =new JButton("确认");
    	jb1.setBounds(90,270,70,50);
    	jb2 =new JButton("返回");
    	jb2.setBounds(180,270,70,50);
    	cz.add(jla);
    	cz.add(jlb);
    	cz.add(jpa);
    	cz.add(jpb);
    	cz.add(jlz);
    	cz.add(jaz);
    	cz.add(jb1);
    	cz.add(jb2);
    	
    	jb1.addActionListener(this);
    	jb2.addActionListener(this);
    //	pack();
    	show();
    	rfz = new readFile();
    	wfz = new writeFile();
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == jb1)
    {
    	JOptionPane jop = new JOptionPane();
    	str3 = new String(jpa.getText());
    	str4 = new String(jpb.getText());
    	str5 = new String(jaz.getText());
    	
    	if(str3.compareTo("") == 0)
    	jop.showMessageDialog(null,"请输入想申请的号码!","result",jop.ERROR_MESSAGE);
    	else if(str4.compareTo("") == 0)
    	jop.showMessageDialog(null,"请输入对应的密码!","result",jop.ERROR_MESSAGE);
    	else
    	{
    		if(rfz.readFile(str3+str4,"haha.txt"))
    		jop.showMessageDialog(null,"该号码已经存在!","result",jop.ERROR_MESSAGE);
    		else {
    		wfz.writeFile(str3+str4+"\n","haha.txt");
    		f= new File(str3+".txt"); 
    		ff= new File(str3+"A.txt");  	      
    	    try{
    	    	f.createNewFile();
    	    	ff.createNewFile();	
    	    }
    	    catch(IOException io){System.out.println(f.getPath());System.out.println(ff.getPath());} 
    	    wfz.writeFile(str5,f.getName());
    	   wfz.writeFile("",f.getName());
    		jop.showMessageDialog(null,"注册成功!","result",jop.PLAIN_MESSAGE);}
    	}	 
    }
    if(e.getSource() == jb2)
    {
    	dispose();
    }
	}
}

//登录成功的界面
class MyDialogFrame extends JFrame 
	implements ActionListener
{
	JButton jb11,jb12,jb13,jb14,jlm;
	Haoyou js;
	FileFrame ff;
	JTextField jtm;
	Ziliao zl;
	MenuBar j;
	String strm;
	readFile rfm;
	writeFile wfm;
	MyDialogFrame(String str)
	{
		super(str+"在线");
		super.setSize(430,500);
		
		Container cm = getContentPane();
		cm.setLayout(null);
		cm.setBounds(0,0,430,500);
		cm.setBackground(Color.pink);
		
		jb11 = new JButton("自己的资料");
		jb11.setBounds(0,0,100,70);
		jb13 = new JButton("打开文件");
		jb13.setBounds(0,80,100,70);
		jb12 = new JButton("退出登录");
		jb12.setBounds(0,160,100,70);
		js = new Haoyou(str);
		js.setBounds(110,50,200,400);
	//	js.setBackground(Color.green);
		jlm = new JButton("添加好友");
		jlm.setBounds(320,0,100,50);
		jtm = new JTextField();
		jtm.setBounds(110,0,200,50);
	
	add(jb11);
	add(jb13);
		add(jb12);
	add(js);
	add(jlm);
	add(jtm);

		this.addWindowListener(new WinAdpt());
		jb11.addActionListener(this);
		jb12.addActionListener(this);
		jb13.addActionListener(this);
		jlm.addActionListener(this);
	
		//this.getContentPane().setBackground(Color.orange);
		setVisible(true);		
		strm = new String(str);
		rfm = new readFile();
		wfm = new writeFile();
	}

	public void actionPerformed(ActionEvent e)
	
	{
		if(e.getSource() == jb11){
			zl = new Ziliao(strm);
		
		}
		else if(e.getSource() == jb13)
		{
			ff = new FileFrame();	
		}	
		else if(e.getSource() == jb12)
		{
			super.dispose();
		}
		else if(e.getSource() == jlm)
		{
			//从文件中读取
			String strstr = new String(jtm.getText());
			int mm = js.hy.getItemCount();
			if(rfm.readFile(strstr,"haha.txt")){
				if(!rfm.readFile(strstr,strm+"A.txt"))
				{
					js.hy.addItem(strstr);
			   		 wfm.writeFile(strstr,strm+"A.txt");
				}
				else
				{
					JOptionPane jop = new JOptionPane();
				jop.showMessageDialog(null,"你已经拥有该好友!","result",jop.ERROR_MESSAGE);
				}
			    
			}
			else
			{
				JOptionPane jop = new JOptionPane();
				jop.showMessageDialog(null,"该用户不存在!","result",jop.ERROR_MESSAGE);
			}
			
		}
	
	}
}

//个人资料对话框
class Ziliao extends JFrame implements ActionListener
{
	JLabel jl11,jl12,jl13,jt12;                  
	JTextArea ja11;
	JButton jbz,jbz1;
	readFile rfz;
	writeFile wfz;
	String strz;
	Ziliao(String string)
	{
		super(string);
		Container c = this.getContentPane();
		c.setLayout(null);
		c.setBounds(0,0,200,240);
		c.setBackground(Color.cyan);
		jl12 = new JLabel("qq号码");
		jl12.setBounds(5,5,50,40);
		jl13 = new JLabel("资料");
		jl13.setBounds(5,55,50,40);
		jt12 = new JLabel(string);
		jt12.setBounds(70,5,100,40);
		ja11 = new JTextArea(10,5);
		ja11.setBounds(70,55,100,100);
		jbz = new JButton("返回");
		jbz.setBounds(5,160,70,40);
		jbz1 = new JButton("编辑保存");
		jbz1.setBounds(100,160,70,40);
		add(jl12);
		add(jt12);
		add(jl13);
		add(ja11);
		add(jbz1);
		add(jbz);
		jbz.addActionListener(this);
		jbz1.addActionListener(this);
		setSize(200,240);
		rfz = new readFile();
		ja11.setText(rfz.readFile(string+".txt"));
		strz = new String(string);
		show();
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == jbz)
		{super.dispose();
		}
		else if(e.getSource() == jbz1)
		{
			wfz = new writeFile();
			wfz.fugaiFile(ja11.getText(),strz+".txt");
		}
	}
}

//打开文件的对话框
class FileFrame extends JFrame implements ActionListener
{
  JTextArea ta;
  JButton open, quit;
  FileDialog fd;

  FileFrame()
  {
    super("Acquire and display text");
    ta = new JTextArea(20,20);
    open = new JButton("Open");
    quit = new JButton("Close");
    open.addActionListener(this);
    quit.addActionListener(this);
    this.addWindowListener(new WinAdpt());
    setLayout(new FlowLayout());
    add(ta);
    add(open);
    add(quit);
    setSize(350,280);
    pack();
    show();
  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getActionCommand()=="Open")
    {
      fd = new FileDialog(this,"Open File", FileDialog.LOAD);
     // fd.setDirectory("e:\\temp");
      fd.show();
      try{
        File myfile = new File(fd.getDirectory(), fd.getFile());
        RandomAccessFile raf = new RandomAccessFile(myfile,"r");
        while(raf.getFilePointer()<raf.length())
        {
          ta.append(raf.readLine() + "\n");
        }
      }
      catch(IOException ioe)
      {
        System.err.println(ioe.toString());
      }
    }
    if(e.getActionCommand()=="Close")
    {
      dispose();
     // System.exit(0);
    }
  }
}

//好友列表
class Haoyou extends ScrollPane implements ActionListener
{
	List hy;
	Duihua d;
	String thisLine = null;
	Haoyou(String str)
	{
		super(ScrollPane.SCROLLBARS_AS_NEEDED);
		hy = new List();
		hy.setBackground(Color.green);
		
		try {
       FileInputStream fin =  new FileInputStream(str+"A.txt");
       BufferedReader myInput = new BufferedReader
                         (new InputStreamReader(fin));
      while ((thisLine = myInput.readLine()) != null) {  
         hy.addItem(thisLine);
       }
     } catch (Exception e) {
       System.out.println("Cannot load, exception!");
     }
		hy.addActionListener(this);
		super.add(hy);
		super.setSize(100,100);
	}
	public void actionPerformed(ActionEvent e)
	{
		int i = 0;
		int ii = hy.getItemCount();
		for(i=0;i<ii;i++)
		{
		if(e.getActionCommand() == hy.getItem(i))
		break;
		}
		d = new Duihua(hy.getItem(i));
	
	}
	
}

//双击好友弹出对话框
class Duihua extends JFrame
{
	JTextArea jtd;
	JButton jbd,jbd1;
	Duihua(String strd)
	{
		super(strd);
		super.setSize(300,400);
		Container c = this.getContentPane();
		c.setLayout(null);
		c.setBounds(0,0,300,400);
		c.setBackground(Color.pink);
		jtd = new JTextArea();
		jtd.setBounds(10,10,200,200);
		jbd = new JButton("send");
		jbd.setBounds(10,230,80,60);
		
		c.add(jtd);
		c.add(jbd);
		this.setVisible(true);
		
	}
}

class WinAdpt extends WindowAdapter
{
	public void windowClosing(WindowEvent e)
	{
		((JFrame)e.getWindow()).dispose();
	}
}

class readFile {
/*	String xinxi;
	String fn;
	readFile()*/
public boolean readFile(String xinxi,String fn) {
     String thisLine = null;
     try {
       FileInputStream fin =  new FileInputStream(fn);
       BufferedReader myInput = new BufferedReader
                         (new InputStreamReader(fin));
      while ((thisLine = myInput.readLine()) != null) {  
         if(thisLine.compareTo(xinxi) == 0)
         break;
       }
       
     } catch (Exception e) {
       System.out.println("Cannot load, exception!");
     }
    if(thisLine == null)
       return false;
       else 
       return true;
   }
   
public String readFile(String fn) {
     String thisLine, ret = "";
     try {
       FileInputStream fin =  new FileInputStream(fn);
       BufferedReader myInput = new BufferedReader
                         (new InputStreamReader(fin));
      while ((thisLine = myInput.readLine()) != null) {  
         ret += thisLine + "\n";
       }
     } catch (Exception e) {
       ret = "Cannot load, exception!";
     }
     return ret;
   }
}

class writeFile{
	public void writeFile(String xinxi,String fn) {
     try {
       RandomAccessFile fo =  new RandomAccessFile(fn,"rw");
       
       String xinxinew = xinxi+"\n";
       fo.seek(fo.length());
       fo.write(xinxinew.getBytes());
       
     } catch (Exception e) {
       System.out.println("Cannot load, exception!");
     }
   }
   public void fugaiFile(String xinxi,String fn) {
     try {
       FileOutputStream fin =  new FileOutputStream(fn);
       fin.write(xinxi.getBytes());
     } catch (Exception e) {
       System.out.println("Cannot load, exception!");
     }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -