我该如何解决不同类型的收藏

时间:2013-11-23 03:44:29

标签: java

这里有很多问题。

到目前为止我编写了一个代码,但在某些方法中很难用代码编写。

1.我不知道如何做setDice(List dice)方法。如果我使用(JButton b:dice),那么它一直给我编译时错误。

2.帮助我实现setWordIsCorrect(boolean isCorrect)& clearCurrentWord()方法

确保您无法触及其他方法并修改方法签名。

package cse1030.games.boggle;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * The view for the Boggle app. Please see the lab for a detailed description of
 * the view.
 * 
 * @author CSE1030_F13_14
 * 
 */
public class BoggleView extends JFrame implements ActionListener {

    /**
     * The string representing the clear command. The view listens for its own
     * clear event.
     */
    public static final String CLEAR_COMMAND = "clear";

    /**
     * The string representing the roll command.
     */
    public static final String ROLL_COMMAND = "roll";

    /**
     * The string repesenting the submit command.
     */
    public static final String SUBMIT_COMMAND = "submit";

    /**
     * A list that contains references to the buttons representing the dice.
     */
    private List<JButton> diceButtons;

    /**
     * The text field that displays the current word.
     */
    private JTextField word;

    /**
     * The set of dice buttons that have already been used to form the current
     * word.
     */
    private Set<JButton> usedButtons;

    /**
     * The text area that displays the list of correct words.
     */
    private JTextArea correctWords;

    /**
     * The text area that displays the list of incorrect words.
     */
    private JTextArea incorrectWords;

