qq_43794238
只跟傻子玩
采纳率0%
2020-06-30 23:10

通过LoginFrame loginFrame = new LoginFrame(); loginFrame.setVisible(true);弹框到下一页空白,不知道是什么问题求大神指教

图片说明图片说明

regframe代码
```package sys.mtj.com.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import sys.mtj.com.entity.Student;
import sys.mtj.com.model.StudentDao;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.AbstractAction;
import javax.swing.Action;

public class RegFrame extends JFrame{

    private JFrame frame;
    private JTextField userNameTextField;
    private JTextField passwordTextField_1;
    private JTextField emailTextField_2;
    private final Action action = new SwingAction();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RegFrame window = new RegFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public RegFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setTitle("学生信息注册");
        frame.getContentPane().setForeground(Color.BLACK);
        frame.setBounds(100, 100, 456, 327);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        JLabel lblNewLabel = new JLabel("学生信息注册");
        lblNewLabel.setBounds(185, 13, 142, 37);
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 20));

        JLabel label = new JLabel("账号");
        label.setBounds(78, 66, 72, 18);
        label.setFont(new Font("宋体", Font.BOLD, 18));

        JLabel passwordTextField = new JLabel("密码");
        passwordTextField.setBounds(78, 110, 72, 18);
        passwordTextField.setFont(new Font("宋体", Font.BOLD, 18));

        JLabel emailTextField = new JLabel("邮箱");
        emailTextField.setBounds(78, 154, 72, 18);
        emailTextField.setFont(new Font("宋体", Font.BOLD, 18));
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(lblNewLabel);
        frame.getContentPane().add(label);
        frame.getContentPane().add(passwordTextField);
        frame.getContentPane().add(emailTextField);

        userNameTextField = new JTextField();
        userNameTextField.setBounds(164, 65, 163, 24);
        frame.getContentPane().add(userNameTextField);
        userNameTextField.setColumns(10);

        passwordTextField_1 = new JTextField();
        passwordTextField_1.setColumns(10);
        passwordTextField_1.setBounds(164, 109, 163, 24);
        frame.getContentPane().add(passwordTextField_1);

        emailTextField_2 = new JTextField();
        emailTextField_2.setColumns(10);
        emailTextField_2.setBounds(164, 153, 163, 24);
        frame.getContentPane().add(emailTextField_2);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/zhuce.png")));
        lblNewLabel_1.setBounds(143, 8, 72, 42);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("");
        lblNewLabel_2.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/yonghuming.png")));
        lblNewLabel_2.setBounds(38, 53, 72, 37);
        frame.getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_2_1 = new JLabel("");
        lblNewLabel_2_1.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/mima.png")));
        lblNewLabel_2_1.setBounds(38, 97, 72, 37);
        frame.getContentPane().add(lblNewLabel_2_1);

        JLabel lblNewLabel_2_2 = new JLabel("");
        lblNewLabel_2_2.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/youxiang(1).png")));
        lblNewLabel_2_2.setBounds(38, 141, 72, 37);
        frame.getContentPane().add(lblNewLabel_2_2);

        JButton btnregButton = new JButton("注册");
        btnregButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reg(e);
            }
        });
        btnregButton.setBackground(Color.LIGHT_GRAY);
        btnregButton.setForeground(Color.BLACK);
        btnregButton.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/zhucehuiyuan.png")));

        btnregButton.setBounds(104, 200, 95, 27);
        frame.getContentPane().add(btnregButton);

        JButton btnRestartButton_1 = new JButton("重置");
        btnRestartButton_1.setAction(action);
        btnRestartButton_1.setBackground(Color.LIGHT_GRAY);
        btnRestartButton_1.setIcon(new ImageIcon(RegFrame.class.getResource("/sys/mtj/com/image/zhongzhi.png")));
        btnRestartButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetRegInfo(e);
            }
        });
        btnRestartButton_1.setBounds(257, 200, 95, 27);
        frame.getContentPane().add(btnRestartButton_1);
    }
    protected void reg(ActionEvent e) {
        // TODO Auto-generated method stub
         String userName=userNameTextField.getText().toString();
         String password=passwordTextField_1.getText().toString();
         String email=emailTextField_2.getText().toString();
         //判断非空
         if("".equals(userName)||userName==null) {
             JOptionPane.showMessageDialog(this,"注册账号不能为空");
             return;

         }
         if("".equals(password)||password==null) {
             JOptionPane.showMessageDialog(this,"注册密码不能为空");
             return;
         }
         if("".equals(email)||email==null) {
                JOptionPane.showMessageDialog(this, "注册邮箱不能为空");
                return;//结束当前方法不再往下执行
            }


         //将数据插入到数据库表中
         Student s=new Student();
        s.setName(userName);
        s.setPassword(password);
        s.setEmail(email);
        //调用数据model的方法进行添加操作
        StudentDao dao = new StudentDao();
        int row = dao.addStudent(s);
        if(row>=1) {//注册成功
            //注册成功 弹出提示框 点击提示框的确定按钮,跳转到登陆页面
            JOptionPane.showMessageDialog(this, "注册成功");
            //跳转  当前注册页面隐藏  显示登陆页面
            frame.dispose();//当前注册页面隐藏
            LoginFrame loginFrame = new LoginFrame();
            loginFrame.setVisible(true);

        }else {
            //注册失败,弹出提示框
            JOptionPane.showMessageDialog(this, "注册失败");

        }








    }

    protected void resetRegInfo(ActionEvent e) {
        // TODO Auto-generated method stub


         //清空操作
         userNameTextField.setText("");
         passwordTextField_1.setText("");
         emailTextField_2.setText("");

    }
    private class SwingAction extends AbstractAction {
        public SwingAction() {
            putValue(NAME, "重置");

        }
        public void actionPerformed(ActionEvent e) {
        }
    }
}










loginframe代码
package sys.mtj.com.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JTextPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import sys.mtj.com.entity.Student;
import sys.mtj.com.model.StudentDao;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginFrame extends JFrame{

    private JFrame frame;
    private JTextField userNametextField;
    private JTextField pwdtextField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrame window = new LoginFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public LoginFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("宋体", Font.BOLD, 20));
        frame.setTitle("学生管理系统");
        frame.getContentPane().setBackground(Color.WHITE);


        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/xueshengxinxi(1).png")));

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/yonghuming.png")));

        JLabel lblNewLabel_1_1 = new JLabel("");
        lblNewLabel_1_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/mima.png")));

        JLabel lblNewLabel_2 = new JLabel("用户名");
        lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 18));

        JLabel lblNewLabel_2_1 = new JLabel("密码");
        lblNewLabel_2_1.setFont(new Font("宋体", Font.BOLD, 18));

        JLabel lblNewLabel_3 = new JLabel("学生管理系统");
        lblNewLabel_3.setFont(new Font("宋体", Font.BOLD, 20));

        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                login(e);
            }

        });
        loginButton.setBackground(SystemColor.textHighlight);
        loginButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/denglu.png")));

        JButton regButton = new JButton("注册");
        regButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
            }
        });
        regButton.setBackground(SystemColor.textHighlight);
        regButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/zhucehuiyuan.png")));

        JButton resetButton = new JButton("重置");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset(e);
            }
        });
        resetButton.setBackground(SystemColor.textHighlight);
        resetButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/sys/mtj/com/image/zhongzhi.png")));

        userNametextField = new JTextField();
        userNametextField.setColumns(10);

        pwdtextField = new JTextField();
        pwdtextField.setColumns(10);
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(122)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(41)
                            .addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE))))
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(42)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(52)
                            .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)))
                    .addGap(10)
                    .addComponent(userNametextField, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE))
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(42)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_1_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(52)
                            .addComponent(lblNewLabel_2_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)))
                    .addGap(10)
                    .addComponent(pwdtextField, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE))
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(66)
                    .addComponent(loginButton)
                    .addGap(41)
                    .addComponent(regButton)
                    .addGap(37)
                    .addComponent(resetButton))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(13)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                    .addGap(28)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(6)
                            .addComponent(userNametextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                    .addGap(24)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(lblNewLabel_1_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNewLabel_2_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(pwdtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(20)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(loginButton)
                        .addComponent(regButton)
                        .addComponent(resetButton)))
        );
        frame.getContentPane().setLayout(groupLayout);
        frame.setBounds(100, 100, 450, 300);
        //frame.setSize(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
    }

    protected void login(ActionEvent e) {
        // TODO Auto-generated method stub
        String name=userNametextField.getText().toString();
        String pwd=pwdtextField.getText().toString();
         if("".equals(name)||name==null) {
             JOptionPane.showMessageDialog(this,"请输入账号");
             return;

         }
         if("".equals(pwd)||pwd==null) {
             JOptionPane.showMessageDialog(this,"请输入密码");
             return;
         }


         //根据学生的账号和密码  查询当前学生是否存在
         //如果存在就可以执行登录 去到主页


         StudentDao s=new StudentDao();
         Student student = s.findStudentByNameAndPwd(name, pwd);
         if(student!=null) {
             JOptionPane.showMessageDialog(this,"欢迎"+student.getName()+"登陆成功");
             frame.dispose();
             StudentIndexFrame index=new StudentIndexFrame();
             index.setVisible(true);

         }else {


         //如果不存在,就说明该用户没有注册 应该去注册
         JOptionPane.showMessageDialog(this,"用户不存在,请前往注册");

         }


    }

    protected void reset(ActionEvent e) {
        userNametextField.setText("");
        pwdtextField.setText("");
        // TODO Auto-generated method stub


    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答