亲测可以运行,无主方法。只需在主方法中创建该类对象,调用即可运行。
package 学生管理系统;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainInterface2 extends JFrame implements ActionListener{
JPanel jp1,jp2,jp3,jp4,jp5; //面板
JLabel labelTitle; //标题
JLabel labelUserName, labelPassword, labelChoose; //用户名,密码提示
JTextField textUserName; //输入用户名
JPasswordField textPassword = null ; //输入密码 * 设置为JPasswordField
JButton buttonEnter; //登录按扭
JButton buttonRegister, buttonForget; //注册按钮,忘记密码按钮
JRadioButton stu,tea; //单选按钮
//构造
MainInterface2() {
init();
this.setResizable(false); //设置窗口大小不可调节
this.setTitle("学生管理系统"); //设置窗口标题
this.setLocationRelativeTo(null); //在屏幕中间显示(居中显示)
this.setBounds(650,240,430,470);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //设置关闭窗口同时关闭JVM
this.setVisible(true); //设置为窗口可见
}
//设置窗口组件
void init(){
//设置字体
Font fontTitle = new Font("宋体",Font.BOLD, 30 ); //标题字体
Font fontLabel = new Font("黑体", Font.BOLD,20 );//提示语字体
//设置面板
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel();
//设置标题
labelTitle = new JLabel("学生管理系统");
labelTitle.setFont(fontTitle);
//设置Label
labelUserName = new JLabel("用户名 : ");
labelUserName.setFont(fontLabel);
labelPassword = new JLabel("密 码 : ");
labelPassword.setFont(fontLabel);
labelChoose = new JLabel("登录身份");
labelChoose.setFont(fontLabel);
//设置JTextField
textUserName = new JTextField(10);
//设置JPasswordField
textPassword = new JPasswordField(10);
//设置按钮
buttonEnter = new JButton(" 登录 ");
buttonRegister = new JButton(" 注册用户 ");
buttonForget = new JButton(" 忘记密码 ");
//设置单选按钮
ButtonGroup group;
stu = new JRadioButton("学生");
tea = new JRadioButton("教师");
group = new ButtonGroup();
group.add( stu );
group.add( tea );
tea.setSelected( true ); //启用选项
//设置布局
this.setLayout( new GridLayout(6, 1) ); //网格式布局
//添加各个组件
jp1.add( labelTitle );
jp2.add( labelUserName);
jp2.add( textUserName );
jp2.add( labelPassword );
//jp2.add(textPassword );
jp2.add( textPassword );
jp3.add( labelChoose );
jp3.add ( stu );
jp3.add( tea );
jp4.add( buttonEnter );
jp5.add( buttonRegister );
jp5.add ( buttonForget );
this.add( jp1 );
this.add( jp2 );
this.add( jp3 );
this.add( jp4 );
this.add( new JPanel() );
this.add( jp5 );
//添加监视器
tea.addActionListener( this );
textUserName.addActionListener( this );
textPassword.addActionListener( this );
buttonEnter.addActionListener( this );
}
//设置窗口背景
void setBackground(){
ImageIcon background = new ImageIcon("bb.jpg"); //根目录下文件名
JLabel label = new JLabel( background );
label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
//将窗格转化为JPanel,否则不能调用setOpaque
JPanel imagePanel = new JPanel();
imagePanel = ( JPanel )this.getContentPane();
imagePanel.setOpaque(false); //设置窗口透明
imagePanel.setLayout( new FlowLayout() );
this.getLayeredPane().add( label , new Integer( Integer.MIN_VALUE));
}
//监视器
public void actionPerformed(ActionEvent e) {
//如果点击了登录按钮
if( e.getSource() == buttonEnter )
{
//判断所处身份,判断账户密码
if( tea.isSelected() ) //如果教师身份被选中
teaLogin(); //调用方法判断账户密码
}
}
//判断教师账户密码
public void teaLogin(){
final String tea_Name = "123456";
final String tea_Password = "000";
String s = String.valueOf( textPassword.getPassword() );
//如果账户密码正确
if( tea_Name.equals( textUserName.getText() ) && tea_Password.equals( s ) )
{
JOptionPane.showMessageDialog( null , "登录成功", "提示 ", JOptionPane.WARNING_MESSAGE ); //在正确情况下弹出消息对话框
//调用clear清除账号密码
clear();
//关闭当前界面
dispose();
}
else if ( textUserName.getText().isEmpty() && s.isEmpty() ) // null 与 isEmpty()的区别, null是对象为空,isEmpty()时值为空
{
JOptionPane.showMessageDialog(null , "请输入用户名和密码", "提示", JOptionPane.WARNING_MESSAGE ); //提示错误
}
else if ( textUserName.getText().isEmpty() ) //当同户名为空
{
JOptionPane.showMessageDialog( null , "请输入用户名","提示", JOptionPane.WARNING_MESSAGE );
}
else if( s.isEmpty() ) //当密码为空
{
JOptionPane.showMessageDialog(null, "请输入密码", "提示",JOptionPane.WARNING_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "账户或密码错误","提示",JOptionPane.WARNING_MESSAGE);
}
}
//清楚文本框用户名,密码
public void clear(){
textUserName.setText("");
textPassword.setText("");
}
}