博客详情

javaGui--textField 、label 、button 组件 (原创)

作者: 朝如青丝暮成雪
发布时间:2018-08-11 16:57:58  文章分类:javaGUI   阅读(954)  评论(0)

如题,本篇我们介绍下javaGui中 文本框、密码框、标签、按钮等组件。


登录窗体


package com.tingcream.javaGui.component;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


/**
 * 登录窗体   
 *  文本框、密码框、按钮、标签、按钮点击事件
 *  
 *  GridLayout+FlowLayout
 *  
 *  用户名  [     ]
 *  密    码  [     ]
 *  [登录]  [重置]
 *  
 *  @author jelly 
 * 
 */
public class _01_textField_label_button extends JFrame {

	
	private static final long serialVersionUID = 1L;
	
	public  JPanel panel1,panel2,panel3;
	public  JTextField textField;
	public  JPasswordField passwordField;
	public  JLabel label1 ,label2 ;
	public  JButton button1 ,button2 ;
	
	
	public static void main(String[] args) {
		new  _01_textField_label_button();
	}
	
	public _01_textField_label_button(){
		 
		
		panel1=new JPanel();
		panel2=new JPanel();
		panel3=new JPanel();
		
		 
		 label1=new JLabel("用户名");
		 label2=new JLabel("密     码");
		 button1=new JButton("登录");
		 button2=new JButton("重置");
		 textField=new JTextField(10);
		 passwordField=new JPasswordField(10);
		
		 
		 
	    panel1.add(label1); 
	    panel1.add(textField);
	    
	    panel2.add(label2);
	    panel2.add(passwordField);
	     
	    panel3.add(button1);
	    panel3.add(button2);
		
	    
	    //点击事件
	    button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			  String username=textField.getText().trim();
			  String password =new String(passwordField.getPassword());
			  if(username.equals("")){
				  JOptionPane.showMessageDialog(null, "用户名不能为空");
				  return ;
			  }
			  if(password.equals("")){
				  JOptionPane.showMessageDialog(null, "密码不能为空");
				  return ;
			  }
			  
			  if(username.equals("admin")&& password.equals("123456")){
				  JOptionPane.showMessageDialog(null, "恭喜,登陆成功!");
			  }else{
				  JOptionPane.showMessageDialog(null, "抱歉,用户名或密码错误!");
			  }
				
			}
		});
		
	    
	    button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
			}
		});
		
		this.setLayout(new GridLayout(3, 1,3,3));//3行1列 网格布局
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
		
		 this.setTitle("用户登录");
		 this.setSize(350,150);
		 this.setLocationRelativeTo(null);
		 this.setResizable(false);
		 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 this.setVisible(true);
		 
		
	}

}



ok !!



关键字:  javaGUI
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号