更改ContentPane的背景颜色

时间:2014-04-08 23:25:32

标签: java swing user-interface jframe jpanel

我正在制作一个GUI,而且我在使用窗格方面没有遇到任何问题 我的GUI分为两部分topPanebottomPane)。

我在两个窗格上都有按钮和标签,但其中一个按钮功能我想要更改背景颜色,但它没有完成这项工作。

我所做的是我使用Container(称为thisContentPane)来更改整个GUI的背景颜色。

这是我目前的代码:

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

public class TempConverter extends JFrame
{
    //Creating a contentPane down inside the inner class
    Container thisContentPane;
    //class scope variables : DO NOT CREATE THIS OBJECTS HERE.
    JButton calculateButton, clearButton;
    JTextField celsiusField, fahrenheitField, kelvinField;

    //menu
    JMenuBar menuBar = new JMenuBar();
    JMenu backgroundColor = new JMenu("Background Color");
    JMenu help = new JMenu("Help");

    JMenuItem lightGray, white, black, blue, howToUse, about;


    //constructor 
    TempConverter()
    {
        super("Temperature Converter App");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setSize(400,200);;
        this.setLocationRelativeTo(null);


        //menuBar
        this.setJMenuBar(menuBar);


        menuBar.add(backgroundColor);

        //adding JMenu to JMenuBar
        menuBar.add(backgroundColor);
        menuBar.add(help);

        //adding JMenuItems
        lightGray = backgroundColor.add("LIGHTGRAY");
        white = backgroundColor.add("WHITE");
        black = backgroundColor.add("BLACK");
        blue = backgroundColor.add("BLUE");

        howToUse = help.add("How To Use");
        about = help.add("Help");


        //babysitter
        MaryPoppins babysitter = new MaryPoppins();

        //adding action listener to the menu item
        lightGray.addActionListener(babysitter);
        white.addActionListener(babysitter);
        black.addActionListener(babysitter);
        blue.addActionListener(babysitter);
        howToUse.addActionListener(babysitter);
        about.addActionListener(babysitter);


        //building JPanels
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(3,2,0,20));

        //add this to JFrame in centerzone
        this.add(topPanel, BorderLayout.CENTER);

        //bottom panel
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());

        //add this to JFrame in bottom
        this.add(bottomPanel, BorderLayout.SOUTH);

        //add components to the panels
        //add the buttons
        calculateButton = new JButton("Calculate");
        clearButton = new JButton("Clear");

        //add buttons
        bottomPanel.add(calculateButton);
        bottomPanel.add(clearButton);

        //register listeners
        calculateButton.addActionListener(babysitter);
        clearButton.addActionListener(babysitter);

        //add components to the top panel
        JLabel labelOne = new JLabel("Celsius:");
        JLabel secondOne = new JLabel("Fahrenheit:");
        JLabel thirdOne = new JLabel("Kelvin:");

        celsiusField = new JTextField("");
        fahrenheitField = new JTextField("");
        kelvinField = new JTextField("");

        //add the label and text fields
        topPanel.add(labelOne);
        topPanel.add(celsiusField);
        topPanel.add(secondOne);
        topPanel.add(fahrenheitField);
        topPanel.add(thirdOne);
        topPanel.add(kelvinField);

        this.setVisible(true);

    } // end constructor

    public static void main (String[] args) {
        new TempConverter();
    }


    private class MaryPoppins implements ActionListener
    {   

        //implement  the abstract method from the interface
        public void actionPerformed(ActionEvent ev)
        {
            thisContentPane = getContentPane();

            if(ev.getActionCommand().equals("LIGHTGRAY"))
            {
                thisContentPane.setBackground(Color.lightGray);
            }
            else if (ev.getActionCommand().equals("BLUE"))
            {
                thisContentPane.setBackground(Color.BLUE);
            }
            else if(ev.getActionCommand().equals("WHITE") )
            {
                thisContentPane.setBackground(Color.WHITE);
            }   
            else if (ev.getActionCommand().equals("BLACK"))
            {
                thisContentPane.setBackground(Color.BLACK);
            }else if (ev.getActionCommand().equals("Clear"))
            {
                thisContentPane.setBackground(Color.BLACK);
            }
            else if (ev.getActionCommand().equals("BLACK"))
            {
                thisContentPane.setBackground(Color.BLACK);
            }



        }//end ActionPerformed()

    }//end inner class 

} // end class

当我点击按钮或菜单项时,它什么也没做。

1 个答案:

答案 0 :(得分:2)

您的问题是contentPanel的背景颜色不是“可见”:您的topPanelbottomPanel位于其上方:)

你应该这样做:

if (ev.getActionCommand().equals("LIGHTGRAY")) {
    thisTopPanel.setBackground(Color.lightGray);
    thisBottemPanel.setBackground(Color.lightGray);
}

...并为您的每个if条件(您知道我的意思)执行此操作。

但这并不是最好的方式。在我看来,另一种选择是完全正确的,因为它反映了你正在寻找的确切行为,将是:

topPanel.setOpaque(false);
bottomPanel.setOpaque(false);

我显然会推荐第二个选项;)


此外,由于我在这,我更喜欢使用Color.LIGHTGRAY(和Color.BLACKColor.WHITE等)而不是Color.lightGrey,因为这些别名遵循惯例,声明常量必须为大写