如题,本篇我们介绍下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 !!
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1