Test.java
import javax.swing.JFrame;
public class Test extends JFrame{
public static void main(String args[]) {
JianTing jt=new JianTing();
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.*;
public class JianTing extends Chuangkou implements ActionListener{
public JianTing() {
exit.addActionListener(this);
update.addActionListener(this);
information.addActionListener(this);
register.addActionListener(this);
checkinfo.addActionListener(this);
color.addActionListener(this);
center.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {//处理不同的监听事件
if(e.getActionCommand()=="Exit") {
this.dispose();
}
if(e.getActionCommand()=="Update") {
this.aboutUpdate();
}
if(e.getActionCommand()=="Information") {
this.aboutInfo();
}
if(e.getActionCommand()=="Register") {
try {
register();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getActionCommand()=="Check Info") {
checkInfoEmail();
}
if(e.getActionCommand()==" Color ") {
this.colorChange();
}
if(e.getActionCommand()==" Center ") {
centerSet();
}
}
public void aboutInfo() {
JOptionPane.showMessageDialog(null,"author:Guo Minghui; date:2018-12-15","Information",JOptionPane.PLAIN_MESSAGE);
}
public void aboutUpdate() {
JOptionPane.showConfirmDialog(null, "update?", "choose one", JOptionPane.YES_NO_OPTION);
}
public void register() throws Exception {
if(checkInfoEmail()==true) {
WriteToFile(email.getText(),password.getText());
JOptionPane.showMessageDialog(null,"Successful register!","提示消息",JOptionPane.PLAIN_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"Failed register!","提示消息",JOptionPane.PLAIN_MESSAGE);
}
}
public void WriteToFile(String str1,String str2) throws Exception {
FileWriter fw = new FileWriter("D:\\JianTing.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(str1+"\r\n");
bw.write(str2+"\r\n");
bw.close();
fw.close();
}
public boolean checkInfoEmail()
{// 验证邮箱的正则表达式
String format = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";
//p{Alpha}:内容是必选的,和字母字符[\p{Lower}\p{Upper}]等价。如:200896@163.com不是合法的。
//w{2,15}: 2~15个[a-zA-Z_0-9]字符;w{}内容是必选的。 如:dyh@152.com是合法的。
//[a-z0-9]{3,}:至少三个[a-z0-9]字符,[]内的是必选的;如:dyh200896@16.com是不合法的。
//[.]:'.'号时必选的; 如:dyh200896@163com是不合法的。
//p{Lower}{2,}小写字母,两个以上。如:dyh200896@163.c是不合法的。
String mima="^[0-9]{4,4}$";
if ((email.getText()).matches(format)&&(password.getText()).matches(mima)){
status.setText("It is legal !");// 邮箱名合法,status显示合法
return true;
}
else
{
status.setText("It is illegal !");// 邮箱名不合法,status显示不合法
return false;
}
}
public void colorChange() {
if (this.getContentPane().getBackground()==Color.WHITE){
this.getContentPane().setBackground(Color.BLUE);
}
else {
this.getContentPane().setBackground(Color.WHITE);
}
}
public void centerSet(){
this.setLocationRelativeTo(null);//位于中间
}
}
//�����ɫ���ر仯��center���м�,�߿���ɫ��
/*
check�ж�������˺������Ƿ���ȷ
color��һ�»�һ����ɫ
center��һ���ô��ھ���
status��ʾ����λ�õ���Ϣ
check�ж��˺Ÿ�ʽ�Ƿ���ȷ�������Ƿ�����λ
about�����µĴ���
*/
//ʹ��Grid BagLayout����
import javax.swing.*;
import java.awt.*;
public class Chuangkou extends JFrame{
public JMenu file;
public JMenu about;
public JPanel blank2;
public JButton checkinfo;
public JButton color;
public JButton center;
public JButton register;
public JTextField email;
public JPasswordField password;
public JTextField status;
public JTextArea blank3;
public JMenuBar br;
public JMenuItem exit;
public JMenuItem update;
public JMenuItem information;
public Chuangkou() {
init();
this.setTitle("Hello World");
this.pack();
this.setSize(377,230);
this.setVisible(true);
// this.setBackground(Color.BLUE);//���ñ���ɫΪ��ɫ
// this.getContentPane().setVisible(true);
this.getContentPane().setBackground(Color.WHITE);
// this.setLocationRelativeTo(null);//λ���м�
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
file=new JMenu("File");
about=new JMenu("About");
blank2=new JPanel();
email=new JTextField(15);
password=new JPasswordField(15);
register=new JButton("Register");
checkinfo=new JButton("Check Info");
color=new JButton(" Color ");
center=new JButton(" Center ");
status=new JTextField("stutas");
blank3=new JTextArea();
br=new JMenuBar();
exit=new JMenuItem("Exit");
update=new JMenuItem("Update");
information=new JMenuItem("Information");
GridBagLayout layout=new GridBagLayout();
this.setLayout(layout);
br.add(file);
br.add(about);
file.add(exit);
about.add(update);
about.add(information);
this.setJMenuBar(br);
this.add(blank2);
this.add(new JLabel("Email"));
this.add(email);
this.add(checkinfo);
this.add(new JLabel("Password"));
this.add(password);
this.add(color);
this.add(register);
this.add(center);
this.add(blank3);
this.add(status);
GridBagConstraints s=new GridBagConstraints();
s.fill=GridBagConstraints.NONE;
s.gridwidth=0;
s.weightx=0;
s.weighty=0.6;
layout.setConstraints(blank2, s);
s.gridwidth=3;
s.weightx=0;
s.weighty=0;
layout.setConstraints(new JLabel("Email"), s);
s.gridwidth=2;
s.weightx=0;
s.weighty=0;
layout.setConstraints(email, s);
s.gridwidth=0;
s.weightx=0;
s.weighty=0;
s.insets=new Insets(0,10,0,0);
layout.setConstraints(checkinfo, s);
s.gridwidth=3;
s.weightx=0;
s.weighty=0;
layout.setConstraints(new JLabel("Password"), s);
s.gridwidth=2;
s.weightx=0;
s.weighty=0;
s.insets=new Insets(0,1,0,0);
layout.setConstraints(password, s);
s.gridwidth=0;
s.weightx=0;
s.weighty=0;
s.insets=new Insets(0,10,0,0);
layout.setConstraints(color, s);
s.gridwidth=3;
s.weightx=0;
s.weighty=0;
layout.setConstraints(register, s);
s.gridwidth=0;
s.weightx=0;
s.weighty=0;
s.insets=new Insets(0,10,0,0);
layout.setConstraints(center, s);
s.gridwidth=0;
s.weightx=0;
s.weighty=1;
s.fill=GridBagConstraints.VERTICAL;
layout.setConstraints(blank3, s);
s.gridwidth=0;
s.weightx=1;
s.weighty=0;
s.anchor = GridBagConstraints.SOUTH;
s.insets=new Insets(0,0,0,0);
s.fill=GridBagConstraints.HORIZONTAL;
layout.setConstraints(status, s);
}
}