重置按钮java

时间:2017-03-27 03:07:37

标签: java swing jbutton reset

我正在创建一个模型 - 视图 - 控制器算法。如果我在我的控制器类中定义了JButton的实例。例如,我有:一个用于重置,退出,撤消,重做和设置的按钮。

我已经实现了他们的actionListeners。在我的Controller类中是actionPerformed()方法的位置。我实现了所有5个按钮。现在按钮被设置为启用。但是,当禁用重做和撤消并按下重置按钮时,它们将保持禁用状态。如何重置重做和撤消按钮的状态。

这是我的GameController类,具体是actionPerformed:

public void actionPerformed(ActionEvent e)  {
    if (e.getSource() instanceof DotButton) {
        try {
            undo.push((GameModel) gameModel.clone());
        } catch (CloneNotSupportedException g) {
            g.printStackTrace();
        }
        if(gameModel.getNumberOfSteps()==0){
            int row = ((DotButton)(e.getSource())).getRow();
            int column = ((DotButton)(e.getSource())).getColumn();
            gameModel.capture(row,column);
            gameModel.step();
        }
        selectColor(((DotButton)(e.getSource())).getColor());
    } else if (e.getSource() instanceof JButton) {
        JButton clicked = (JButton)(e.getSource());

        if (clicked.getText().equals("Quit")) {
            /*try {
                serialization.serialize(gameModel, "savedGame.ser");
            } catch (IOException f) {
                f.printStackTrace();
            }*/
             System.exit(0);
         } else if (clicked.getText().equals("Reset")){
            reset();
            if (clicked.getText().equals("Redo")) {
                clicked.setEnabled(true);
            }
            if (clicked.getText().equals("Undo")) {
                clicked.setEnabled(true);
            }
         } else if (clicked.getText().equals("Redo")) {
            if (redo.isEmpty()) {
                clicked.setEnabled(false);
            }
            try {
                redo();
            } catch (CloneNotSupportedException g) {
                g.printStackTrace();
            }
        }
         else if (clicked.getText().equals("Undo")) {
            if (undo.isEmpty()) {
                clicked.setEnabled(false);
            }
            try {
                undo();
            } catch (CloneNotSupportedException g) {
                g.printStackTrace();
            }
         }
        else if (clicked.getText().equals("Settings")) {
            gameView.settingsMenu();
        }
    } else if (e.getSource() instanceof JRadioButton) {
        JRadioButton clickedR = (JRadioButton)(e.getSource());
        if (clickedR.getText().equals("Torus")) {
            setting1 = true;
        }
        if (clickedR.getText().equals("Diagonal")) {
            setting2 = true;
        }
    }

这是我创建按钮的GameView类:

public GameView(GameModel model, GameController gameController){
    super("Flood it -- the ITI 1121 version");

    this.gameModel = model;

    this.gameController = gameController;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.WHITE);

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setLayout(new GridLayout(gameModel.getSize(), gameModel.getSize()));
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
    board = new DotButton[gameModel.getSize()][gameModel.getSize()];

    for (int row = 0; row < gameModel.getSize(); row++) {
        for (int column = 0; column < gameModel.getSize(); column++) {
            board[row][column] = new DotButton(row, column, gameModel.getColor(row,column), 
                (gameModel.getSize() < 26 ? DotButton.MEDIUM_SIZE : DotButton.SMALL_SIZE));
            board[row][column].addActionListener(gameController);
            panel.add(board[row][column]);
        }
    }

    add(panel, BorderLayout.CENTER);
    buttons = new JButton[5];
    buttons[0] = new JButton("Reset");
    buttons[1] = new JButton("Quit");
    buttons[2] = new JButton("Undo");
    buttons[3] = new JButton("Redo");
    buttons[4] = new JButton("Settings");
    for (JButton button : buttons) {
        button.setFocusPainted(false);
        button.addActionListener(gameController);
    }

    JPanel settingsPanel = new JPanel();
    settingsPanel.setBackground(Color.WHITE);
    settingsPanel.add(buttons[2]);
    settingsPanel.add(buttons[3]);
    settingsPanel.add(buttons[4]);

    JPanel control = new JPanel();
    control.setBackground(Color.WHITE);
    scoreLabel = new JLabel();
    control.add(scoreLabel);
    control.add(buttons[0]);
    control.add(buttons[1]);


    add(settingsPanel, BorderLayout.NORTH);
    add(control, BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

我应该在GameController类和GameView中创建JButton实例。

1 个答案:

答案 0 :(得分:2)

这种情况永远不可能实现

if

如果第一个true语句是true,那么第二个句子永远不会是>>> a = "   " >>> a.isspace() False >>> a '\xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 \xe2\x80\x83\xe2\x80\x83 ' >>> print a                                  >>>