在外部类中,我多次调用这些方法,但是setBackground()仅在我首次调用这些方法中的任何时候才起作用。

例如,我有对象CellPane cellpane1, cellpane2,当我第一次调用cellpane1.drawPlayer()时,它按预期工作,当我第一次调用cellpane2.drawWall()时,它也按预期工作。但是,当我在调用cellpane1.drawWall()后为cellpane1,cellpane1.drawPlayer()调用drawWall()时,它不起作用。背景颜色没有改变。

我需要重新粉刷或重新失效,还是什么?我是这些东西的新手。 :(我做错了什么?

private static class CellPane extends JPanel {

    private Color defaultBackground;

    private CellPane() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                defaultBackground = getBackground();
                setBackground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setBackground(defaultBackground);
            }
        });
    }

    private void drawDefault() {
        setBackground(defaultBackground);
    }

    private void drawBoxOnGoal() {
        defaultBackground = getBackground();
        setBackground(Color.decode("#4a3d32"));
    }

    private void drawPlayerOnGoal() {
        defaultBackground = getBackground();
        setBackground(Color.decode("#f9cda8"));
    }

    private void drawGoal() {
        defaultBackground = getBackground();
        setBackground(Color.decode("#f9cda8"));
    }

    private void drawBox() {
        defaultBackground = getBackground();
        setBackground(Color.decode("#7a5230"));
    }

    private void drawPlayer() {
        defaultBackground = getBackground();
        setBackground(Color.YELLOW);

    }

    private void drawWall() {
        defaultBackground = getBackground();
        setBackground(Color.RED);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }

}


[编辑]

例:

public class MainClass {



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

private MainClass() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { }

            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            frame.add(new Map());

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

private static class Map  extends JPanel implements ActionListener{

    CellPane cellPane;
    boolean flag = true;

    JButton button = new JButton("button");

    public Map() {
        setLayout(new GridBagLayout());

        button.addActionListener(this);

        add(button);

        create(flag);
    }

    public void create(boolean flag) {
        GridBagConstraints gbc = new GridBagConstraints();

        cellPane = new CellPane();
        gbc.gridx = 0;
        gbc.gridy = 0;
        if(flag)
            cellPane.drawPlayer();
        else cellPane.drawWall();
        add(cellPane, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        flag = false;
        create(flag);
    }

}


@SuppressWarnings("serial")
private static class CellPane extends JPanel {

    private Color defaultBackground;


    private void drawPlayer() {
        defaultBackground = getBackground();
        setBackground(Color.YELLOW);

    }

    private void drawWall() {
        defaultBackground = getBackground();
        setBackground(Color.RED);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }

}}


我知道这不是一个完美的例子,但可以运行。所以我想在单击按钮时更改cellPane的背景。当flag为true且背景设置为YELLOW时,构造函数将调用create(flag)。然后单击按钮,标志变为false并调用create(flag),其中flag = false,因此它将调用cellPane.setWall()来设置背景红色,但不会。

我做错了什么?也许有些简单,但是我对这些东西还是陌生的。

最佳答案

单击按钮时,您不会更改cellPane的颜色。您正在创建另一种颜色不同的cellPane,并将其添加到与现有单元相同的gridbag单元中。

只需更改颜色,它将正常工作:

    public Map() {
        setLayout(new GridBagLayout());

        button.addActionListener(this);

        add(button);

        create();
        draw();
    }

    private void draw() {
        if (flag) {
            cellPane.drawPlayer();
        }
        else {
            cellPane.drawWall();
        }
    }

    private void create() {
        GridBagConstraints gbc = new GridBagConstraints();

        cellPane = new CellPane();
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(cellPane, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        flag = false;
        draw();
    }

10-06 13:41