【问题标题】:Background color won't change in panel面板中的背景颜色不会改变
【发布时间】:2013-10-06 03:54:12
【问题描述】:

以下代码会生成一个带有按钮的窗口,但是当我运行 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;
}

【问题讨论】:

  • frame.show(); 不要忽略弃用警告。
  • +1 表示sscce

标签: java swing jpanel setbackground


【解决方案1】:

一种方法是将nest ColorAction 作为ButtonPanel 中的一个内部类,它可以隐式访问封闭面板。

附录:正如 @Andrew Thompson 和 @nachokk 在 cmets 中所指出的,隐式可访问性可以通过使用封闭类名称限定 this 来明确。有关详细信息,请参阅JLS §15.8.4. Qualified this。在这个例子中,这两个调用是等价的:

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

作为更通用的替代方案,考虑将目标面板和颜色封装在Action 中,如here 所述。

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

【讨论】:

  • 另见§15.8.4. Qualified this。 -- 这是在(现已删除)答案上发表的评论,是唯一值得重复的部分。
  • @AndrewThompson:我听从你的意见,但如果你恢复你的答案,我发现你为清楚起见的论点是有说服力的。
  • 这个问题的大部分答案都是错误的(鉴于代码结构)。如果我确实复活了其中的任何部分,那只会作为评论。
  • 另外,@AndrewThompson 感谢您的帮助,并表示您的“this.setbackground”修复不正确。我之前曾尝试过,但担心我的编译器有问题导致它失败。
  • 很高兴你把它整理好了。 :)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-21
      • 2014-04-21
      • 2021-01-23
      • 2011-06-15
      • 2022-06-11
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多