【发布时间】:2015-11-09 00:42:43
【问题描述】:
我正在尝试连接四个 gui。
我有一个 Gui 类,它创建一个棋盘(一个 array[][] ints)和一个方法 move - 它接受 player(int) 和 column(int) - 以及一个检查是否有赢家的方法。
我使用 gridLayout 创建了一个 gui,我有一排按钮和 7 个 JLabel 数组(它们是空槽的 ImageIcon)如果玩家选择该列,我现在需要访问特定数组的最底部 JLabel。
我在循环中创建了按钮:
- A) 我如何访问每个按钮 - 它们没有唯一的名称。
- B) 我如何访问每个 JLabels - 它们也是在循环中创建的
- C) 我应该在哪里实例化我的 Board 类?
- D) 当我在 gui 中添加每个新对象时 this.add(JLabel)--
如何将所有内容放入新面板中,以便在网格顶部有一个标题栏?
我的 gui 代码
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class ConnectFourGui extends JFrame {
private JLabel title;
private JButton button;
ImageIcon[] emptySlot;
ImageIcon arrow;
public ConnectFourGui() {
this.setTitle("Connect Four");
this.setSize(800, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel panel = new JPanel();
//Container container = this.getContentPane();
this.setLayout(new GridLayout(7, 7));
this.title = new JLabel("Connect Four");
title.setForeground(Color.RED);
title.setFont(new Font("Sans-Serif", Font.BOLD, 50));
title.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < 7; i++){
arrow = new ImageIcon("arrowButton.png");
this.button = new JButton(arrow);
this.add(button);
}
for (int i = 0; i < 7; i++){
emptySlot = new ImageIcon[6];
for (int j = 0; j<6;j++){
emptySlot[j] = new ImageIcon("emptySlot.png");
this.add(new JLabel(emptySlot[j]));
}
}
【问题讨论】:
标签: java user-interface