javaGridBagLayout

 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);
			
	}
		
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟火里的尘埃.

有问题可以留言

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值