【问题标题】:Changing background color of a ContentPane更改 ContentPane 的背景颜色
【发布时间】:2014-04-08 23:25:32
【问题描述】:

我正在开发 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

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

【问题讨论】:

    标签: java swing user-interface jframe jpanel


    【解决方案1】:

    您的问题是您的contentPanel 的背景颜色不“可见”:您的topPanel 和您的bottomPanel 在它上面:)

    你应该这样做:

    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,因为这些别名遵守规定的约定常量必须大写

    【讨论】:

    • 你的意思是 setOpaque(true)?
    • 不,不,我的意思是setOpaque(false)。这将使您的面板透明,因此即使您的topPanelbottomPanel 覆盖了它,您也会看到contentPane 的背景颜色发生变化。试试看,让我们知道它是否有效;)
    • 问题解决后别忘了mark the answer as accepted ;)
    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2013-08-08
    • 1970-01-01
    • 2012-04-21
    • 2017-08-11
    相关资源
    最近更新 更多