2014-07-15 57 views
1

我想改變JMenuBar和JToolBar的背景顏色。爲此,我嘗試過但沒有工作。我遵循了某些網站提供的解決方案。但是,這些也不能正常工作。如何更改JMenuBar和JToolBar的背景顏色?

這裏是我的代碼:

import java.awt.Color; 

public class JFrameDemo extends javax.swing.JFrame { 

public JFrameDemo() { 

    Color b=new Color(0,150,255); 
    initComponents(); 
    menuBar.setForeground(Color.GREEN); 
    toolbar.setBackground(b); 
} 

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

    toolbar = new javax.swing.JToolBar(); 
    close = new javax.swing.JButton(); 
    open = new javax.swing.JButton(); 
    menuBar = new javax.swing.JMenuBar(); 
    jMenu3 = new javax.swing.JMenu(); 
    jMenu4 = new javax.swing.JMenu(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    toolbar.setRollover(true); 

    close.setText("close"); 
    close.setFocusable(false); 
    close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 
    close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
    toolbar.add(close); 

    open.setText("open"); 
    open.setFocusable(false); 
    open.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 
    open.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
    toolbar.add(open); 

    menuBar.setBackground(new java.awt.Color(51, 51, 255)); 

    jMenu3.setText("File"); 
    menuBar.add(jMenu3); 

    jMenu4.setText("Edit"); 
    menuBar.add(jMenu4); 

    setJMenuBar(menuBar); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(toolbar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(0, 306, Short.MAX_VALUE)) 
    ); 

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

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(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(JFrameDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      // ImageIcon img = new ImageIcon("C:\\Icons\book-edit-icon.png"); 
      JFrameDemo fdemo=new JFrameDemo(); 

      fdemo.setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
private javax.swing.JButton close; 
private javax.swing.JMenu jMenu3; 
private javax.swing.JMenu jMenu4; 
private javax.swing.JMenuBar menuBar; 
private javax.swing.JButton open; 
private javax.swing.JToolBar toolbar; 
// End of variables declaration     
} 
+0

你什麼意思 '不能正常工作' 呢? – robzillaDev

+0

設置一個不錯的[標籤:外觀]。 –

+0

提示:添加@robzillaDev(或者,'@'是重要的)* *通知*新的評論的人。 –

回答

5

你有沒有嘗試setOpaque(真)對你的JMenuBar?

你也可以這樣做:

menuBar.setUI (new BasicMenuBarUI(){ 
    public void paint (Graphics g, JComponent c){ 
     g.setColor (YOUR_COLOR_HERE); 
     g.fillRect (0, 0, c.getWidth(), c.getHeight()); 
    } 
}); 

或一般:

UIManager.put ("MenuBarUI", YOUR_SPECIFIC_UI_HERE); 
+0

好的,謝謝你工作的很好。但是,我想申請JToolbar的東西(顏色)也是如此。我可以如何實現。請幫助我...... – user3556256

+0

我已經爲JToolBar應用了以下代碼。 toolbar.setUI(new BasicToolBarUI(){ @Override public void paint(Graphics g,JComponent c){g.setColor(Color.BLUE); g.fillRect(0,0,c.getWidth(),c .getHeight()); } });我已經爲JMenubar和JToolbar應用了顏色。但是,結果隱藏了Jtoolbar,並且顏色被應用於JMenubar。我想申請這兩種顏色。請幫助我...謝謝。 – user3556256

2

我假設你的意思是這麼想的設置顏色。 我不知道,如果是在揮杆glich,但我一直被告知做這種方式:

public class BackgroundMenuBar extends JMenuBar 
{ 
    Color bgColor=Color.WHITE; 

    public void setColor(Color color) 
    { 
     bgColor=color; 
    } 

    @Override 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setColor(bgColor); 
     g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1); 

    } 
} 

然後你使用的JMenuBar的新類instend,並設置色​​彩搭配的setColor。

例採取從:SO: Change background and text color of JMenubar and...

+0

我是通過使用NetBeans IDE創建JMenuBar和JToolBar的。因爲我的項目是通過IDE開發而不是手動的。所以,我不知道如何使用上面的類。請幫助我。 – user3556256

+0

在你的UI包中創建一個類(我假設你有這樣的)然後你有menuBar = new javax.swing.JMenuBar();而不是menuBar =新的BackgroundMenuBar();然後menuBar.setColor(Color.YOUR_COLOR_CHOICH); –

+0

@ user3556256參見上面 –