- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里有很多问题。
到目前为止我已经写了一段代码,但有些方法很难用代码编写。
1.我不知道如何执行setDice(List dice)方法。如果我使用 for(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);
}
}
如果您能帮助我如何执行这些方法,我将不胜感激...
最佳答案
您需要使用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));
}
} }
关于java - 如何解决不同类型的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158264/
编辑:我似乎问错了这个问题。 我正在尝试寻找一种方法来查询一个集合是否在另一个集合中可用。例如: SELECT * FROM something WHERE (1, 3) IN (1, 2, 3, 4
这两种方法似乎 produce the same results ,但我一直很难真正说服人们第二种方法有效,因为它显然并不为人所知。 // Create some data var foo = { '
我一直在学习Kotlin,并且遇到过Collections API。在Kotlin之前,我一直在学习Java,并且我知道Java中有很多不同类型的Collections API。例如,我们使用List
为什么我会得到不同的行为: Collection col2 = new ArrayList(col); 集合 col2 = new ArrayList(); col2.addAll(col) 我正在与
所以我有一个代表专辑信息的 JSON 对象。给定“function updateRecords(id, prop, value)”我希望能够更新每个条目。正确的完成代码如下。 我得到了指示,粗体部分,
我想存储一个对象集合,这些对象根据它们所代表的值进行键控。这些键可以重复。例如: [4] => Bob [5] => Mary [5] => Sue [9] => Steve [10] =>
在检查 ArrayList API 时,我注意到一些看起来很奇怪的东西。 确实,这里是 ArrayList 构造函数实现,其中 Collection 作为参数传递: public ArrayList(
我正在为 API 编写一个 swagger 定义文件。 API 是用于 GET 请求的 /path/to/my/api: get: summary: My Custom API d
我知道scala.collection包中有两个非常有用的对象,可以帮助我们实现这个目标: JavaConverters(如果我想明确说明并准确说明我要转换的内容) JavaConversions(如
我已经阅读了无数其他帖子,但似乎无法弄清楚发生了什么,所以是时候寻求帮助了。 我正在尝试将包含集合的域实体映射到也包含集合的 dtos。 这是一个原始示例; (我提前为代码墙道歉,我尽量保持简短):
我正在创建一个具有 ArrayList 的类,因此当我调用构造函数时,它会初始化该数组: public class ElementsList { private ArrayList list;
我正在阅读事件指南和指南的开头,它说: You can also add an event listener to any element in the this.$ collection using
我是 Python 新手,想知道如何使用键在字典中存储不同数据类型的列表 例如 - {[Key1,int1,int1,String1] , [Key2,int2,int2,String2], [Key
int[] mylist = { 2, 4, 5 }; IEnumerable list1 = mylist; list1.ToList().Add(1); // why 1 does not get
我在 UI 表单中的每一行之后将以下内容添加到 HashMap 集合中 声明 Map> map = new HashMap>(); List valSetOne = new ArrayList();
我正在开发我的第一个 Java 项目,我有一个问题。问题应该很简单(虽然代码不是那么短,但没有理由被吓倒:))。我创建了一个基本的角色扮演游戏,并且有一个定义每个角色的抽象类“Character”。在
我正在开发一款应用程序,可以为用户收集推文、Facebook 状态和 Facebook 照片。目前,用户确切地设定了他们希望这种收获发生的时间和时间,并且蜘蛛会在此期间拉取数据。 when 和 to
有谁知道在 C# 中是否有与 Java 的 Set 集合等效的好方法?我知道您可以通过填充但忽略值来使用 Dictionary 或 HashTable 在某种程度上模仿集合,但这不是一种非常优雅的方式
EXISTS 该函数返回 集合中第一个元素的索引,如果集合为空,返回NULLNULLNULL Collecti
RDF集合是通过属性 rdf:parseType="Collection" 来描述仅包含指定成员的组 rdf:parseType="Collection" 属
我是一名优秀的程序员,十分优秀!