2013-04-26 43 views
0

因此,我正在製作一個棋盤,每當我嘗試放下按鈕時,它們都會重新定向,並且會在右下方變得越來越雜亂。右上角的第一個按鈕(squareB [0] [0])看起來不錯,但所有其他JButton都混亂了。在JPanels中格式化JButton

所以我的問題是我該如何解決這個問題或允許我將按鈕放置在某個座標上?

import java.awt.*; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import java.io.*; 

public class Test implements ActionListener 
{ 
private JFrame frame = new JFrame(); 
private JPanel[][] square = new JPanel[8][8]; 
private JButton[][] button = new JButton[8][8]; 
public static void main(String[] args) 
{ 
    new Test(); 
} 
Test() 
{ 
    frame.setLayout(new GridLayout(8,8)); 
    for(int x=0; x<8; x++) 
    { 
     for(int y=0; y<8; y++) 
     { 
      square[x][y] = new JPanel(); 
      frame.add(square[x][y]); 
      square[x][y].setSize(100,100); 
      square[x][y].setLayout(new GridLayout(1,1)); 
      if(y%2==0) 
       if(x%2==0) 
        square[x][y].setBackground(Color.white); 
       else 
        square[x][y].setBackground(Color.black); 
      if(y%2!=0) 
       if(x%2==0) 
        square[x][y].setBackground(Color.black); 
       else 
        square[x][y].setBackground(Color.white); 

      button[x][y] = new JButton(); 
      button[x][y] = new TestMethods(x,y); 
      square[x][y].add(button[x][y]); 
      button[x][y].setOpaque(false); 
      button[x][y].setContentAreaFilled(true); 
      button[x][y].setBorderPainted(true); 
      button[x][y].addActionListener(this); 
     } 
    } 
    frame.setSize(800,800); 
    frame.setVisible(true); 
} 
public void actionPerformed(ActionEvent e) 
{ 

} 
} 
class TestMethods extends JButton 
{ 
private int x, y; 
public TestMethods(int x, int y) 
{ 
    this.x = x; 
    this.y = y; 
} 
public int getX() {return x;} 

public int getY() {return y;} 
} 

回答

1

我覺得你的問題的一部分是與您TestMethods類的getXgetY方法。由JButton使用這些被覆蓋的方法來確定它的位置在它的父,你應該避免重寫他們

class TestMethods extends JButton 
{ 
    private int x, y; 
    public TestMethods(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
    } 
    public int getX() {return x;} 

    public int getY() {return y;} 
} 

我會的,相反,是更好地與你的目的,使用方法名。

class TestMethods extends JButton 
{ 
    private int gridX, gridY; 
    public TestMethods(int x, int y) 
    { 
     this.gridX = x; 
     this.gridY = y; 
    } 
    public int getGridX() {return gridX ;} 

    public int getGridY() {return gridY ;} 
} 
+0

非常感謝! – user2317760 2013-04-26 23:36:37

0

嗯,我找不到你的代碼壞了,所以我寫了一個方法。我知道這是不是一個答案,但可能是有用的:

public static void main(String[] args) { 
    final JFrame frame = new JFrame(); 
    frame.setPreferredSize(new Dimension(800, 800)); 
    frame.setLayout(new GridLayout(8, 8)); 

    boolean isWhite = true; 
    for (int i = 0; i < 8; i++) { 
     for (int j = 0; j < 8; j++) { 
      final JButton button = new JButton(); 
      if (isWhite) { 
       button.setBackground(Color.white); 
      } else { 
       button.setBackground(Color.black); 
      } 
      isWhite = !isWhite; 
      frame.add(button); 
     } 
     isWhite = !isWhite; 
    } 
    frame.pack(); 
    frame.setVisible(true); 
} 
0

請看由MadProgrammer給出了答案。另外我修改了你的例子來創建棋盤。請看看創建棋盤模式的修改方式。

Test() 
{ 
    frame.setLayout(new GridLayout(8,8)); 
    for(int x=0; x<8; x++) 
    { 
    for(int y=0; y<8; y++) 
    { 
     square[x][y] = new JPanel(); 
     frame.add(square[x][y]); 
     square[x][y].setSize(100,100); 
     square[x][y].setLayout(new GridLayout(1,1)); 



     button[x][y] = new TestMethods(x,y); 
     button[x][y].setOpaque(true); 
     button[x][y].setContentAreaFilled(true); 
     button[x][y].setBorderPainted(true); 
     button[x][y].addActionListener(this); 

     if((y + x)%2==0) 
     { 
      button[x][y].setBackground(Color.WHITE); 
     } 
     else 
     { 
      button[x][y].setBackground(Color.BLACK); 
     } 

     square[x][y].add(button[x][y]); 
    } 
    } 
    frame.setSize(800,800); 
    frame.setVisible(true); 
}