gpt4 book ai didi

java - 尽管使用了 validate(),但 GUI 没有更新

转载 作者:行者123 更新时间:2023-11-30 07:22:28 25 4
gpt4 key购买 nike

这是我正在制作的 Connect Four 程序。到目前为止,我能够将跳棋添加到空的(合格的)插槽中,每次都交替使用红色和黑色。我的代码可能效率很低,因为这是我的第一个 swing 程序。但是,我唯一的主要问题是在单击空插槽后我无法真正更新 GUI。我尝试了 validate(),但它似乎没有做任何事情。可以在下面找到程序中使用的图像的链接。非常感谢您的帮助!

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.*;

public class GameFrame extends JFrame {

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new GameFrame();
frame.setContentPane(new JLabel(new ImageIcon(getClass()
.getResource("675 x 588 Connect Four.png"))));
frame.addSlots();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/

static GameFrame frame;
private static Icon slotForButton;
public static JButton slotButton;
static private JButton[] slot = new JButton[42];
static private String[] slotColor = new String[42];
static boolean turn = true;
static boolean legitClick;
static String slotClicked;
static int slotNum;
static Container gamePane;

public GameFrame() {
setBounds(100, 100, 685, 622);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
super.setTitle("Connect Four by Joel Christophel");
}

void addSlots() {
ButtonHandler bh = new ButtonHandler();
gamePane = getContentPane();
gamePane.setLayout(new GridLayout(6, 6));
int counter = 0;

for (JButton e : slot) {
slot[counter] = makeSlot("white");
slot[counter].setBorderPainted(false);
slot[counter].setContentAreaFilled(false);
slot[counter].setFocusPainted(false);
slot[counter].setActionCommand(counter + "");
slotColor[counter] = "white";

slot[counter].addActionListener(bh);
add(slot[counter]);

counter++;
}
}

static JButton makeSlot(String color) {

if (color.equals("white")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("Space.png"));
}

else if (color.equals("red")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("Red Checker.png"));
}

else if (color.equals("black")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("Black Checker.png"));
}

slotButton = new JButton(slotForButton);

return slotButton;
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
slotClicked = e.getActionCommand();

GameFrame.legitClick(slotClicked);
}

}

private static void changeTurn() {
turn = !turn; // true is red's turn; false is black's
}

private static void legitClick(String slotClicked1) {
legitClick = false;

slotClicked = slotClicked1;
Scanner numScan = new Scanner(slotClicked);
slotNum = numScan.nextInt();

try {
if (!slotColor[slotNum + 7].equals("white")&&slotColor[slotNum].equals("white")) {
legitClick = true;
}
}

catch (ArrayIndexOutOfBoundsException t) {

if (slotColor[slotNum].equals("white")) {
legitClick = true;
}
}

if (legitClick == true) {
if (turn == true) {
slot[slotNum] = makeSlot("red");
slotColor[slotNum] = "red";
System.out.println("Put " + slotColor[slotNum] + " checker in slot number " + slotNum + ".");
}

else if (turn == false) {
slot[slotNum] = makeSlot("black");
slotColor[slotNum] = "black";
System.out.println("Put " + slotColor[slotNum] + " checker in slot number " + slotNum + ".");
}

gamePane.validate();
GameFrame.changeTurn();
}
System.out.println(turn);
}

}

http://i.stack.imgur.com/8cNB3.png 675 x 588 连接四.png

http://i.stack.imgur.com/6oX7A.png黑色方格.png

http://i.stack.imgur.com/cdF7u.png红色方格.png

http://i.stack.imgur.com/JNT61.png空间.png

最佳答案

有两件事,在我看来你的代码做错了。

  • 首先检查你在你的内部指定的条件legitClick()方法,无论什么时候点击,总是返回false(legitClick)。因此, View 不会出现任何变化。
  • 看来您在 makeSlot() 中创建了一个新的 JButton,这并不是您想要的,您只需更改 Icon 用于相应的 JButton 而不是创建新的。

我在 makeSlot() 方法和 legitClick() 方法中对代码进行了一些修改,尽管您必须自己更改逻辑才能使其真正起作用如你所愿,因为我不知道西洋跳棋是如何工作的:(

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.*;

public class GameFrame extends JFrame {

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new GameFrame();
frame.setContentPane(new JLabel(new ImageIcon(getClass()
.getResource("/image/675 x 588 Connect Four.png"))));
frame.addSlots();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/

static GameFrame frame;
private static Icon slotForButton;
public static JButton slotButton;
static private JButton[] slot = new JButton[42];
static private String[] slotColor = new String[42];
static boolean turn = true;
static boolean legitClick;
static String slotClicked;
static int slotNum;
static Container gamePane;

public GameFrame() {
setBounds(100, 100, 685, 622);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
super.setTitle("Connect Four by Joel Christophel");
}

void addSlots() {
ButtonHandler bh = new ButtonHandler();
gamePane = getContentPane();
gamePane.setLayout(new GridLayout(6, 6));

for (int counter = 0; counter < slot.length; counter++) {
// Added by me.
slot[counter] = new JButton();
slot[counter].setIcon(makeSlot("white", counter));
slot[counter].setBorderPainted(false);
slot[counter].setContentAreaFilled(false);
slot[counter].setFocusPainted(false);
slot[counter].setActionCommand(counter + "");
slotColor[counter] = "white";

slot[counter].addActionListener(bh);
add(slot[counter]);

//counter++;
}
}

static Icon makeSlot(String color, int index) {

if (color.equals("white")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("/image/Space.png"));
}

else if (color.equals("red")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("/image/Red Checker.png"));
}

else if (color.equals("black")) {
slotForButton = new ImageIcon(
GameFrame.class.getResource("/image/Black Checker.png"));
}

//slot[index].setIcon(slotForButton);

return slotForButton;
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
slotClicked = e.getActionCommand();

GameFrame.legitClick(slotClicked);
}

}

private static void changeTurn() {
turn = !turn; // true is red's turn; false is black's
}

private static void legitClick(String slotClicked1) {
legitClick = false;

slotClicked = slotClicked1;
Scanner numScan = new Scanner(slotClicked);
slotNum = numScan.nextInt();

try {
if (slotColor[slotNum + 7].equals("white")&&slotColor[slotNum].equals("white")) {
legitClick = true;
}
}

catch (ArrayIndexOutOfBoundsException t) {

if (slotColor[slotNum].equals("white")) {
legitClick = true;
}
}

/*
* This variable is used to tell
* makeSlot that the JButton at
* this index is pressed. So
* simply change the Icon for this
* JButton, instead of adding a new
* JButton.
*/
int index = Integer.parseInt(slotClicked1);

if (legitClick == true) {
if (turn == true) {
slot[index].setIcon(makeSlot("red", index));
slotColor[slotNum] = "red";
System.out.println("Put " + slotColor[slotNum] + " checker in slot number " + slotNum + ".");
}

else if (turn == false) {
slot[index].setIcon(makeSlot("black", index));
slotColor[slotNum] = "black";
System.out.println("Put " + slotColor[slotNum] + " checker in slot number " + slotNum + ".");
}

//gamePane.validate();
GameFrame.changeTurn();
}
System.out.println(turn);
}

}

关于java - 尽管使用了 validate(),但 GUI 没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com