非常简陋的聊天界面代码
来源:互联网 发布: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("");
}
}
}
- 非常简陋的聊天界面代码
- 简陋聊天
- 一个非常简陋的域名注册辅助工具
- 一个比较简陋的命令行socket聊天程序
- 聊天界面关键代码实现
- share中简单聊天界面的实现<代码>
- share中简单聊天界面的实现<代码>
- 第一行代码3.7.2编写精美的聊天界面
- 自制简陋在线聊天系统
- 【练习代码】试用百度BAE和练手jetty continuation的副成品 —— 一个简陋的WEB版LBS聊天应用
- 界面化的聊天
- 精美的聊天界面
- 非常简单的Socket聊天程序
- listview的一个聊天界面
- 聊天界面cell的搭建
- 编写精美的聊天界面
- 编写简单的聊天界面
- #解决仿微信聊天界面键盘遮盖聊天的界面
- xml序列化
- 深度解析:相比京东亚马逊“重资产”特征,阿里更加“互联网化”
- C++Primer学习笔记(使用类,友元,类转换)
- xmlpull读取xml文件
- BigDecimal.setScale 处理java小数点 .
- 非常简陋的聊天界面代码
- 从Hello World到Hello World
- 【LeetCode笔记】Regular Expression Matching
- AFNetwork上传图片
- 获取文件MD5,SHA1校验码
- 算法复杂度的估计,以及常见评测结果
- 上海充气城堡翻倒致13名儿童受伤 或因广场风大
- Help him (BestCoder Round #12) HDU 5059
- 【iphone应用开发】UILabel自适应大小