【问题标题】:How do I create individual button names in an array?如何在数组中创建单个按钮名称?
【发布时间】:2011-01-03 16:49:42
【问题描述】:

我正在尝试制作一款战舰游戏,我可以控制棋盘。如何制作一个允许每个按钮返回位置的数组。用于存放船只和命中物等。我想制作每个单独的按钮,但我不知道该怎么做。

【问题讨论】:

    标签: java arrays swing jbutton


    【解决方案1】:

    你可以扩展按钮类:

    public class BattleShipButton extends JButton {
    
        private Coordinate coords;
        public BattleShipButton(Coordinates coords) {
            this.coords = coords;  
            setPreferredSize(new Dimension(20, 20));
        }
        public Coordinates getCoordinates() {
            return coords;
        }
    }
    

    然后,您可以在循环中实例化按钮并传入正确的坐标。

    BattleShipButton[][] buttons = new BattleShipButton[boardWidth][boardHeight];
    for(int i = (int)'a'; i < boardWidth; i++){
        for(int j = 0; j < boardHeight; j++) {
            buttons[i][j] = new BattleShipButton(new Coordinate((char)i, j));
        }
    }
    

    然后每个按钮将具有您可以使用getCoordiantes() 获得的正确坐标。

    【讨论】:

    • JButton 可能有一个保持屏幕位置的属性。
    • @Alan,正确,您可以使用getLocation()getLocationOnScreen(),但这只会为您提供像素坐标。这将为您提供合乎逻辑的战舰坐标。
    【解决方案2】:

    如果你想知道他们的位置,他们的索引,只要在一个矩阵中做它们就足够了。

    如果你想更具体,你可以创建自己的按钮,然后有一个返回的函数,那个位置

    【讨论】:

      【解决方案3】:

      创建一个扩展 AbstractAction 的新类。在创建按钮时为其提供坐标属性并创建新实例并传递给 JButton 的构造函数。

      在这样做时,您将覆盖 Action 的 actionPerformed 方法,以允许您单击按钮事件来利用这些坐标,这些坐标都很好地封装在同一个类中。

      【讨论】:

        【解决方案4】:

        如果这对您有帮助,请不要这样做,但总比没有好 我用了表单申请去看看

        你可以像这样创建一个数组

        Button[] Barray = new Button[32,32];
        
        than you will have to fill this array 
        
        for (int i = 0; i <= a - 1; i++)
        {
        
        for (int j = 0; j <= b - 1; j++)
                {
            Barray (i, j) = new Button(); // here you create a dynamic button
        
                Barray (i, j).Location = new System.Drawing.Point(x, y); // here you locate the button in to your box or in a similar container
        
                Barray (i, j).Size = new System.Drawing.Point(23, 23); // in this line you resize your button 
        
                Barray (i, j).Name = i + j; // in this lie you rename your button so that you will be able to reach your button and know what is its location
                Barray (i, j).Click += Button_Click; // in this line you will add the button_click event to your dynamic buttons
                Form1.box.Controls.Add(dizi(i, j)); // and this line adds your button to your container 
                x += 23; // in this line i am increasing the x location so i the buttons will not be placed at  the same x location
            }
            x = 0; // this line i ll make x zero that means i m finis previous line and i ll start to place buttons on another line 
            y += 23; // this line i m increasing the y location to move the next line
        }
        

        在这些代码之后,我需要创建一个事件来捕获按钮点击事件:)

        private void button_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {   
        //here you can use e eventargs reach your buttons name so you can do anything you want :)
        }
        

        希望对你有帮助

        【讨论】:

        • 你在哪里看到 C# 的问题?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 2015-09-18
        • 2015-06-23
        • 1970-01-01
        • 2011-03-14
        • 2018-09-13
        相关资源
        最近更新 更多