[英]How can I compare two buttons java
我正在嘗試編寫一個 memory 游戲,我選擇了具有特定顏色的每個按鈕。 我想比較這兩個按鈕。 當用戶單擊下一步按鈕時,我想開始比較。 我想出了比較顏色的背景。 我試圖將背景顏色保存在一個變量中並測試它們是否相同但是即使兩個 colors 相同也不會進入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");
}
}
}
}
}
}
JButton 和許多其他 UI 元素一樣具有與其屏幕呈現相關的屬性。 除非您真的只需要這些屬性,否則將這些與 UI 相關的屬性解釋為與應用程序相關是一種濫用行為。
創建您自己的數據 model。有了它,您就會知道游戲板上有多少個字段以及將哪些值放在哪個字段(顏色、玩家、建築物等)上。 為了干凈起見,請確保 model 不包含對 UI 元素的任何引用。
在下一步中創建該板的視圖。 它會將 map 字段發送到某個 UI 組件,並確保呈現一個。 在您的情況下,每個按鈕都會附加一個 ActionListener,它知道單擊按鈕時意味着哪個字段。
由於您可能只需要在單擊第二個按鈕時進行比較,因此您需要記住已經按下了多少個按鈕以及哪些按鈕。 所有這些都會將 go 轉換為您的數據 model。
所以當一個按鈕被按下時,檢查一個選擇是否已經存在。 如果不是,則應設置此選擇。 (它類似於第一個按鈕)。 如果已經有選擇,則將所選字段與第二個按鈕的字段進行比較,然后重置選擇。
您的邏輯——在方法actionPerformed
中——是有缺陷的。 每次調用方法actionPerformed
時,即每次單擊JButton
時,您都將Click1
和Click2
重置為黑色。
您顯然希望Click1
保持在變量Click1_turn
為true時分配給它的顏色。
因此,您應該使Click1
和Click2
都成為 class 成員變量,以便它們在對actionPerformed
的單獨調用之間保持分配的值。
如果您通過調試器運行您的代碼,您會發現這一點。 請注意,每個程序員都需要學習如何調試代碼,包括其他人編寫的代碼。
使Click1
和Click2
都成為 class 成員變量是修復程序的最簡單方法。 請參考下面的代碼。 請注意,我更改了標識符以符合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());
這是沒有必要的。 默認布局是合適的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.