Character[] buttonsToAdd = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
List<Character> shuffled = Arrays.asList(buttonsToAdd);
Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
Map<Character, JButton> buttons = new HashMap<Character, JButton>();
Map<Color, JButton> col = new HashMap<Color, JButton>();
我有这些数字列表,用于字母和4种颜色的字母。我想从字母数组列表中随机选择4个字母,将它们应用于4个按钮,然后将这4个按钮分别从颜色数组列表中分配一个随机颜色。我该怎么做呢?
我尝试过随机使用setBackground在我的for语句的按钮上,但它不允许我引用“颜色”。数组列表那样。
for (char c : buttonsToAdd) {
JButton button = new JButton(c + "");
Random rnd = new Random();
button.setBackground(new Color(rnd.nextInt(colors)));
Collections.shuffle(shuffled);
_pan.add(button);
buttons.put(c, button);
button.addKeyListener(this);
}
我还是Java的新手,是否有一种简单的方法可以限制分配给按钮的按钮的数量来自ButtonsToAdd&#39; list to 4,以及从颜色列表中为每一个分配一个随机颜色?
public class Game implements Runnable, KeyListener {
JFrame _frame = new JFrame("Window");
JPanel _pan = new JPanel();
Character[] buttonsToAdd = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
Map<Character, JButton> buttons = new HashMap<Character, JButton>();
List<Character> shuffled = Arrays.asList(buttonsToAdd);
Color[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
Map<Color, JButton> col = new HashMap<Color, JButton>();
List<Character> shuffledCharacters = Arrays.asList(buttonsToAdd);
@Override
public void run() {
_frame.add(_pan);
_frame.setVisible(true);
_pan.setLayout(new GridLayout(4, 4));
for (char c : buttonsToAdd) {
JButton button = new JButton(c + "");
Random rnd = new Random();
button.setBackground(colors[rnd.nextInt(colors.length)]);
_pan.add(button);
buttons.put(c, button);
button.addKeyListener(this);
Collections.shuffle(shuffledCharacters);
}
_frame.pack();
_frame.setLocationRelativeTo(null);
_frame.setResizable(true);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
char key = e.getKeyChar();
System.out.println(key);
JButton button = null;
if ((button = buttons.get(Character.toUpperCase(key))) != null) {
_pan.remove(button);
_pan.invalidate();
_frame.repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
答案 0 :(得分:1)
button.setBackground(new Color(rnd.nextInt(colors)));
不。是的,你可以将一个int传递给Color构造函数,但是你不会得到你期望的颜色。但不仅如此,由于Random#nextInt(...)
不接受Color数组作为参数,因此该行不会编译。一个int,是的,比如colors.length
,但不是数组本身。
而是尝试
Random rnd = new Random();
// and elsewhere
int randomIndex = rnd.nextInt(colors.length);
button.setBackground(colors[randomIndex]);
或者,如果您坚持压缩代码,
button.setBackground(colors[rnd.nextInt(colors.length)]);
注意,由于颜色似乎是常量,因此应将其重新命名为COLORS,并因此声明:
Color[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
因为根据Java命名约定,常量应该是全大写的。
答案 1 :(得分:1)
所以,你需要打破你的要求......
首先,您需要为每个角色构建按钮......
Character[] buttonsToAdd = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
Map<Character, JButton> buttons = new HashMap<Character, JButton>();
for (Character c : buttonsToAdd) {
buttons.put(c, new JButton(c.toString()));
}
然后你需要随机播放颜色和字符
Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
List<Color> shuffledColors = Arrays.asList(colors);
List<Character> shuffledCharacters = Arrays.asList(buttonsToAdd);
Collections.shuffle(shuffledColors);
Collections.shuffle(shuffledCharacters);
然后你需要用按钮映射颜色,但你需要character
作为获取按钮的键......
Map<Color, JButton> col = new HashMap<Color, JButton>();
for (int index = 0; index < shuffledColors.size(); index++) {
Color color = shuffledColors.get(index);
Character c = shuffledCharacters.get(index);
JButton btn = buttons.get(c);
col.put(color, btn);
}
<强>更新强>
因此,根据您更新的要求,您当前的方法将无效,因为您需要所有按钮都有一种可用的颜色,但您的Map<Color, JButton>
只允许一个按钮与单色。
相反,你可能需要做更像......的事情。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Game {
public static void main(String[] args) {
new Game();
}
public Game() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<String> characters = new ArrayList<>(Arrays.asList(new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}));
private List<JButton> buttons;
private List<Color> colors = new ArrayList<>(Arrays.asList(new Color[]{Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW}));
private Map<String, JButton> characterToButton = new HashMap<>(25);
private Map<Color, List<JButton>> colorToButtons = new HashMap<>(25);
public TestPane() {
setLayout(new GridLayout(4, 4));
List<Integer> indicies = new ArrayList<>(4 * 4);
buttons = new ArrayList<>(25);
for (int index = 0; index < 4 * 4; index++) {
Collections.shuffle(colors);
Color color = colors.get(0);
indicies.add(index);
JButton btn = new JButton();
buttons.add(btn);
btn.setBackground(color);
add(btn);
List<JButton> btns = colorToButtons.get(color);
if (btns == null) {
btns = new ArrayList<>(25);
colorToButtons.put(color, btns);
}
btns.add(btn);
}
Collections.shuffle(characters);
Collections.shuffle(indicies);
List<Color> usedColor = new ArrayList<>(4);
for (int index = 0; index < 4; index++) {
String value = characters.get(index);
JButton btn = null;
do {
int buttonIndex = indicies.remove(0);
btn = buttons.get(buttonIndex);
} while (usedColor.contains(btn.getBackground()));
usedColor.add(btn.getBackground());
btn.setText(value);
characterToButton.put(value, btn);
addKeyBinding(btn, value);
}
}
protected void addKeyBinding(JComponent comp, String value) {
InputMap inputMap = comp.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = comp.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("typed " + value.toLowerCase()), value);
actionMap.put(value, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = characterToButton.get(value);
Color color = btn.getBackground();
for (JButton others : colorToButtons.get(color)) {
others.setEnabled(false);
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}