English 中文(简体)
设为 Visible( false); 从 JFrame 内部
原标题:setVIsible(false); from within JFrame

我找不到任何直截了当的答案 回答我的问题, 希望你们可以!

我创建了一个类,叫做“Alass”内部的类,是一个动作处理器,它包含一系列对帐单,如果这些对帐单对照数据库核对了输入值。

我想做的是隐藏JFrame Frame, 也就是用Frame. setvisible(false) 来隐藏在星系中创建的JFrame(frame the Frame),

目前版本的代码(见以下不起作用的文本):

public class aClass{
static JTextField USER_NAME;
static JPasswordField PASSWORD;
static JButton submit;

private static class Handler implements ActionListener {
    @Override

    public void actionPerformed(ActionEvent e) {
        String USER_NAME_I = USER_NAME.getText();   
        String PASSWORD_I = PASSWORD.getText();   

        try {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(aClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fake",
             /*DB USER_NAME*/       "fakeuname", 
             /*DB password*/       "fakepassword");              

            Statement st = con.createStatement();
            String FindQuery = "SELECT acc_pass FROM users where acc_name= "+USER_NAME_I+" ";      

            //System.out.println(FindQuery);                        

            ResultSet rs = st.executeQuery(FindQuery);              

            if(rs.next())                                           
            {
                String PASSWORD_DB = rs.getString(1);                   

                if(PASSWORD_DB.equals(PASSWORD_I))                         
                {   
                    //THIS WILL NOT WORK
                    loginFrame.setVisible(false);


                    //do something       
                }

                else                                                
                {
                    JOptionPane.showMessageDialog(null,
                            "Wrong Password",
                            "Error",1);
                }//end else

           }//end if (rs.next())
            else                                                    
            {
                JOptionPane.showMessageDialog(null,
                        "Wrong Username",
                        "Error",1);
            }//end else

        } catch (SQLException ex) 
        {
            Logger.getLogger(aClass.class.getName()).log(Level.SEVERE, null, ex);
        }//end catch (SQLException ex)
    }//end ActionEvent
}//end ActionListener


aClass(){                                                   

    JFrame loginFrame = new JFrame("Logon");                        
    loginFrame.setVisible(true);                                    
    loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);   

    USER_NAME = new JTextField(10);                                     
    PASSWORD = new JPasswordField(10);                                 
    loginFrame.setLayout(new GridLayout(0,1));

    JPanel loginPanel = new JPanel();                               
    loginFrame.add(loginPanel);

    loginPanel.add(new JLabel("User Name: "));                       
    loginPanel.add(USER_NAME);                                          
    loginPanel.add(new JLabel("Password: "));                      
    loginPanel.add(PASSWORD);                                         

    submit = new JButton("Submit");                                 
    loginPanel.add(submit);                                         
    submit.addActionListener(new Handler());                        

 }//end aClass

public static void main(String args[])
{
    new aClass();                           
}//end main
}//end class

EDIT: My knowledge on the subject is limited, the code above is an edited version of an online tutorial I found. I ll fiddle some and see if I can get it to worki with JOptionPane and edit again if i need any help!

最佳回答

方法行动Perfed () 并不知道存在登录Frame, 因为登录Frame 的范围对构建者来说是本地的。 因此, 要么将登录Frame 的申报从构建者( 即作为成员变量, 如提交 JButton ) 中移出, 要么将其传递给 Handler 。 传递到 Handle 的方法之一是在 Handler 类中设置一个使用 JFrame 的构建器 。

问题回答

您如何将 loginFrame 变量从 aClass () <(/code) > 使用到 actionPerfor化 ?

和 yeah i d 建议相同的东西, 不要在这里使用框架, 使用 JDialog JOppPane





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...