javaGui--textField 、label 、button 组件

如题,本篇我们介绍下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);
          
         
    }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值