我正在尝试使用Swing构建一个非常简单的连接4游戏 主游戏按预期工作,但当我将其用作更大GUI中的组件时,我无法再与其进行交互。
这是主要的GUI代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainLayout extends JFrame {
public MainLayout(){
// TOP PANEL, series of button using FlowLayout
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 10));
topPanel.setBackground(Color.BLUE);
JButton newGameBtn = new JButton("New Game");
topPanel.add(newGameBtn);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(5,5));
this.getContentPane().setBackground(Color.cyan);
this.setTitle("Connect 4");
JPanel grid = new JPanel(new GridLayout(6,7,3,3));
grid.setBackground(Color.blue);
board comp = new board(new Dimension(7,6), new Dimension(60, 60));
comp.setFocusable(true);
comp.setEnabled(true);
this.getContentPane().add(topPanel, BorderLayout.NORTH);
this.getContentPane().add(comp, BorderLayout.CENTER);
this.setSize(420,430);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MainLayout mainWindow = new MainLayout();
mainWindow.setVisible(true);
}
}
电路板组件使用键盘输入来移动方块。