2014-07-15 1 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

당신이 당신의 JMenuBar로 (참) setOpaque하려고 했나 :에서

예 걸릴?

또한이 작업을 수행 할 수 있습니다

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에 다음 코드를 적용했습니다. 반환 값 : 새로운 BasicToolBarUI (새로운 BasicToolBarUI() { @Override 공백 페인트 (그래픽 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); 

    } 
} 

이 그럼 당신은 setColor를로는 A의 JMenuBar의 새로운 클래스 instend 및 설정 색상을 사용합니다. SO: Change background and text color of JMenubar and...

+0

NetBeans IDE를 사용하여 JMenuBar 및 JToolBar를 만들었습니다. 프로젝트가 IDE가 아닌 수동으로 개발 중이기 때문에 위의 클래스를 사용하는 방법을 모르겠습니다. 제발 도와주세요. – user3556256

+0

UI 패키지에 클래스를 만들고 (내가 가지고 있다고 생각한 다음) menuBar = new javax.swing.JMenuBar(); 대신 menuBar = new BackgroundMenuBar(); 다음 menuBar.setColor (Color.YO_COLOR_CHOICH); 등등 –

+0

@ user3556256 위의 내용을 참조하십시오. –

관련 문제