JButton显示悬停Java Swing

JButton显示悬停Java Swing

问题描述:

我目前正在尝试制作一个基本的Java国际象棋游戏,其中ChessBoard初始化并填充ChessSquares。每次我将窗口设置为可见时,每个JButton都不会显示,除非悬停。我该如何阻止?JButton显示悬停Java Swing

器ChessBoard

public class ChessBoard extends JFrame implements ActionListener { 

    private JPanel p = new JPanel(); 
    private ChessSquare[][] board = new ChessSquare[8][8]; 

    public ChessBoard(){ 

     //Specify frame window. 
     setSize(640,640); 
     setTitle("Chess"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add panel to frame window. 
     p.setLayout(new GridLayout(8,8)); 
     setContentPane(p); 

     //Populate Chess Board with squares. 
     for(int y=0; y<8; y++){ 
      for(int x=0; x<8; x++){ 
       board[x][y] = new ChessSquare(x,y); 
       board[x][y].addActionListener(this); 
       p.add(board[x][y]); 
      } 
     } 

     //Show window. 
     setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
     ChessSquare s = (ChessSquare) e.getSource(); 
     //... 
    } 
} 

ChessSquare

import javax.swing.*; 

public class ChessSquare extends JButton{ 

    private int x, y; 

    public ChessSquare(int x, int y){ 
     this.x = x; 
     this.y = y; 
    } 


    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 
} 

On Render | After Mouse Over

+1

1)为了更好地帮助越早,张贴[MCVE]或[简要,独立的,正确的示例](http://www.sscce.org/)。 2)另请参见[制作强大的可调整大小的Swing Chess GUI](http://*.com/q/21142686/418556)。 3)在'getX()'/'getY()'方法中添加'@ Override'符号来**不**得到编译错误信息。这反过来表明a)它们已经存在b)混淆现有版本可能是不明智的。最终,我很少看到重写组件类的一个很好的理由,在这种情况下,它是一个(可能的 - 给出结果)坏主意。 –

添加@Override符号到getX()/getY()方法得到一个编译器错误信息。这反过来表示:

  • 这些方法已经存在。
  • 混淆现有的实现可能是不明智的。

最终,我很少看到重写组件类的一个很好的理由,在这种情况下,它是一个(可证明 - 给出结果)坏主意。

此MCVE支持在ChessSquare组件中将问题替换为ChessBoard类中的标准按钮。它工作得很好,尽管代码中还有一些问题(例如,没有调用pack() &来猜测所需的GUI大小,在这种情况下,导致棋盘是不是方块) 。

import java.awt.GridLayout; 
import java.awt.event.*; 
import javax.swing.*; 

public class ChessBoard extends JFrame implements ActionListener { 

    private JPanel p = new JPanel(); 
    private JButton[][] board = new JButton[8][8]; 

    public ChessBoard() { 

     //Specify frame window. 
     setSize(640, 640); 
     setTitle("Chess"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add panel to frame window. 
     p.setLayout(new GridLayout(8, 8)); 
     setContentPane(p); 

     //Populate Chess Board with squares. 
     for (int y = 0; y < 8; y++) { 
      for (int x = 0; x < 8; x++) { 
       board[x][y] = new JButton(x + "," + y); 
       board[x][y].addActionListener(this); 
       p.add(board[x][y]); 
      } 
     } 

     //Show window. 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ChessBoard(); 
      } 
     }); 
    } 

    public void actionPerformed(ActionEvent e) { 
     JButton s = (JButton) e.getSource(); 
     //... 
    } 
} 
  1. 不是正方形”,而不是在Making a robust, resizable Swing Chess GUI看到的棋盘。

+0

啊,我明白这个重写,我的错误!甚至没有想到这一点! 谢谢你的帮助!我只需更换按钮! –