import javax.swing.JPanel//面板
import javax.swing.JButton//按钮
import javax.swing.JLabel//标签
import javax.swing.JTextField//文本框
import java.awt.Font//字体
import java.awt.Color//颜色
import javax.swing.JPasswordField//密码框
import java.awt.event.ActionListener//事件监听
import java.awt.event.ActionEvent//事件处理
import javax.swing.JOptionPane//消息窗口public class UserLogIn extends JFrame{
public JPanel pnluser
public JLabel lbluserLogIn
public JLabel lbluserName
public JLabel lbluserPWD
public JTextField txtName
public JPasswordField pwdPwd
public JButton btnSub
public JButton btnReset
public UserLogIn(){
pnluser = new JPanel()
lbluserLogIn = new JLabel()
lbluserName = new JLabel()
lbluserPWD = new JLabel()
txtName = new JTextField()
pwdPwd = new JPasswordField()
btnSub = new JButton()
btnReset = new JButton()
userInit()
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置关闭框架的同时结束程序
this.setSize(300,200)//设置框架大小为长300,宽200
this.setResizable(false)//设置框架不可以改变大小
this.setTitle("用户登录")//设置框架标题
this.pnluser.setLayout(null)//设置面板布局管理
this.pnluser.setBackground(Color.cyan)//设置面板背景颜色
this.lbluserLogIn.setText("用户登录")//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14))//设置标签字体
this.lbluserLogIn.setForeground(Color.RED)//设置标签字体颜色
this.lbluserName.setText("用户名:")
this.lbluserPWD.setText("密 码:")
this.btnSub.setText("登录")
this.btnReset.setText("重置")
this.lbluserLogIn.setBounds(120,15,60,20)//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20)
this.lbluserPWD.setBounds(50,85,60,25)
this.txtName.setBounds(110,55,120,20)
this.pwdPwd.setBounds(110,85,120,20)
this.btnSub.setBounds(85,120,60,20)
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e)
}
}
)
this.btnReset.setBounds(155,120,60,20)
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e)
}
}
)
this.pnluser.add(lbluserLogIn)//加载标签到面板
this.pnluser.add(lbluserName)
this.pnluser.add(lbluserPWD)
this.pnluser.add(txtName)
this.pnluser.add(pwdPwd)
this.pnluser.add(btnSub)
this.pnluser.add(btnReset)
this.add(pnluser)//加载面板到框架
this.setVisible(true)//设置框架可显
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText()
String pwd = String.valueOf(pwdPwd.getPassword())
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE)
return
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE)
return
}else if(true){
this.dispose()
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE)
return
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("")
pwdPwd.setText("")
}
public static void main(String[] args){
new UserLogIn()
}
}
程序如下:
mport java.awt.HeadlessException
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.ImageIcon
import javax.swing.JButton
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用户名:")
JLabel lbl2 = new JLabel("密 码:")
JTextField txt = new JTextField("admin",20)
JPasswordField pwd = new JPasswordField(20)
JButton btn = new JButton("登录")
JPanel pnl = new JPanel()
private int error = 0
public MainFrame(String title) throws HeadlessException {
super(title)
init()
}
private void init() {
this.setResizable(false)
pwd.setEchoChar('*')
pnl.add(lbl1)
pnl.add(txt)
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equal花憨羔窖薏忌割媳公颅s(new String(pwd.getPassword()))){
pnl.removeAll()
JLabel lbl3 = new JLabel()
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"))
lbl3.setIcon(icon)
pnl.add(lbl3)
}
else{
if(error <3){
JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次")
error++
}
else{
JOptionPane.showMessageDialog(null,"对不起,您不是合法用户")
txt.setEnabled(false)
pwd.setEnabled(false)
btn.setEnabled(false)
}
}
}
})
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("测试")
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frm.setBounds(100, 100, 300, 120)
frm.setVisible(true)
}
}
编程的注意事项:
1、Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
2、 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
3、2006年11月13日,Java技术的发明者Sun公司宣布,将Java技术作为免费软件对外发布。Sun公司正式发布的有关Java平台标准版的第一批源代码,以及Java迷你版的可执行源代码。从2007年3月起,全世界所有的开发人员均可对Java源代码进行修改。