如何使按钮可见?

时间:2014-03-23 21:37:27

标签: java

我创建了一个按钮默认情况下,通过自定义代码不可见。 但现在我的问题是我不能让按钮可见!我想要的是,当用户LOG IN意味着LOG OUT按钮应该是可见的。 我怎么能用netbeans做到这一点?能帮助我吗?我是JAVA GUI的新手。我应该在这里添加什么条件?

 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;
}

1 个答案:

答案 0 :(得分:0)

在Java GUI中成为新手我相信最好手工编写一些GUI代码,只是为了看看它是如何工作的。当您了解swing的概念时,您可以转到GUI设计器,以便更轻松,更快速地完成任务。但是,如果没有(深入)了解事件处理,布局管理器等事情,您无法确定“如何”实现您想要实现的内容(并使其显示在屏幕上并进行交互)

所以你想要的是当你按下另一个按钮(LOG IN一个按钮)时按钮变得可见。 这必须通过ActionListener到LOG IN按钮来完成。因此,您必须向编译器声明此类将具有一些操作事件(“操作”事件是按钮单击的情况,“键”事件是按下键的情况等)。

所以你写了你的班级定义:

class GUIExample implements ActionListener{

现在您可以定义按钮(我将定义其中的几个)。 注意:总是将按钮(和其他组件)名称声明为类字段(不在实例化它们的方法中),因此您可以在所有类方法中引用它们。

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

应该这样做! 现在让我们做一个构造函数!

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!
}

现在,如果按下LOG IN按钮,你必须告诉JVM“做什么”!

    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!
    }

应该这样做!现在通过main调用构造函数:

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

就是这样!要运行此示例,请不要忘记导入Swing:

 import javax.swing.*;

和事件包:

 import java.awt.event.*;

希望我帮忙!