非常简陋的聊天界面代码

来源:互联网 发布:php xss csrf 编辑:程序博客网 时间:2024/05/22 06:44

QQlogin.java:


package chapter2;


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class QQlogin extends JFrame implements ActionListener{


JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JPanel p4=new JPanel();

JPanel p5=new JPanel();

// 添加组件

JLabel name=new JLabel("用户名");

JLabel pass=new JLabel("密码");

JTextField namet=new JTextField();

JPasswordField passt=new JPasswordField();

JButton login=new JButton("登陆");

JButton reg=new JButton("注册");

JButton can=new JButton("取消");


QQlogin(){

this.setSize(400, 250);

// 布局管理

this.setLayout(new BorderLayout());

p1.setLayout(new GridLayout(2,1));

p2.setLayout(new FlowLayout());

p3.setLayout(new GridLayout(1,3));

p4.setLayout(new GridLayout(1,3));

// 生成界面

this.add(p1,BorderLayout.CENTER);

this.add(p2,BorderLayout.SOUTH);

this.add(p5,BorderLayout.NORTH);

p1.add(p3);

p1.add(p4);

p2.add(login);

p2.add(reg);

p2.add(can);

p3.add(name);

p3.add(namet);

p4.add(pass);

p4.add(passt);

login.addActionListener(this);

reg.addActionListener(this);

can.addActionListener(this);

this.setVisible(true);

this.setResizable(false);

}

public static void main(String[] args) {

QQlogin lg=new QQlogin();

}


@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getActionCommand().equals("登陆")){

if(namet.getText().equals("aaa")&&passt.getText().equals("111")){

this.setVisible(false);

QQMain qm=new QQMain();

}

}

}


}

package chapter2;


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class QQlogin extends JFrame implements ActionListener{


JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JPanel p4=new JPanel();

JPanel p5=new JPanel();

// 添加组件

JLabel name=new JLabel("用户名");

JLabel pass=new JLabel("密码");

JTextField namet=new JTextField();

JPasswordField passt=new JPasswordField();

JButton login=new JButton("登陆");

JButton reg=new JButton("注册");

JButton can=new JButton("取消");


QQlogin(){

this.setSize(400, 250);

// 布局管理

this.setLayout(new BorderLayout());

p1.setLayout(new GridLayout(2,1));

p2.setLayout(new FlowLayout());

p3.setLayout(new GridLayout(1,3));

p4.setLayout(new GridLayout(1,3));

// 生成界面

this.add(p1,BorderLayout.CENTER);

this.add(p2,BorderLayout.SOUTH);

this.add(p5,BorderLayout.NORTH);

p1.add(p3);

p1.add(p4);

p2.add(login);

p2.add(reg);

p2.add(can);

p3.add(name);

p3.add(namet);

p4.add(pass);

p4.add(passt);

login.addActionListener(this);

reg.addActionListener(this);

can.addActionListener(this);

this.setVisible(true);

this.setResizable(false);

}

public static void main(String[] args) {

QQlogin lg=new QQlogin();

}


@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getActionCommand().equals("登陆")){

if(namet.getText().equals("aaa")&&passt.getText().equals("111")){

this.setVisible(false);//验证成功的话,登陆界面消失

QQMain qm=new QQMain();// 新建QQMain对象,同时主界面出现。跳转到QQMain.java

}

}

}


}


QQMain.java:

package chapter2;


import javax.swing.*;


import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.*;


public class QQMain extends JFrame implements ActionListener{

// 组件

JTextField message=new JTextField();

JComboBox list=new JComboBox();

JButton send =new JButton("send");

JTextArea record =new JTextArea();

JScrollPane jsp=new JScrollPane(record);

File f=new File("E:\\record.txt");

FileReader fr;


FileWriter fw;


BufferedReader br;

PrintWriter pw;

QQMain(){

record.setEditable(false);

this.setSize(400, 500);

// 面板

JPanel p1=new JPanel();

JPanel p2=new JPanel();

jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

// 布局

this.setLayout(new BorderLayout());

p1.setLayout(new BorderLayout());

p2.setLayout(new GridLayout(1,2));

this.add(p1,BorderLayout.NORTH);

this.add(jsp,BorderLayout.CENTER);


p1.add(message,BorderLayout.CENTER);

p1.add(p2,BorderLayout.SOUTH);

p2.add(list);

p2.add(send);

//要求一登陆就显示聊天记录,因此,这部分放在了构造函数里。

try {

fr=new FileReader(f);

br=new BufferedReader(fr);

while(br.ready())

record.append(br.readLine()+"\n");//有“\n”是为了,每读一条记录,就换行;append为追加模式,此处如果为setText()会

} catch (Exception e) {             覆盖前边的聊天记录

// e.printStackTrace();

try {

f.createNewFile();//如果聊天记录文件不存在,则创建聊天记录文件record.txt

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

send.addActionListener(this);

this.setVisible(true);

}

public static void main(String args[]){

}


public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("send")){

record.append(message.getText()+"\n");

try {

fw=new FileWriter(f, true);// 此处,在创建FileWriter时用中在的构造函数,第二个参数为true,会追加写入,而不是覆盖

pw=new PrintWriter(fw);       写入。 

pw.println(message.getText());

pw.close();// 此处,pw.close()会调用flush()函数,将缓冲区PrinteWriter 中未满的内容刷新,写入record.txt,避免

             文件而失。

} catch (Exception e2) {

}

message.setText("");


}

}


}





0 0