2013-07-18 25 views
1

我正在嘗試編寫一個Java程序,當您單擊一個按鈕時隱藏該按鈕並顯示圖像。我已經這樣做了,使按鈕消失,但標籤不出現。這裏是我的代碼:Java中的按鈕和JLabels

final JLabel label = new JLabel(image, JLabel.CENTER); 
label.setAlignmentX(0); 
label.setAlignmentY(0); 
label.setVisible(false); 
label.setIcon(image); 

final JButton button = new JButton("CLICK"); 
button.addActionListener(new ActionListener(){ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     button.setVisible(false); 
     label.setVisible(true); 
    } 

}); 
+0

擺脫label.setVisible(假)的',' –

+1

提供SSCCE。目前尚不清楚標籤是否被添加到面板中。 –

+0

一切都取決於使用的LayoutManager,在一些佈局管理器的特定情況下(例如NullLayout沒有Instest,FlowLayout.CENTER)可能導致此問題,那麼您不能在沒有發佈[SSCCE](http:// sscce.org/),簡短,可運行,可編譯 – mKorbel

回答

0

放在一個JFrame所有項目,並做了一個JFramerevalidate()得到顯示更改。

+0

你不添加組件到一個框架。您將組件添加到JPanel。然後將面板添加到框架。 JFrame沒有實現revalidate()方法。你應該在面板上重新驗證()。 – camickr

0

試試這個...

public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     jLabel1 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("jButton1"); 
     jButton1.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       jButton1MouseClicked(evt); 
      } 
     }); 

     jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1371634490.png"))); // NOI18N 
     jLabel1.setText("jLabel1"); 
     jLabel1.setVisible(false); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jButton1) 
       .addGap(159, 159, 159)) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(148, 148, 148) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(120, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGap(48, 48, 48) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE) 
       .addComponent(jButton1) 
       .addGap(91, 91, 91)) 
     ); 

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

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { 
     // TODO add your handling code here: 

     jLabel1.setVisible(true); 
     jButton1.setVisible(false); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    // End of variables declaration 
} 
0

的解決方案是將兩個按鈕,並在同一個面板上的標籤。最初隱藏標籤。然後,當點擊按鈕時,隱藏按鈕並取消隱藏標籤。

下面是一個例子:

public class MagicButton { 

    public static void main(String[] args) { 
     // Components 
     final JFrame frame = new JFrame("Magic Button"); 
     final JButton btn = new JButton("Now, you see me!"); 
     final JLabel label = new JLabel("Now, you don't!"); 

     // Arrangement 
     frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); 
     frame.add(label); 
     frame.add(btn); 

     // Make Label invisible 
     label.setVisible(false); 

     // When button clicked: hide button, show label, repack frame 
     btn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       btn.setVisible(false); 
       label.setVisible(true); 
       frame.pack(); 
      } 
     }); 

     // pack and display 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
}