2013-05-14 33 views
0

當我爲我的JButton s數組設置X和Y值時,我只能得到正確的值乘以93.我可以通過將值除以93來解決問題,但我寧願找出錯誤在哪裏擺在首位。Java不斷給我我的座標乘以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; 
    } 
} 
+1

我猜它歸結爲按鈕的大小爲93像素.. – 2013-05-14 12:53:49

回答

1

你在混合你的兩個Button類。

在這一行,你要添加一個ActionListener Button.button

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

因爲JButton也有方法getXgetY,你可以給他們打電話。當你這樣做:

pressedButton.getX() 

你得到的不是你ButtonJButtonx位置。

我覺得這是解決這個問題的是使你的按鈕擴展JButton和重命名xyrowcolumn,比如最簡單的方法:

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

public void actionPerformed(ActionEvent a) { 
    Button pressedButton = (Button)a.getSource(); 
    System.out.print(pressedButton.getColumn()); 
    System.out.print(pressedButton.getRow()); 
} 
0

我覺得x和y超類中定義,請嘗試更改變量名稱到別的東西。例如

private int myX = 0; 
private int myY = 0; 
+1

'Button'沒有父,他使用組成,而不是繼承 – 2013-05-14 12:54:01

+0

噢。然後,JButton上的getX與get和set自定義Button類中的設置無關,因此將不會是相同的值。你可能需要一個JButton - > Button的hashmap,這樣你就可以使用事件源來查找模型對象(Button) – 2013-05-14 12:57:43

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; 
    } 
} 
+0

哇,即使我們的變量名也是一樣的:) – 2013-05-14 13:08:01

+1

+1 ..... aaach half_sized without 'get/putClientProperty',原因來自2-3個失眠症周 – mKorbel 2013-05-17 09:32:41

相關問題