bluej中的背景颜色不会改变

时间:2014-06-01 07:28:52

标签: java swing background jframe awt

我编写了一个代码,在框架中有一个标签和一个按钮。我也改变了背景但它永远不会改变。

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

1 个答案:

答案 0 :(得分:3)

您应该通过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);
    }

}

另外,请考虑使用变量命名约定;例如,Main_Menu应命名为mainMenu