    /**
     * Create the Boggle user interface. Please see the lab for a detailed
     * description of the user interface.
     * 
     * @param controller
     *          the controller that listens for submit and roll events
     */
    public BoggleView(BoggleController controller) {
        super("Boggle");
        this.diceButtons = new ArrayList<JButton>();
        this.usedButtons = new HashSet<JButton>();

        JPanel contentPanel = new JPanel();
        JPanel leftPanel = this.makeLeftPanel();
        JPanel rightPanel = this.makeRightPanel();
        JPanel middlePanel = this.makeMiddlePanel(controller);
        contentPanel.add(leftPanel);
        contentPanel.add(middlePanel);
        contentPanel.add(rightPanel);
        this.setContentPane(contentPanel);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Creates the panel that contains the buttons representing the Boggle dice.
     * 
     * @return the <code>JPanel</code> that contains the buttons representing the
     *         Boggle dice.
     * 
     */
    private JPanel makeDicePanel() {
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, 32);
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(4, 4));
        p.setMaximumSize(new Dimension(450, 450));
        for (int i = 0; i < 16; i++) {
            JButton b = new JButton("" + i);
            b.setPreferredSize(new Dimension(100, 100));
            b.setMaximumSize(b.getSize());
            b.setFont(font);
            b.setBackground(Color.WHITE);
            b.setActionCommand("" + i);
            b.addActionListener(this);
            p.add(b);
            this.diceButtons.add(b);
        }
        return p;
    }

    /**
     * Returns the buttons surrounding the button representing the die that was
     * last selected by the user. These are the buttons that could legally be
     * chosen next by the user when forming a word.
     * 
     * @param idx
     *          the index of the button representing the die that was last
     *          selected by the user
     * @return the buttons surrounding the last selected die
     */
    private List<JButton> findNeighbors(int idx) {
        List<JButton> neighbors = new ArrayList<JButton>();
        final int row = idx / 4;
        final int col = idx % 4;
        final int minRow = Math.max(0, row - 1);
        final int maxRow = Math.min(3, row + 1);
        final int minCol = Math.max(0, col - 1);
        final int maxCol = Math.min(3, col + 1);
        for (int i = minRow; i <= maxRow; i++) {
            for (int j = minCol; j <= maxCol; j++) {
                int n = i * 4 + j;
                if (n != idx) {
                    neighbors.add(this.diceButtons.get(n));
                }
            }
        }
        return neighbors;
    }

    /**
     * Disable all of the buttons representing the dice.
     */
    private void disableAllDiceButtons() {
        for (JButton b : this.diceButtons) {
            b.setEnabled(false);
        }
    }

    /**
     * Enable all of the buttons representing the dice.
     */
    private void enableAllDiceButtons() {
        for (JButton b : this.diceButtons) {
            b.setEnabled(true);
            b.setBackground(Color.WHITE);
        }
    }

    /**
     * Responds to events from the view. This method responds to an event where
     * the action command is either <code>BoggleView.CLEAR_COMMAND</code>,
     * <code>BoggleView.ROLL_COMMAND</code>, or
     * <code>BoggleView.SUBMIT_COMMAND</code>.
     * 
     * @param event
     *          an event emitted by the view
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals(CLEAR_COMMAND)) {
            this.clearCurrentWord();
        } else if (command.equals(ROLL_COMMAND)) {
            this.clearCorrectWords();
            this.clearIncorrectWords();
            this.clearCurrentWord();
        } else {
            try {
                int d = Integer.parseInt(command);
                JButton b = this.diceButtons.get(d);
                b.setBackground(Color.BLUE);
                this.word.setText(this.word.getText() + b.getText());
                this.usedButtons.add(b);
                this.disableAllDiceButtons();
                List<JButton> neighbors = findNeighbors(d);
                for (JButton n : neighbors) {
                    if (!this.usedButtons.contains(n)) {
                        n.setEnabled(true);
                    }
                }
            } catch (NumberFormatException ex) {

            }
        }
    }

    /**
     * Creates the left-hand panel. Please see the lab for a detailed description
     * of the panel's contents.
     * 
     * @return the left-hand <code>JPanel</code> with all of its necessary
     *         components
     */
    private JPanel makeLeftPanel() {
        // create the panel
        JPanel p = new JPanel();

        // set the layout for the panel to use a BoxLayout;
        // BoxLayout stacks its components vertically or horizontally
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

        // create a label for the list of correct words and add it to the panel
        JLabel label = new JLabel("Correct Words");
        p.add(label);

        // create the list of correct words, remove the ability for the user to
        // edit the list, and add it to the panel
        this.correctWords = new JTextArea(30, 16);
        this.correctWords.setEditable(false);
        p.add(this.correctWords);

        return p;
    }

    /**
     * Creates the right-hand panel. Please see the lab for a detailed description
     * of the panel's contents.
     * 
     * @return the right-hand <code>JPanel</code> with all of its necessary
     *         components
     */
    private JPanel makeRightPanel() {
        JPanel p = new JPanel();

        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        JLabel label = new JLabel("Incorrect Words");
        p.add(label);

        this.incorrectWords = new JTextArea(30, 16);
        this.incorrectWords.setEditable(false);
        p.add(this.incorrectWords);

        return p;
    }

    /**
     * Creates the middle panel. Please see the lab for a detailed description of
     * the panel's contents.
     * 
     * @return the middle <code>JPanel</code> with all of its necessary components
     */
    private JPanel makeMiddlePanel(BoggleController controller) {
        JPanel p = new JPanel();

        // 1. set the layout to a BoxLayout (same as makeLeftPanel)
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

        // 2. make the dice panel and add it to p; there is a method that makes the
        // dice panel for you!
        p.add(makeDicePanel());

        // 3. make the contorl panel and add it to p; there is a method that makes
        // the control for you!
        p.add(makeControlPanel(controller));

        return p;
    }

    /**
     * Creates the panel that contains the clear, submit, and re-roll buttons, and
     * the text field for the word.
     * 
     * @return the <code>JPanel</code> that contains the controls below the dice
     * 
     */
    private JPanel makeControlPanel(BoggleController controller) {
        JPanel p = new JPanel();

        // You don't need to create a lay out. JPanel uses FlowLayout if you don't
        // specify a lay out.

        // Make the clear button
        JButton clear = new JButton("Clear");

        // Set its action command to the clear command
        clear.setActionCommand(BoggleView.CLEAR_COMMAND);

        // Add this as an action listener; see the actionPerformed method above.
        // The controller does not need to listen to this button because the model
        // is not needed to clear the current word.
        clear.addActionListener(this);

        // Add the clear button to the panel.
        p.add(clear);

        // Make a text field that can display a 16 character word
        this.word = new JTextField(16);

        // Disable editing by the user.
        this.word.setEditable(false);

        // Add the text field to the panel.
        p.add(this.word);

        // - make the submit button
        JButton submit = new JButton("Submit");

        // - set its action command
        submit.setActionCommand(BoggleView.SUBMIT_COMMAND);

        // - add the controller as an action listener
        submit.addActionListener(controller);

        // - add the submit button to the panel
        p.add(submit);

        // - make the re-roll button
        JButton roll = new JButton("Re-roll");

        // - set its action command
        roll.setActionCommand(BoggleView.ROLL_COMMAND);

        // - add the controller as an action listener
        roll.addActionListener(controller);

        // - add this as an action listener
        roll.addActionListener(this);

        // - add the re-roll button to the panel
        p.add(roll);

        return p;
    }

    /**
     * Get the current string that is in the word text field.
     * 
     * @return the current string that is in the word text field
     */
    public String getWord() {
        // change the return statement below
        return this.word.getText();
    }

    /**
     * Sets the text on the buttons representing the dice.
     * 
     * @pre. <code>dice.size() == 16</code>
     * 
     * @param dice
     *          a list of 16 Boggle dice
     */
    public void setDice(List<BoggleDie> dice) {
        **for (JButton b : dice) {
            b.setText(b.getText());
        }**
    }

    /**
     * Causes the view to update after the submitted word is evaluated for
     * correctness. If <code>isCorrect == true</code> then the current word is
     * added to the list of correct words. If <code>isCorrect == false</code> then
     * the current word is added to the list of incorrect words. In both cases,
     * the current word is cleared.
     * 
     * @param isCorrect
     *          <code>true</code> if the current word has been determined to be a
     *          legal Boggle word, <code>false</code> otherwise
     */
    public void setWordIsCorrect(boolean isCorrect) {
        if(isCorrect == true) {

        } else {

        }
    }

    /**
     * Clears the list of correct words.
     */
    private void clearCorrectWords() {
        this.correctWords.setText(null);
    }

    /**
     * Clears the list of incorrect words.
     */
    private void clearIncorrectWords() {
        this.incorrectWords.setText(null);
    }

    /**
     * Clears the current word and prepares the view to accept a new word. This
     * requires re-enabling all of the dice buttons and clearing the set
     * this.usedButtons
     */
    private void clearCurrentWord() {
        // 1. enable all of the dice buttons; there is a method that does this for
        // you
        enableAllDiceButtons();

        // 2. set the text of this.word to the empty string


        // 3. remove all of the buttons from this.usedButtons

    }

    public static void main(String[] args) {
        BoggleView v = new BoggleView(null);
        v.setVisible(true);
    }

}

