2009-12-11 46 views
2

我創建了JToolBar(Java Swing)。我在框架上設置了一個包含JToolBar的背景圖像。我想我的JToolBar是透明的,以便保持在框架上的圖像應該是可見的。我正在使用setOpaque(false),但它對我的工具欄沒有任何影響。有沒有人有過此事?我的代碼如下Java Swing JToolBar

import javax.swing.ImageIcon; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class NewJFrame extends javax.swing.JFrame { 
    static NewJFrame dialog = null; 

    /** Creates new form NewJFrame */ 
    public NewJFrame() { 
     initComponents(); 
     jToolBar1.setOpaque(false); 
    } 

    /** 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() { 

     jToolBar1 = new javax.swing.JToolBar(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jToolBar1.setRollover(true); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(275, Short.MAX_VALUE)) 
     ); 

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

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 



      public void run() { 

       try { 
        try { 
         // Set cross-platform Java L&F (also called "Metal") 
         UIManager.setLookAndFeel(UIManager. 
           getSystemLookAndFeelClassName()); 

        } catch (UnsupportedLookAndFeelException ex) { 
         ex.printStackTrace(); 
        } 
       } catch (ClassNotFoundException e) { 
        e.printStackTrace(); 
       } catch (InstantiationException e) { 
        e.printStackTrace(); 
       } catch (IllegalAccessException e) { 
        e.printStackTrace(); 
       } 
       ImagePanel panel = new ImagePanel(new ImageIcon("image").getImage()); 
       dialog = new NewJFrame(); 
       dialog.getContentPane().add(panel); 
       dialog.setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JToolBar jToolBar1; 
    // End of variables declaration 

} 

回答

2

JToolBar創建JButton來包含您在其上設置的操作。默認情況下,JButtons是不透明的。爲了得到你描述的效果,你需要重寫JToolBar.createActionComponent。

例如:

jToolBar1 = new javax.swing.JToolBar() { 
     @Override 
     protected JButton createActionComponent(Action a) { 
      JButton jb = super.createActionComponent(a); 
      jb.setOpaque(false); 
      return jb; 
     } 
    }; 

注:因人而異視LAF使用。