1

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

2 回答 2

5

您是否尝试在您的 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 );
于 2014-07-15T08:48:10.203 回答
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:更改 JMenubar 的背景和文本颜色和...

于 2014-07-15T07:52:42.393 回答