繁体   English   中英

面板中的背景颜色不会改变

[英]Background color won't change in panel

下面的代码生成一个带按钮的窗口,但是当我运行i并按下按钮时会弹出一条错误消息。 根据Spring工具提示:

Cannot make a static reference to the non-static method setBackground(Color) from the type JComponent

据我所知,这个程序实际上是从我的Java教科书行输入的。 这是一本较旧的书,因此可能存在不兼容性,但似乎不太可能。

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

public class ButtonTest
{
    public static void main(String[] args)
    {
        final ButtonFrame frame = new ButtonFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
}
}

class ButtonFrame extends JFrame
{
    public ButtonFrame()
    {
    setTitle("Button Test");
    setSize(Default_width, Default_height);

        //panel
        ButtonPanel panel = new ButtonPanel();
        Container contentPane=getContentPane();
        contentPane.add(panel);
    }

    public static final int Default_width = 300;
public static final int Default_height = 200;
}

class ButtonPanel extends JPanel
{
public ButtonPanel()
{
    JButton yellowButton = new JButton("Yellow");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("Red");

    add(yellowButton);
    add(blueButton);
    add(redButton);

    ColorAction yellowAction= new ColorAction(Color.YELLOW);
    ColorAction redAction = new ColorAction(Color.RED);
    ColorAction blueAction = new ColorAction(Color.BLUE);

    yellowButton.addActionListener(yellowAction);
    blueButton.addActionListener(blueAction);
    redButton.addActionListener(redAction);
    }
}

    class ColorAction implements ActionListener
    {
        public ColorAction(Color c)
    {
        backgroundColor=c;
    }

    public void actionPerformed(ActionEvent event)
    {
    ButtonPanel.setBackground(backgroundColor);
    }

        private Color backgroundColor;
}

一种方法是将ColorAction 嵌套ButtonPanel作为内部类 ,其中它具有对封闭面板的隐式访问。

附录:如通过@Andrew汤普森和@nachokk评论指出,隐含的可及的是由合格的明确this使用封闭类的名称。 JLS§15.8.4。 合格 this了解详情。 在此示例中,这两个调用是等效的:

 setBackground(backgroundColor);
 ButtonPanel.this.setBackground(backgroundColor);

作为一个更广泛的选择,考虑封装目标板和颜色的Action ,所概述这里

图片

class ButtonPanel extends JPanel {

    public ButtonPanel() {
        JButton yellowButton = new JButton("Yellow");
        JButton blueButton = new JButton("Blue");
        JButton redButton = new JButton("Red");

        add(yellowButton);
        add(blueButton);
        add(redButton);

        ColorAction yellowAction = new ColorAction(Color.YELLOW);
        ColorAction redAction = new ColorAction(Color.RED);
        ColorAction blueAction = new ColorAction(Color.BLUE);

        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        redButton.addActionListener(redAction);
    }

    private class ColorAction implements ActionListener {

        public ColorAction(Color c) {
            backgroundColor = c;
        }

         @Override
         public void actionPerformed(ActionEvent event) {
            setBackground(backgroundColor);
        }
        private Color backgroundColor;
    }
}

ButtonPanel.setBackground()不是静态方法,因此您无法将其称为一个静态方法。 您需要ButtonPanel的具体实例来设置背景。

ButtonPanel bp = new ButtonPanel();
bp.setBackground(backgroundColor);

外观和感觉的变化也有帮助:

//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM