0

当我为我的 s 数组设置 X 和 Y 值时JButton,我得到的只是乘以 93 的正确值。我可以通过将该值除以 93 来解决问题,但我宁愿首先找出错误的位置。

我在代码中有两个类,一个用于实际程序,一个用于按钮对象以及坐标。

这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.io.*;
public class ConnectFour implements ActionListener
{
     JFrame frame = new JFrame();
     Button [][] buttons = new Button[6][7];
     public ConnectFour()
     {
         frame.setSize(700,600);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setLayout(new GridLayout(6,7));
         frame.setLocationRelativeTo(null);
         frame.setTitle("Connect Four");
         for(int filler = 0; filler <= 5; filler++)
         {   
              for(int filler2 = 0; filler2 <= 6; filler2++) 
              {
                   buttons[filler][filler2] = new Button();
                   buttons[filler][filler2].setX(filler2);
                   buttons[filler][filler2].setY(filler);
                   //System.out.println(buttons[filler][filler2].getX());
                   //System.out.print(buttons[filler][filler2].getY());
                   frame.add(buttons[filler][filler2].button);
                   buttons[filler][filler2].button.addActionListener(this);
              }
         }
         frame.setVisible(true);
     }
     public void actionPerformed(ActionEvent a)
     {
         JButton pressedButton = (JButton)a.getSource();
         System.out.print(pressedButton.getY() / 93);
         System.out.print(pressedButton.getX() / 93);
     }
     public static void main(String args[])
     {
          ConnectFour gameplay = new ConnectFour();
     }
}

这是Button课程:

import javax.swing.JButton;
public class Button
{
    JButton button;
    private int x = 0;
    private int y = 0;
    public Button()
    {
        button = new JButton();
    }
    public int getX() {return x;}
    public int getY() {return y;}
    public void setX(int xIndex) 
    {
        x = xIndex;
    }
    public void setY(int yIndex) 
    {
        y = yIndex;
    }
} 
4

3 回答 3

1

你正在混合你的两个Button课程。

在这一行中,您将一个 actionListener 添加到Button.button

buttons[filler][filler2].button.addActionListener(this);

因为JButton也有方法getXand getY,你可以调用它们。当你这样做时:

pressedButton.getX()

你得到的x位置,而JButton不是你的Button

我认为解决此问题的最简单方法是使您的按钮扩展JButton并重命名为xand ,例如:yrowcolumn

public class Button extends JButton {
    private int row = 0;
    private int column = 0;

    public Button(int row, int column) {
        super();

        this.row = row;
        this.column = column;    
    }
    public int getRow() {return row;}
    public int getColumn() {return column;}
} 

您可以将按钮创建为

for(int filler = 0; filler <= 5; filler++) {   
    for(int filler2 = 0; filler2 <= 6; filler2++) {
        buttons[filler][filler2] = new Button(filler2, filler);
        frame.add(buttons[filler][filler2]);
        buttons[filler][filler2].addActionListener(this);
    }
}

ActionListener并在as中使用它们

public void actionPerformed(ActionEvent a) {
    Button pressedButton = (Button)a.getSource();
    System.out.print(pressedButton.getColumn());
    System.out.print(pressedButton.getRow());
}
于 2013-05-14T12:56:45.557 回答
1

我不知道如何使用您的作文示例来做到这一点,但这一个可以按照您的意愿工作。

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.io.*;

public class ConnectFour implements ActionListener
{
    JFrame frame = new JFrame();
    CustomButton [][] buttons = new CustomButton[6][7];

    public ConnectFour()
    {
        frame.setSize(700,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(6,7));
        frame.setLocationRelativeTo(null);
        frame.setTitle("Connect Four");
        for(int filler = 0; filler <= 5; filler++)
        {
            for(int filler2 = 0; filler2 <= 6; filler2++)
            {
                buttons[filler][filler2] = new CustomButton(filler,filler2);
                frame.add(buttons[filler][filler2]);
                buttons[filler][filler2].addActionListener(this);
            }
        }
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent a)
    {
        CustomButton pressedButton = (CustomButton)a.getSource();
        System.out.println(pressedButton.getRow() + "/" + pressedButton.getCol());
    }

    public static void main(String args[])
    {
        ConnectFour gameplay = new ConnectFour();
    }
}

class CustomButton extends JButton
{
    private int row = 0;
    private int col = 0;

    public CustomButton(int row, int col)
    {
        this.row = row;
        this.col = col;
    }

    public int getRow() {return row;}
    public int getCol() {return col;}

    public void setRow(int row)
    {
        this.row = row;
    }

    public void setCol(int col)
    {
        this.col = col;
    }
}
于 2013-05-14T13:06:05.383 回答
0

我认为 x 和 y 是在超类中定义的,请尝试将变量名称更改为其他名称。例如

private int myX = 0;
private int myY = 0;
于 2013-05-14T12:53:24.783 回答