在单独的 class 中编辑和更新 运行 实例变量

Edit and update a running instance variable in a separate class

我正在制作 Simon Says 风格的游戏,有四个彩色方块,计算机会执行一个序列,然后您复制它,等等,现在我想添加一些更高级的功能。我正在查看的当前功能是希望根据用户的意愿更改颜色面板的实际颜色,并能够单独更改它们。

如何让 'color panel(s)' 通过 JColorChooser 更改为新颜色,同时保持其他所有设置?

目前我把它分成了几个不同的 classes 并且在让它们全部通信并正常工作时遇到了问题。

主要class(只有一个片段):

public class Simonish implements ActionListener, MouseListener {

private ColorPanel colorPanel[] = new ColorPanel[4];
private ScorePanel scorePanel = new ScorePanel();
private Menu menuBar = new Menu();
private JPanel gameBoard = new JPanel();
private Random rand = new Random();
private ArrayList<ColorPanel> compSeq = new ArrayList<ColorPanel>();
private Iterator<ColorPanel> iter;
private JFrame frame = new JFrame();
private boolean playerTurn = false;
private int speed = 500;

public Simonish(Color[] colors){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane = (JPanel)frame.getContentPane();
    pane.setLayout(new BorderLayout());

    gameBoard.setLayout(new GridLayout(2,2));
    gameBoard.setPreferredSize(new Dimension(400,400));

    for (int i=0;i<colorPanel.length;i++){
        colorPanel[i] = new ColorPanel(colors[i]);
        colorPanel[i].addMouseListener(this);
        gameBoard.add(colorPanel[i]);
    }

    scorePanel.addStartListener(this);

    frame.setJMenuBar(menuBar);
    pane.add(scorePanel, BorderLayout.NORTH);
    pane.add(gameBoard, BorderLayout.CENTER);

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

}

我的菜单代码(构建菜单栏并实现操作):

public class Menu extends JMenuBar {

private JMenuBar menuBar = new JMenuBar();

private JMenu settings = new JMenu("Settings");
private JMenu stats = new JMenu("Stats");
private JMenu help = new JMenu("Help");

private JMenuItem chooseColor = new JMenuItem(new ChooseColorAction("Choose Color"));
private JMenuItem colorMode = new JMenuItem(new ColorModeAction("Color Mode"));
private JMenuItem hScore = new JMenuItem("High Scores");
private JMenuItem history = new JMenuItem("History");
private JMenuItem about = new JMenuItem("About");
private JMenuItem rules = new JMenuItem("Rules");

public Menu(){
    this.add(settings);
    this.add(stats);
    this.add(help);

    settings.add(chooseColor);
    settings.add(colorMode);

    stats.add(hScore);
    stats.add(history);

    help.add(about);
    help.add(rules);
}
}

操作class放置变色代码:

public class ColorModeAction extends AbstractAction {

public ColorModeAction(String name){
    super(name);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    Color[] colors = {Color.CYAN, Color.BLACK, Color.WHITE, Color.GREEN};
    //new Simonish(colors);
    //JOptionPane.showMessageDialog(null, "Color Mode");
}
}

使用接口与您的 类 进行通信。例如; ColorModeAction需要改变颜色,所以它应该以一个接口作为参数,可以改变颜色:

public interface ColorChanger {

    public void changeColor(int index, Color newColor);
} 

让 Simonish 实现该接口:

public class Simonish implements ActionListener, MouseListener, ColorChanger  {

    public void changeColor(int index, Color new Color) {
        //Change the given panel's color
    }
}

Simonish作为参数传递给菜单,并将new ColorModeAction("Color Mode")移至构造函数。然后将ColorChanger作为参数传递给ColorModeAction

public class Menu extends JMenuBar {

    ...
    private JMenuItem colorMode;
    ...

    public class Menu(ColorChanger colorChanger) {
         colorMode = new JMenuItem(new ColorModeAction(colorChanger, "Color Mode"));
    }
}

和新的 ColorModeAction:

public class ColorModeAction extends AbstractAction {

    private ColorChanger colorChanger;

    public ColorModeAction(ColorChanger colorChanger, String name) {
        super(name);

        this.colorChanger = colorChanger;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Color[] colors = { Color.CYAN, Color.BLACK, Color.WHITE, Color.GREEN };

        colorChanger.changeColor(index, Color)

    }
}

它不是完全可用的代码,但我想你明白了。