简体   繁体   English

如何使按钮可见?

[英]How to make button visible?

I have made a button By default invisible by customizing code.我通过自定义代码使按钮默认不可见。 But now my problem is I cant make the button visible!但现在我的问题是我不能让按钮可见! The thing I want is,that when user LOG IN means the LOG OUT button should be visible.我想要的是,当用户登录意味着 LOG OUT 按钮应该是可见的。 How can i do it using netbeans?我如何使用 netbeans 做到这一点? can any1 help me?任何人可以帮助我吗? I m new in JAVA GUI.我是 JAVA GUI 的新手。 What condition should i add here?我应该在这里添加什么条件?

 JLabel j1;

public NewJFrame() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jButton4 = new javax.swing.JButton();
    jLabel5 = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    jComboBox1 = new javax.swing.JComboBox();
    jButton1 = new javax.swing.JButton();
    jTextField3 = new javax.swing.JTextField();
    jButton2 = new javax.swing.JButton();
    jPasswordField1 = new javax.swing.JPasswordField();
    jButton3 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jLabel6 = new javax.swing.JLabel();
    jComboBox2 = new javax.swing.JComboBox();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jComboBox3 = new javax.swing.JComboBox();
    jLabel9 = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    jButton6 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(102, 255, 0));
    jLabel2.setText("EMAIL");

    jLabel3.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(102, 255, 0));
    jLabel3.setText("PASSWORD");

    jTextField1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 153), 2, true));
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(evt);
        }
    });

    jButton4.setBackground(new java.awt.Color(0, 153, 255));
    jButton4.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
    jButton4.setText("Log In");
    jButton4.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(0, 153, 255), null, null)));
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    jLabel5.setBackground(new java.awt.Color(255, 153, 102));
    jLabel5.setFont(new java.awt.Font("Rockwell Extra Bold", 3, 24)); // NOI18N
    jLabel5.setForeground(new java.awt.Color(255, 0, 204));
    jLabel5.setText("SELECT TYPE");

    jLabel1.setBackground(new java.awt.Color(153, 102, 255));
    jLabel1.setFont(new java.awt.Font("Bookman Old Style", 3, 48)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 0, 204));
    jLabel1.setText("SHOPPERS BAY...");
    jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(255, 102, 102), new java.awt.Color(255, 255, 102), null));
    jLabel1.setIconTextGap(2);

    jComboBox1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
    jComboBox1.setForeground(new java.awt.Color(0, 0, 51));
    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CLOTHES", "ACCESSORIES", "SHOES" }));
    jComboBox1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createCompoundBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

    jButton1.setBackground(new java.awt.Color(0, 153, 255));
    jButton1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
    jButton1.setText("OK");
    jButton1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 153, 255), new java.awt.Color(0, 153, 255), null, null)));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jTextField3.setForeground(new java.awt.Color(153, 153, 153));
    jTextField3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 255), 1, true));
    jTextField3.setDisabledTextColor(new java.awt.Color(0, 0, 0));
    jTextField3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField3ActionPerformed(evt);
        }
    });

    jButton2.setBackground(new java.awt.Color(255, 51, 51));
    jButton2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
    jButton2.setText("SEARCH");
    jButton2.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jPasswordField1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 153), 2, true));

    jButton3.setBackground(new java.awt.Color(0, 153, 255));
    jButton3.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
    jButton3.setText("Register");
    jButton3.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(0, 153, 255), null, null)));
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton5.setFont(new java.awt.Font("Stencil", 3, 14)); // NOI18N
    jButton5.setForeground(new java.awt.Color(204, 0, 0));
    jButton5.setText("ACCOUNTS");
    jButton5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(102, 204, 0), null, new java.awt.Color(102, 51, 0)));
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    jLabel6.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
    jLabel6.setForeground(new java.awt.Color(255, 0, 204));
    jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel6.setText("New User?");

    jComboBox2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
    jComboBox2.setForeground(new java.awt.Color(0, 0, 51));
    jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "VISITOR", "ACCOUNTANT", "ADMINISTRATOR" }));
    jComboBox2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(102, 255, 0), null, null));
    jComboBox2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox2ActionPerformed(evt);
        }
    });

    jLabel7.setFont(new java.awt.Font("SimSun-ExtB", 1, 11)); // NOI18N
    jLabel7.setForeground(new java.awt.Color(102, 255, 0));
    jLabel7.setText("PRICE:");

    jLabel8.setText("to");

    jTextField2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 255), 1, true));

    jComboBox3.setBackground(new java.awt.Color(255, 204, 204));
    jComboBox3.setFont(new java.awt.Font("Stencil", 1, 12)); // NOI18N
    jComboBox3.setForeground(new java.awt.Color(51, 51, 255));
    jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CLOTHES", "ACCESSORIES", "SHOES" }));
    jComboBox3.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 0, 204)));

    jLabel9.setFont(new java.awt.Font("SimSun-ExtB", 1, 12)); // NOI18N
    jLabel9.setForeground(new java.awt.Color(102, 255, 0));
    jLabel9.setText("CATEGORY:");

    jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/finalproject/Collages - Copy1.png"))); // NOI18N

    jButton6.setText("LOGOUT");
    jButton6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton6ActionPerformed(evt);
        }
    });
    jButton6.setVisible(false);
    if(jComboBox2.getSelectedItem()=="ADMINISTRATOR")
    jButton6.setVisible(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(30, 30, 30)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(69, 69, 69)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(107, 107, 107)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(58, 58, 58)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(4, 4, 4)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(79, 79, 79)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(184, 184, 184))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(168, 168, 168))))))
                .addComponent(jLabel10))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel3)
                .addComponent(jLabel2))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(67, 67, 67))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addGap(14, 14, 14)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(jLabel7)
                            .addGap(14, 14, 14)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(28, 28, 28)
                            .addComponent(jLabel8))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(27, 27, 27)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(24, 24, 24)
                            .addComponent(jButton2)))
                    .addGap(33, 33, 33))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(3, 3, 3)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(20, 20, 20)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(80, 80, 80))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGap(7, 7, 7)
            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(48, 48, 48)))
            .addComponent(jButton5))
    );

    pack();
}// </editor-fold>                        

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                                                             
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        Connection conn=null;
    try{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        conn =DriverManager.getConnection(DB_URL, USER, PASS);
        Statement stmt=conn.createStatement();

        String S1 = jTextField1.getText();
        String S2 = jPasswordField1.getText().toString();
        String S3=jComboBox2.getSelectedItem().toString();
       // System.out.println("User: "+userName + " Password: "+ passWord);

        // if user name is blank, warning message shown
        if(S1.isEmpty() || S1 == null){
            JOptionPane.showMessageDialog(null, "Please insert a valid user name", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        // if password is blank, warning message shown
        if(S2.isEmpty() || S2 == null){
            JOptionPane.showMessageDialog(null, "Password field is empty", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int flag=0;
        String sql;
        sql="SELECT * from member where EMAIL='"+S1+"'";
        ResultSet rs=stmt.executeQuery(sql);
        //System.out.println(rs.next());
        String uname="",pw="",category="";
        while(rs.next()){                       
        uname=rs.getString("EMAIL");
        pw=rs.getString("PASSWORD");
        category=rs.getString("CATEGORY");

        if(uname.matches(S1) && pw.equals(S2)==true && category.matches(S3))
        {
            flag=1;
            /*setVisible(false);
    Congrats J7=new Congrats();
    J7.getContentPane().setBackground(Color.green);
    J7.setVisible(true); */
          if(jComboBox2.getSelectedItem()=="VISITOR")
    {
        setVisible(false);
    NewJFrame f1=new NewJFrame();
    f1.setVisible(true);
    jButton6.setVisible(true);
    }
    else if(jComboBox2.getSelectedItem()=="ACCOUNTANT")
    {
         setVisible(false);
    inter4 f=new inter4();
    f.setVisible(true);
    }
    else if(jComboBox2.getSelectedItem()=="ADMINISTRATOR")
    {
      setVisible(false);
    NewJFrame f1=new NewJFrame();
    f1.setVisible(true);
    }
         }
        } 
        if(flag==0)
            JOptionPane.showMessageDialog(null,"login failed");      
        stmt.close();
        conn.close();
        }
    catch(SQLException ss){
      ss.printStackTrace(); 
}   

}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    setVisible(false);
    NewJFrame21 fF=new NewJFrame21();
    fF.getContentPane().setBackground(Color.magenta);
    fF.setVisible(true);
}                                        

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    setVisible(false);
    inter4 J1=new inter4();
    J1.getContentPane().setBackground(Color.green);
    J1.setVisible(true);

}                                        


// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration                   

private ImageIcon format=null;
}

