【问题标题】:Java Gui GridLayout- accessing objectsJava Gui GridLayout - 访问对象
【发布时间】:2015-11-09 00:42:43
【问题描述】:

我正在尝试连接四个 gui。

我有一个 Gui 类,它创建一个棋盘(一个 array[][] ints)和一个方法 move - 它接受 player(int) 和 column(int) - 以及一个检查是否有赢家的方法。

我使用 gridLayout 创建了一个 gui,我有一排按钮和 7 个 JLabel 数组(它们是空槽的 ImageIcon)如果玩家选择该列,我现在需要访问特定数组的最底部 JLabel。

我在循环中创建了按钮:

  1. A) 我如何访问每个按钮 - 它们没有唯一的名称。
  2. B) 我如何访问每个 JLabels - 它们也是在循环中创建的
  3. C) 我应该在哪里实例化我的 Board 类?
  4. 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


    【解决方案1】:

    尝试像这样为每一列创建单独的数组(参见下面的代码)。还要使您的数组成为 JLabels 数组而不是 ImageIcons。并创建具有唯一名称的每个按钮。

    column1 = new JLabel[6];
        column2 = new JLabel[6];
        column3 = new JLabel[6];
        column4 = new JLabel[6];
        column5 = new JLabel[6];
        column6 = new JLabel[6];
        column7 = new JLabel[6];
    
        for (int j = 0; j < 6; j++) {
            column1[j] = new JLabel(emptySlot);
            column2[j] = new JLabel(emptySlot);
            column3[j] = new JLabel(emptySlot);
            column4[j] = new JLabel(emptySlot);
            column5[j] = new JLabel(emptySlot);
            column6[j] = new JLabel(emptySlot);
            column7[j] = new JLabel(emptySlot);
            this.add(column1[j]);
            this.add(column2[j]);
            this.add(column3[j]);
            this.add(column4[j]);
            this.add(column5[j]);
            this.add(column6[j]);
            this.add(column7[j]);
        }
    

    要根据顶部的按钮访问数组中的元素,请执行以下操作

    buttonName.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent a) {
                column1[3].setIcon(newIcon);
    }
    }
    
    • 您将使用变量而不是我用作示例的数字 3

    在您的构造函数中实例化您的板类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 2013-05-14
      • 2012-10-15
      • 2012-05-16
      相关资源
      最近更新 更多