需要编写两个类
LonginFrame、MainFrame
LonginFrame类
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame {
JLabel lbluserLogIn;
JLabel lbluserName;
JLabel lbluserPWD;
JTextField txtName;
JPasswordField pwdPwd;
JButton btnSub;
JButton btnReset;
public LoginFrame() {
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
void userInit() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(290, 220);
setResizable(false);
setLocationRelativeTo(null);
setTitle("登录");
setVisible(true);
setLayout(null);
getContentPane().setBackground(Color.lightGray);
lbluserLogIn.setText("用户登录");
lbluserLogIn.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 20));
lbluserLogIn.setForeground(Color.RED);
lbluserName.setText("用户名:");
lbluserPWD.setText("密码:");
btnSub.setText("登录");
btnReset.setText("重置");
lbluserLogIn.setBounds(100, 15, 160, 30);
lbluserName.setBounds(50, 55, 60, 20);
lbluserPWD.setBounds(50, 85, 60, 25);
txtName.setBounds(110, 55, 120, 20);
pwdPwd.setBounds(110, 85, 120, 20);
btnSub.setBounds(85, 120, 60, 20);
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnsub_ActionEvent(e);
}
});
btnReset.setBounds(155, 120, 60, 20);
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnReset_ActionEvent(e);
}
});
add(lbluserLogIn);
add(lbluserName);
add(lbluserPWD);
add(txtName);
add(pwdPwd);
add(btnSub);
add(btnReset);
}
void btnsub_ActionEvent(ActionEvent e) {
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if (name.equals("")) {
JOptionPane.showConfirmDialog(null, "账号不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
} else if (pwd.equals("")) {
JOptionPane.showConfirmDialog(null, "密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
} else if (name.equals("abc") && pwd.equals("123")) {
this.dispose(); //释放当前窗体。关闭登录窗体
new MainFrame();
} else {
JOptionPane.showConfirmDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
}
private void MainFrame()
{
JFrame myWindow=new JFrame("其它");
JRadioButton rbMan=new JRadioButton("练习");
JRadioButton rbWonman=new JRadioButton("其它",true);
ButtonGroup bg=new ButtonGroup();
bg.add(rbMan);
bg.add(rbWonman);
JCheckBox[] jcb= {new JCheckBox("帮助"),new JCheckBox("退出")};
}
void btnReset_ActionEvent(ActionEvent e) {
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args) {
LoginFrame lf = new LoginFrame();
}
}
MainFrame类
import javax.swing.*;
import java.awt.event.*;
public class MainFrame extends JFrame implements ActionListener
{
JMenuBar bar;
JMenu exMenu,otherMenu;
JMenuItem helpMenuItem,exitMenuItem;
MainFrame(){
exMenu = new JMenu("练习");
helpMenuItem = new JMenuItem("帮助");
exitMenuItem =new JMenuItem("退出");
otherMenu = new JMenu("其它");
otherMenu.add(helpMenuItem);
otherMenu.add(exitMenuItem);
bar =new JMenuBar();
bar.add(exMenu);
bar.add(otherMenu);
setJMenuBar(bar);
helpMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
setTitle("主题窗口");
setBounds(100,50,420,380);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(e.getSource()==helpMenuItem)
JOptionPane.showConfirmDialog(null, "你好,请查看网络资源!","帮助",JOptionPane.CLOSED_OPTION);
else if(e.getSource()==exitMenuItem)
System.exit(0);
}
}
运行LonginFrame类
在这里设置账号、密码
之后在进入新界面,这个界面还需要进行设置才可以使用