【问题标题】:How to change the Background color of JMenuBar and JToolBar?如何更改 JMenuBar 和 JToolBar 的背景颜色?
【发布时间】:2014-09-05 07:33:23
【问题描述】:

我想改变 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                   
}

【问题讨论】:

  • “工作不正常”是什么意思?
  • 设置一个漂亮的look-and-feel
  • 提示:添加@robzillaDev(或任何重要的@)以通知新评论的人。

标签: java swing background-color jmenubar jtoolbar


【解决方案1】:

您是否尝试在您的 JMenuBar 上设置了 setOpaque(true) ?

你也可以这样做:

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

【讨论】:

  • 好的,谢谢。它工作正常。但是,samething(color)我也想申请JToolbar。我该如何实施。请帮助我......
  • 我为 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。我想同时应用。请帮助我...谢谢。
【解决方案2】:

我假设您的意思是它没有设置颜色。 我不知道这是否是挥杆中的小故障,但我一直被告知要这样做:

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 的新 Class instend,并使用 setColor 设置颜色。

示例来自:SO: Change background and text color of JMenubar and...

【讨论】:

  • 我是使用 NetBeans IDE 创建 JMenuBar 和 JToolBar 因为我的项目是通过 IDE 而不是手动开发的。所以,我不知道如何使用上面的类。请帮助我。
  • 在你的 UI 包中创建一个类(我假设你有这样的)然后你有 menuBar = new javax.swing.JMenuBar();而是 menuBar = new BackgroundMenuBar();然后 menuBar.setColor(Color.YOUR_COLOR_CHOICH);等等
  • @user3556256 见上文
  • 好的,我知道了。但是,我用NetBeans IDE开发了这个程序。所以,IDE生成的源代码不可编辑。MenuBar也是IDE生成的代码。如何修改它.谢谢你的回复...帮帮我
  • @user3556256 我希望这不是真的 - 我自己不使用 NetBeans,但如果你不能编辑 UI,我希望有些事情是错误的
猜你喜欢
  • 2012-08-07
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
相关资源
最近更新 更多