我试图从使用getter和setter的方法中获取值,但是无法让它返回除0之外的任何内容。这是我当前的代码:
int goodRow;
int boardStatus[][] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
};
public void placePiece(int column) {
boolean goodInput = false;
while(!goodInput) {
for(int x = 5; x >= 0; x--) {
if(boardStatus[x][column] == 0) {
setRow(x);
boardStatus[x][column] = 1;
goodInput = true;
break;
}else if(boardStatus[0][x] == 1) {
goodInput = true;
break;
}
}
}
}
public int getRow() {
return goodRow;
}
public void setRow(int row) {
goodRow = row;
}
我的目标是能够调用getRow()并检索方法中接受的x值。在我的placePiece()
方法中,我使用setRow()
方法获取x值,但到目前为止我的策略还没有完成。如何从我的方法中获取x值,以便我可以在程序的其他地方使用它?
- - - - - - - - 编辑
这是我的完整代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JPanel implements MouseListener {
moveMaker mover = new moveMaker();
clickDetector click = new clickDetector();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
for(int y = 0; y < 6; y++) {
for(int x = 0; x < 7; x++) {
g.fillOval(x*70 + 10, y*70 + 10, 50, 50);
}
}
g.setColor(Color.BLACK);
g.fillRect(0, 430, 500, 50);
g.setColor(Color.white);
g.drawString("CONNECT FOUR", 250, 450);
g.setColor(Color.LIGHT_GRAY);
click.paintPiece(g);
}
public void setUpGUI() {
JFrame mainFrame = new JFrame();
clickDetector click = new clickDetector();
mainFrame.addMouseListener(click);
mainFrame.addMouseListener(this);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(500, 500);
mainFrame.getContentPane().add(this);
mainFrame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public static void main(String args[]) {
GUI gui = new GUI();
gui.setUpGUI();
}
}
-
public class moveMaker {
int goodRow;
int boardStatus[][] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
};
public void placePiece(int column) {
boolean goodInput = false;
while(!goodInput) {
for(int x = 5; x >= 0; x--) {
if(boardStatus[x][column] == 0) {
setRow(x);
boardStatus[x][column] = 1;
goodInput = true;
break;
}else if(boardStatus[0][x] == 1) {
setRow(x);
goodInput = true;
break;
}else{
System.out.println(x + " is taken.");
}
}
}
}
public int getRow() {
return goodRow;
}
public void setRow(int row) {
goodRow = row;
}
}
-
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class clickDetector implements MouseListener {
moveMaker mover = new moveMaker();
int mouseX = 0;
@Override
public void mouseClicked(MouseEvent e) {
int xLocation = (e.getX() + 10)/70;
mover.placePiece(xLocation);
setMouseX(xLocation);
System.out.println("Place piece at " + mouseX + ", " + mover.getRow() + ".");
}
public void paintPiece(Graphics g) {
int x = (getMouseX()+10)/70;
int y = (mover.getRow()+10)/70;
g.fillOval(x, y, 50, 50);
System.out.println(x + ", " + y);
}
public int getMouseX() {
return mouseX;
}
public void setMouseX(int x) {
mouseX = x;
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
答案 0 :(得分:0)
您尚未在此块中调用setRow(x)
:
else if(boardStatus[0][x] == 1) {
goodInput = true;
break;
} else { //missing
...
}
您还缺少else
阻止。
编辑:
尝试调试方法setRow()
并观察参数值。
答案 1 :(得分:0)
我将您的代码放在一个名为Trial的类中,并添加了这个main():
public static void main(String[] args){
Trial t = new Trial();
for (int k = 0; k < 5; k++){
t.placePiece(k);
System.out.println(k + ", " + t.getRow());
}
}
输出结果为:
0, 5
1, 5
2, 5
3, 5
4, 5
这是预期的输出:如果placePiece(n)
的值为0(它总是如此),方法setRow(n)
仅调用boardStatus[5][n]
并结束循环;
当你调用getRow()
时,该值将为5,即当你调用setter时循环所在的行!
看看你的while循环的逻辑,它归结为
while(true){
setRow(5);
break;
}
或更简单
public void placePiece(int column){
setRow(5);
}
答案 2 :(得分:0)
从clickDetector类中删除mouseListener,并创建一个新方法;
public void setUpVariables(int x) {
int xLocation = (x + 10)/70;
mover.placePiece(xLocation);
setMouseX(xLocation);
System.out.println("Place piece at " + mouseX + ", " + mover.getRow() + ".");
}
现在,在您的GUI类中,在mouseClicked方法中,您可以添加
click.setUpVariables(e.getX());
为了正确绘画,需要稍微调整paintPiece方法
public void paintPiece(Graphics g) {
int x = (getMouseX())*70 + 10;
int y = mover.getRow()*70 + 10;
g.fillOval(x, y, 50, 50);
}