Being new in Java GUI I believe that is better to write some GUI code by hand, just to see how things work.作为 Java GUI 的新手,我认为最好手动编写一些 GUI 代码,看看事情是如何工作的。 When you understand the concept of swing, you can move on to a GUI designer to make things easier and faster.当您理解了 Swing 的概念后,您就可以转向 GUI 设计器,让事情变得更简单、更快捷。 But without (deep) understanding of things like Event Handling, Layout Managers, and such things you can't be sure about "how" to implement what you want to implement (and make it appear on screen and interact)但是如果没有(深入)了解事件处理、布局管理器等内容,您就无法确定“如何”实现您想要实现的内容(并使其出现在屏幕上并进行交互)

So what you want is a button getting visible when you push another button (the LOG IN one).所以你想要的是当你按下另一个按钮(登录按钮)时一个按钮变得可见。 This has to be done with an ActionListener to the LOG IN button.这必须通过登录按钮的 ActionListener 来完成。 So, you have to state to the compiler that this class will have some Action Events ("action" events are the cases of button clicks, "key" events are the cases of keys pressed, etc).因此,您必须向编译器声明该类将具有一些动作事件(“动作”事件是按钮单击的情况,“键”事件是按键按下的情况等)。

So you write in your class definition:所以你写在你的类定义中:

