开发者社区> 问答> 正文

我需要帮助来为CIV类型的游戏创建带有一组按钮和文本字段的JFrame

通常,我要为每个城市创建一个按钮网格,然后单击每个按钮时,让文本框显示有关城市的信息,例如资源,人口等。

我的代码如下。我希望布局保持不变,因为城市规模在3x2的小到6x4的中到12x8的大按钮网格之间。感谢您提供的任何帮助。

public class Map{

    JFrame frame=new JFrame(); //creates frame
    JButton[][] grid; //names the grid of button

    public Map(int width, int length){ //constructor
        frame.setLayout(new GridLayout(width+1,length)); //set layout

        grid=new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++){
            for(int x=0; x<width; x++){
                grid[x][y]=new JButton("("+x+","+y+")"); //creates new button     
                grid[x][y].addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        System.out.print("This");

                    }});
                frame.add(grid[x][y]); //adds button to grid
            }
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500); //sets appropriate size for frame
        frame.setVisible(true); //makes frame visible
    }


}

展开
收起
垚tutu 2019-12-04 17:15:05 591 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多