我正在嘗試撰寫一個記憶游戲,并且我選擇了具有特定顏色的每個按鈕。我想比較這兩個按鈕。當用戶單擊下一個按鈕時,我想開始比較。我想出了比較顏色的背景。我試圖將背景顏色保存在變數中并測驗它們是否相同,但if即使兩種顏色相同,它也不會進入陳述句。如果您有任何其他想法,請幫助我:)
import java.awt.Color;
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.Arrays;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MemoryGame implements ActionListener {
public static void main(String[] args) {
MemoryGame a = new MemoryGame();
}
ArrayList<Color> colors = new ArrayList<Color>(Arrays.asList(Color.black,
Color.BLUE,
Color.yellow,
Color.GRAY,
Color.cyan,
Color.RED,
Color.PINK,
Color.orange,
Color.orange,
Color.yellow,
Color.black,
Color.BLUE,
Color.PINK,
Color.cyan,
Color.RED,
Color.GRAY));
Random rand = new Random();
JFrame myframe = new JFrame();
JPanel title = new JPanel();
JPanel Button_Panel = new JPanel();
JButton[] buttons = new JButton[16];
JLabel textfield = new JLabel();
boolean Click1_turn = true;
MemoryGame() {
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setSize(1000, 1000);
myframe.getContentPane().setBackground(Color.gray);
myframe.setVisible(true);
myframe.setLayout(new GridLayout()); // to put the buttons in it
Button_Panel.setLayout(new GridLayout(4, 5));
Button_Panel.setBackground(Color.black);
myframe.add(Button_Panel);
for (int i = 0; i < 16; i ) {
buttons[i] = new JButton();
Button_Panel.add(buttons[i]);
buttons[i].setFont(new Font("MV", Font.BOLD, 120));
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
Color Click1 = colors.get(0);
Color Click2 = colors.get(0);
for (int i = 0; i < 16; i ) {
if (e.getSource() == buttons[i]) {
if (Click1_turn) {
buttons[i].setBackground(colors.get(i));
Click1 = buttons[i].getBackground();
System.out.println("1" Click1);
Click1_turn = false;
}
else {
buttons[i].setBackground(colors.get(i));
Click2 = buttons[i].getBackground();
Click1_turn = true;
System.out.println("2" Click2);
if (Click2.equals(Click1)) {
System.out.println("ss");
}
else {
System.out.println("nah");
}
}
}
}
}
}
uj5u.com熱心網友回復:
你的邏輯——在方法actionPerformed上——是有缺陷的。每次呼叫方法時,即每次單擊 a時,您都在重置Click1并Click2變為黑色。actionPerformedJButton
您顯然希望Click1保留變數Click1_turn為true時分配的顏色。
因此,您應該同時創建Click1和Click2類成員變數,以便它們在對actionPerformed.
如果您通過除錯器運行代碼,您會發現這一點。請注意,每個程式員都需要學習如何除錯代碼,包括其他人撰寫的代碼。
同時創建Click1和Click2類成員變數是修復程式的最簡單方法。請參閱下面的代碼。請注意,我更改了識別符號以符合Java 命名約定。
(代碼后有更多注釋。)
import java.awt.Color;
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.Arrays;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MemoryGame implements ActionListener {
private Color click1;
private Color click2;
public static void main(String[] args) {
MemoryGame a = new MemoryGame();
}
ArrayList<Color> colors = new ArrayList<Color>(Arrays.asList(Color.black,
Color.BLUE,
Color.yellow,
Color.GRAY,
Color.cyan,
Color.RED,
Color.PINK,
Color.orange,
Color.orange,
Color.yellow,
Color.black,
Color.BLUE,
Color.PINK,
Color.cyan,
Color.RED,
Color.GRAY));
Random rand = new Random();
JFrame myFrame = new JFrame();
JPanel title = new JPanel();
JPanel buttonPanel = new JPanel();
JButton[] buttons = new JButton[16];
JLabel textfield = new JLabel();
boolean click1Turn = true;
MemoryGame() {
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(1000, 1000);
myFrame.getContentPane().setBackground(Color.gray);
myFrame.setVisible(true);
buttonPanel.setLayout(new GridLayout(0, 4));
buttonPanel.setBackground(Color.black);
myFrame.add(buttonPanel);
for (int i = 0; i < 16; i ) {
buttons[i] = new JButton();
buttonPanel.add(buttons[i]);
buttons[i].setFont(new Font("MV", Font.BOLD, 120));
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
// Color Click1 = colors.get(0);
// Color Click2 = colors.get(0);
for (int i = 0; i < 16; i ) {
if (e.getSource() == buttons[i]) {
if (click1Turn) {
buttons[i].setBackground(colors.get(i));
click1 = buttons[i].getBackground();
System.out.println("1" click1);
click1Turn = false;
}
else {
buttons[i].setBackground(colors.get(i));
click2 = buttons[i].getBackground();
click1Turn = true;
System.out.println("2" click2);
if (click2 == click1) {
System.out.println("ss");
}
else {
System.out.println("nah");
}
}
}
}
}
}
- 由于您所有的顏色都是類中的常量
java.awt.Color,因此您可以使用==它們來比較它們而不是呼叫方法equals。 System.out.println(e.getActionCommand());不列印任何內容,因為沒有一個按鈕具有分配給 的值actionCommand。buttons[i].setFont(new Font("MV", Font.BOLD, 120));由于您沒有為 設定任何文本,因此JButton更改字體無效。Button_Panel.setLayout(new GridLayout(4, 5));通常最好只設定一個維度,即Button_Panel.setLayout(new GridLayout(0, 4));這意味著“網格”將在每行中恰好有四列。請參閱如何使用 GridLayout。myframe.setLayout(new GridLayout());這不是必需的。默認布局是合適的。
uj5u.com熱心網友回復:
JButtons 與許多其他 UI 元素一樣具有與其螢屏渲染相關的屬性。除非您真的只需要這些屬性,否則將這些與 UI 相關的屬性解釋為與應用程式相關是一種濫用。
創建自己的資料模型。這樣您就可以知道游戲板上有多少個欄位以及在哪個欄位上放置哪些值(顏色、玩家、建筑物……)。為了干凈,請確保模型不包含對 UI 元素的任何參考。
在下一步中,創建該板的視圖。它將欄位映射到某個 UI 組件并確保一個被渲染。在您的情況下,每個按鈕都會附加一個 ActionListener,它知道單擊按鈕時的含義。
由于您可能只需要在單擊第二個按鈕時進行比較,因此您需要記住已經按下了多少個按鈕以及哪個按鈕。所有這些都將進入您的資料模型。
所以當一個按鈕被按下時,檢查一個選擇是否已經存在。如果不是,則應設定此選擇。(它類似于第一個按鈕)。如果已經有選擇,請將所選欄位與第二個按鈕的欄位進行比較,然后重置選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511828.html
標籤:爪哇摇摆awt
