-1

I have written a code that has a label and a button in a frame. I have also changed the background but it never changes.

    import javax.swing.*;
    import java.awt.*; 
    import javax.swing.border.*;
    public class Frames 
    {
    JFrame Main_Menu=new JFrame("MAIN MENU");JFrame CIRCUMFERENCE=new JFrame("CIRCUMFERENCE");
    JFrame AREA=new JFrame("AREA");JFrame PERIMETER=new JFrame("PERIMETER");JFrame SETS=new   JFrame("SETS");
    JFrame FUNDAMENTAL_OPRATIONS=new JFrame("FUNDAMENTAL OPRATIONS");JFrame POWER_AND_ROOTS=new JFrame("POWER_AND_ROOTS");
    void Main_Menu()
    {
        JPanel contentPane = (JPanel) Main_Menu.getContentPane();
        contentPane.setLayout(new BorderLayout(10,10));
        contentPane.setBorder(new EmptyBorder(300, 150, 300, 150));
        contentPane.setLayout(new GridLayout(4, 4));
        JPanel buttonPanel = new JPanel(new GridLayout(8,8));
        contentPane.add(Labels.Main_MENU,BorderLayout.NORTH);
        contentPane.add(Buttons.SETS,BorderLayout.SOUTH);
        Main_Menu.setBackground(Color.YELLOW);
        Main_Menu.pack();
        Main_Menu.setVisible(true);
      }
      }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3577735
  • 1
  • 1
  • 2
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Jun 01 '14 at 07:31
  • `contentPane.setLayout(new GridLayout(4, 4)); .. contentPane.add(Labels.Main_MENU,BorderLayout.NORTH);` Makes no sense. We cannot use `BorderLayout` constraints effectively if the layout is `GridLayout`! – Andrew Thompson Jun 01 '14 at 07:33
  • BTW - that code does not compile. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jun 01 '14 at 07:36

1 Answers1

3

You should actually be setting the background color of the content pane via getContentPane().setBackground(Color.YELLOW):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;

public class Frames extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Frames();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Frames() {
        setSize(new Dimension(100, 100));
        setTitle("MAIN MENU");
        getContentPane().setBackground(Color.YELLOW);
        setVisible(true);
    }

}

Also, consider using variable naming conventions; for instance, Main_Menu should be named as mainMenu.

David Yee
  • 3,515
  • 25
  • 45