class GUIExample implements ActionListener{

Now you can define the buttons (I will define few of them).现在您可以定义按钮(我将定义其中的几个)。 Note: always declare button (and other component) names as class fields (not in the method that instantiates them) so you can refer to them inside all the class methods.注意:始终将按钮(和其他组件)名称声明为类字段(而不是在实例化它们的方法中),以便您可以在所有类方法中引用它们。

JButton logIn, logOut;
JFrame frame;    //the application main frame

That should do it!应该这样做! Now lets make a constructor!现在让我们创建一个构造函数!

GUIExample(){
    frame = new JFrame("GUI Example in Swing");    //frame's head text
    logIn = new JButton("LOG IN");
    logOut = new JButton("LOG OUT");
    frame.setSize(300,400);
    frame.setVisible(true);
    frame.add(logIn);
    logIn.setVisible(true);
    frame.add(logOut);
    logOut.setVisible(false);        //The LOG OUT is invisible now!
    logIn.addActionListener(this);        //this tells the JVM to keep an eye on this
                                          // button and run the actionPerformed method
                                          // if it gets pushed!
}

Now you have to tell the JVM "what to do" if the button LOG IN is pressed!现在你必须告诉 JVM 如果按下 LOG IN 按钮“要做什么”!

    public void actionPerformed(ActionEvent e){

        JButton src = (JButton) e.getSource()    //get the objectthat triggered the event
        if (src.equals(logIn))        //if the source is the LOG IN button (in our case it is the only thing that can trigger an event)
            logOut.setVisible(true);    ///finally! make the LOG OUT visible!
    }

This should do it!这个应该可以! Now call the constructor via the main:现在通过 main 调用构造函数:

public static void main (String args[]){
    new GUIExample();
}

This is it!就是这个! To run this example don't forget to import Swing:要运行此示例,请不要忘记导入 Swing:

 import javax.swing.*;

and the event package:和事件包:

 import java.awt.event.*;

Hope I helped!希望我有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM