简体   繁体   中英

JMenuBar Background Color Not Showing

I'm taking an introductory Java course and we're learning Swing (I know, I know, JavaFX is the latest...). Anyway, I've got GridBagLayout working, but for some reason my JMenuBar won't show a background color. I've done some digging and haven't found anything. Here's my code:

package com.company;

import java.awt.*;
import javax.swing.*;

public class BasicGraphics {

public static void main(String[] args) {
    JFrame myframe = new JFrame("Pleasant's GUI");
    myframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JMenuBar myMenu = new JMenuBar();
    myMenu.setBackground(Color.ORANGE);
    myMenu.setOpaque(true);
    myMenu.setPreferredSize(new Dimension(400,25));
    myframe.setJMenuBar(myMenu);
    Container contents = myframe.getContentPane();
    contents.setBackground(Color.blue);
    contents.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 5;
    JLabel lblName = new JLabel("Name");
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.33;
    contents.add(lblName, c);
    JTextField tfName = new JTextField("Enter Name", 20);// columns and text
    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1;
    contents.add(tfName, c);
    JLabel lblPwd = new JLabel("Password");
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0.33;
    contents.add(lblPwd, c);
    JPasswordField pwdPassWord = new JPasswordField(10);
    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 1;
    contents.add(pwdPassWord, c);
    JLabel lblGender = new JLabel("Gender");
    c.gridx = 0;
    c.gridy = 2;
    c.weightx = 0.33;
    contents.add(lblGender, c);
    ButtonGroup bgGender = new ButtonGroup();
    JRadioButton rbMale = new JRadioButton("Male");
    JRadioButton rbFemale = new JRadioButton("Female");
    bgGender.add(rbMale);
    bgGender.add(rbFemale);
    c.gridx = 1;
    c.gridy = 2;
    c.weightx = 0.5;
    contents.add(rbMale, c);
    c.gridy = 3;
    contents.add(rbFemale, c);
    //add to panel and show flow layout by changing order
    //& resize window.  Change BorderLayout to CENTER
    myframe.pack();
    myframe.setVisible(true);
}
}

Thanks!

Here is my code which is work in window 7 with jdk 1.8 which color the JMenuBar as well as JMenu also.

    import java.awt.*;
    import javax.swing.*;

    public class BasicGraphics
    {
        JFrame frame;
        JMenuBar menuBar;
        JMenu fileMenu;
        JMenu editMenu;
        public BasicGraphics()
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            menuBar = new JMenuBar();
            menuBar.setOpaque(true);
            menuBar.setBackground(Color.BLUE);

            fileMenu = new JMenu("File");
            fileMenu.setBackground(Color.RED);

            editMenu = new JMenu("Edit");
            editMenu.setBackground(Color.YELLOW);

            menuBar.add(fileMenu);
            menuBar.add(editMenu);

            frame.setJMenuBar(menuBar);
            frame.setVisible(true);
            frame.pack();
        }
        public static void main(String args[]) {
            new BasicGraphics();
    }
 }  

setOpaque(true); is definitely work , it color the JMenuBar , I am not understand that why is not work in your computer.Your code is also color the JMenuBar in orange.Here is the Image

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM