【问题标题】:How to get the gridlayout cell position of a JButton in Java? [duplicate]如何在 Java 中获取 JButton 的网格布局单元格位置? [复制]
【发布时间】:2017-04-18 17:14:58
【问题描述】:

我正在使用几个按钮在 java 中制作一个猜谜网格游戏。该程序使用 java gridlayout 布局,当单击按钮时,我希望它将其 GridLayout 位置(不是 x 和 y)添加到数组中。

这是需要单元格位置查找器的一段代码。

    public void actionPerformed(ActionEvent e)
    {


      if(placementLimit < 5){

       //placementlimit is limit to buttons clicked

        JButton clickedButton = (JButton)e.getSource(); 

        clickedButton.setBackground(Color.RED);
        /* something like: int pos =  clickedButton.getGridPos
         *  arrayplacedpositions.add(pos);
        */
        placementLimit++;
   } 

我没有制作JPanel,所以 整个项目在这里:

        /* Guessing game GoldMiner By Alexander Smirnov
 * A Computer Game where you hide and place gold.
 */ 
package standard;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;


import javax.swing.*;

public class game extends JPanel
{
    JButton buttons[] = new JButton[64];
    int placementLimit = 0;
    ArrayList goldPos = new ArrayList();


    public game()
    {
        setLayout(new GridLayout(8,8));
        initializebuttons();
    }


    public void initializebuttons()
    {
        for(int i = 0; i <= 63; i++)
        {
            buttons[i] = new JButton();
            buttons[i].setText("");
            buttons[i].addActionListener(new buttonListener());

            add(buttons[i]);
        }
    }



    private class buttonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {


            if(placementLimit < 5){
            JButton clickedButton = (JButton)e.getSource(); 
            clickedButton.setIcon(new ImageIcon("/Users/Administrator/Desktop/gold2.jpg"));
            clickedButton.setBackground(Color.RED);
            JPanel.getComponents();
            placementLimit++;
       } else {


           JOptionPane.showMessageDialog(null, "You have placed the maximum amount of gold!");

            } 

            }






    }
    public static void main(String[] args)
    {
        JFrame window = new JFrame("GoldMiner");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(new game());
        window.setBounds(300,200,300,300);
        Container c = window.getContentPane();

        window.setVisible(true);
    }
}

【问题讨论】:

标签: java swing user-interface jbutton


【解决方案1】:

这不是最简单的事情......可能将JButtons 保存在二维数组中会更有用,或者更好地将JButton 的映射保存到位置。

如果您确实必须从按钮中识别网格单元,请调用

JPanel.getComponents()(或任何父容器),然后在其中找到您的 Button。然后算一算。

假设你在索引 6 找到它。

那么由于您的网格是 x 除以 y 除以 x 得到行,而 mod 由 x 得到列。

所以说网格 ix 4x4

6/4 是 1,所以你在索引为 1 的行(第二行)

6 % 4 是 2,所以你在索引为 2 的列(第三列)

【讨论】:

  • 能否给个代码示例。
  • 另外,我的按钮是使用 for 循环形成的,所以我不能手动调用每个按钮。
  • (1+) 以获得好的建议。另外,我不确定它是否会是一个杂物,但网格位置可以在创建时使用putClientProperty(...) 方法填充到 JButton 中。
  • 或者创建一个ActionListener(或更好的AbstractAction)类,也许是一个内部类,它接受网格位置,并将其设置到按钮中。
  • 我是一个 n00b 所以你能解释一下什么是网格/行/列/边索引吗?另外,我没有 JPanel。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2022-10-09
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
相关资源
最近更新 更多