如果你能帮助我如何做这些方法,我将不胜感激......

2 个答案:

答案 0 :(得分:1)

您需要使用for (BoggleDie b : dice)(List<JButton> dice)。如果列表包含BoggleDie,则必须使用前者。

您可能希望获得一个JButton列表

public void setDice(List<JButton> dice) {
    for (JButton b : dice) {
        b.setText(b.getText());
    }
}

您需要确保实际初始化列表并将JButton添加到列表中,然后才能对其执行任何操作。

List<JButton> diceButtons = new List<JButton>();

for (int i = 0; i < 16; i++){
    diceButton.add(new JButton("" + i + 1);
}

尝试使用此方法

public void setDice(){
    for (int i = 0; i < 16; i++){
        diceButtons.add(new JButton("SomeText");
    }
}

编辑:使用BoggleDie类

public class BoggleDie{
    int value;

    public BoggleDie(int value){
        this.value = value;
    }
}

public class BoggleView ... {
    ...
    private List<BoggleDie> dice = new List<BoggleDie>();
    private List<JButton> diceButton = new ArrayList<JButton>();
    public BoggleView(){

        for (int i = 0; i < 16; i++){
            dice.add(new BoggleDie(i)); // or whatever value you want for the die
        }

        setDice(dice);
    }

    public void setDice(List<BoggleDie> dice) {
        for (BoggleDie b : dice) {
            diceButtons.add(new JButton(b.value));
    }

}     }

答案 1 :(得分:0)

更改代码如下

public void setDice(List<JButton> dice) {
    for (JButton b : dice) {
        b.setText(b.getText());
    }
}
相关问题