Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改内容窗格的背景色_Java_Swing_User Interface_Jframe_Jpanel - Fatal编程技术网

Java 更改内容窗格的背景色

Java 更改内容窗格的背景色,java,swing,user-interface,jframe,jpanel,Java,Swing,User Interface,Jframe,Jpanel,我正在使用图形用户界面,而且我在使用窗格时没有遇到任何问题。 我的GUI分为两部分(顶部窗格和底部窗格) 我在两个窗格上都有按钮和标签,但其中一个按钮功能我想更改背景色,但它不起作用 我所做的是使用容器(称为thisContentPane)来更改整个GUI的背景色 这是我目前的代码: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TempConverter extends JFram

我正在使用图形用户界面,而且我在使用窗格时没有遇到任何问题。
我的GUI分为两部分(顶部窗格和底部窗格)

我在两个窗格上都有按钮和标签,但其中一个按钮功能我想更改背景色,但它不起作用

我所做的是使用
容器
(称为
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

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

您的问题是您的
内容面板的背景色不“可见”:您的
顶部面板和
底部面板位于其顶部:)

您应该:

if (ev.getActionCommand().equals("LIGHTGRAY")) {
    thisTopPanel.setBackground(Color.lightGray);
    thisBottemPanel.setBackground(Color.lightGray);
}
。。。并针对您的每个
if
条件执行此操作(您知道我的意思)

但这并不是最好的办法。在我看来,另一个完全有意义的选择,因为它反映了你所寻找的确切行为,将是:

topPanel.setOpaque(false);
bottomPanel.setOpaque(false);
我显然会推荐第二种选择;)



另外,既然我已经这么做了,我更喜欢使用
Color.LIGHTGRAY
(和
Color.BLACK
Color.WHITE
,等等)而不是
Color.LIGHTGRAY
,因为这些别名遵守了一个惯例,即常量必须是大写的,你是说setOpaque(true)?不,不,我的意思是
setOpaque(false)
。这将使您的面板透明,因此即使您的
topPanel
bottomPanel
正在覆盖它,您也会看到
contentPane
的背景颜色发生变化。试试看,让我们知道它是否有效;)一旦您的问题得到解决,请不要忘记;)