JFrame没有配置()

时间:2017-03-12 08:15:09

标签: java

所以,我有一个登录JFrame,它在我运行代码时出现。问题是如果用户输入正确的用户名和密码,则在显示另一帧时,需要处理此登录框,但它不是。我尝试了dispose()setVisible = false,但仍无法隐藏或处置。

 class LoggingWindow extends JFrame {


  static JFrame loginFrame = new JFrame();   
  JPanel loginPanel = new JPanel();
  JTextField loginNameFld = new JTextField(10);
  JPasswordField loginPassFld = new JPasswordField(10);
  JTextField statusFld = new JTextField(11);
  String userName = "user";
  String password = "password";

  //Initialize loginFrame 

   public static void initLoginFrame() {     
    JFrame loginWindow = new LoggingWindow();
    //loginFrame.setTitle("\"Moflo Registration\"");
    loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loginWindow.setResizable(false);
    loginWindow.setUndecorated(true);
    loginWindow.setVisible(true);
    loginWindow.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
    loginWindow.setSize(new Dimension(220, 290));
    loginWindow.setLocationRelativeTo(null);
    loginWindow.pack();

   LoggingWindow() {

        loginFrame.add(loginPanel);
        loginPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbb = new GridBagConstraints();
        gbb.insets = new Insets(1, 1, 1, 1);
        gbb.anchor = GridBagConstraints.CENTER;


        JPanel loginNameAndPasswordPanel = new JPanel();
        loginPanel.add(loginNameAndPasswordPanel,gbb);
        gbb.gridx = 0;
        gbb.gridy = 2;


        loginNameAndPasswordPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.insets = new Insets(0,0,0,0);

        JLabel loginNameLab = new JLabel("Нэр :           ");
        gbc.gridx = 0;
        gbc.gridy = 0;
        loginNameAndPasswordPanel.add(loginNameLab, gbc);

        JLabel loginPassLab = new JLabel("Нууц үг :     ");
        gbc.gridx = 0;
        gbc.gridy = 1;
        loginNameAndPasswordPanel.add(loginPassLab, gbc);

        loginNameFld.setHorizontalAlignment(JTextField.CENTER);
        gbc.gridx = 1;
        gbc.gridy = 0;
        loginNameAndPasswordPanel.add(loginNameFld, gbc);

        loginPassFld.setHorizontalAlignment(JTextField.CENTER);
        gbc.gridx = 1;
        gbc.gridy = 1;
        loginNameAndPasswordPanel.add(loginPassFld, gbc);

        statusFld.setEditable(false);

        loginNameAndPasswordPanel.add(statusFld, gbc);
        statusFld.setHorizontalAlignment(JTextField.CENTER);

        JPanel buttonsPanel = new JPanel();
        loginPanel.add(buttonsPanel,gbb);
        gbb.gridx = 0;
        gbb.gridy = 3;  
        buttonsPanel.setLayout(new GridBagLayout());
        GridBagConstraints gba = new GridBagConstraints();
        gba.anchor = GridBagConstraints.LINE_END;
        gba.insets = new Insets(2, 2, 2, 2);

        JButton loginBtn = new JButton("Нэвтрэх");
        gba.gridx = 0;
        gba.gridy = 0;
        buttonsPanel.add(loginBtn, gba);     

        loginBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {

        String name = loginNameFld.getText();
        String pass = loginPassFld.getText();

    if(event.getSource() == loginBtn){
    if (name.equals(userName) && pass.equals(password)) {
                  initMainFrame();
                  loginFrame.dispose(); 
    JOptionPane.showMessageDialog(null, "Системд нэвтэрлээ. Өнөөдөр " + showDate, " ", JOptionPane.INFORMATION_MESSAGE);

    } else {
        statusFld.setText("Нэр эсвэл нууц үг буруу байна.");
    }

        }
        }
    }); 

        JButton closeBtn = new JButton("       Хаах    ");
        gba.gridx = 1;
        gba.gridy = 0;
        buttonsPanel.add(closeBtn, gba);
        add(loginPanel);
        closeBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    }); 
}
            //Main method
public static void main(String[] args) {

   SwingUtilities.invokeLater(new Runnable() {
   @Override
   public void run() {

       initLoginFrame();
     }
    });
   }
  }

  public class MainFrame extends JFrame {

 //Initialzie mainFrame
public static void initMainFrame() {
    JFrame mainFrame = new MainFrame();


    mainFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    mainFrame.setVisible(true);
    mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    mainFrame.setMinimumSize(new Dimension(800, 600));
    mainFrame.setLocationRelativeTo(null);
}

一些,我认为为了简洁起见,没有显示不重要的陈述

3 个答案:

答案 0 :(得分:1)

我相信您将“loginWindow”与“loginFrame”混淆了。你试着用

loginFrame.dispose(); 

但你的内容是在loginWindow上,而不是loginFrame。

我能够让它处理用户名窗口,执行以下操作。

static JFrame loginWindow;  <--- create as class variable, not local.
//loginFrame.add(loginPanel);  <--- doesn't appear that this is actually used

if(event.getSource() == loginBtn){
                    if (name.equals(userName) && pass.equals(password)) {
                        MainFrame.initMainFrame();
                        //loginFrame.dispose();    <--- again, not used
                        loginWindow.dispose();     <--- want to dispose
                        JOptionPane.showMessageDialog(null, "Системд нэвтэрлээ. Өнөөдөр " , " ", JOptionPane.INFORMATION_MESSAGE);

                    } else {
                        statusFld.setText("Нэр эсвэл нууц үг буруу байна.");
                    }

                }

您还必须更改此内容:

JFrame loginWindow = new LoggingWindow(); 

到:

 loginWindow = new LoggingWindow();

答案 1 :(得分:0)

您可以随时发送&#34;关闭&#34;事件到JFrame对象:

loginFrame.dispatchEvent(new WindowEvent(loginFrame, WindowEvent.WINDOW_CLOSING));

答案 2 :(得分:0)

我肯定与loginFrame混淆确实没用。我创建了一个类变量:static JFrame loginWindow;但它会导致NullPointerException。