首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java GridLayout和添加按钮的问题

Java GridLayout和添加按钮的问题
EN

Stack Overflow用户
提问于 2018-10-15 00:45:45
回答 1查看 584关注 0票数 1

我想将100个按钮添加到GridLayout中,并且我的代码可以工作,但有时它只添加一个按钮,如果我单击其他按钮所属的位置,则会显示我单击的按钮。这完全是随机发生的,我不明白。下面是我的代码:

代码语言:javascript
复制
    public class GamePanel extends JPanel {
    GameUI controler;
    GridLayout gameLayout = new GridLayout(10,10);
    JButton gameButtons[] = new JButton[100];
    ImageIcon ice;
    JButton startButton;
    JButton exitButton;
    ImageIcon startIcon;
    ImageIcon exitIcon;
    URL urlIcon;
    private int i;

    public GamePanel(GameUI controler) {
        this.setLayout(gameLayout);
        this.controler = controler;
        urlIcon = this.getClass().getResource("/icons/Overlay.png");
        ice = new ImageIcon(urlIcon);
        makeButtons();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    public void makeButtons() {
        for(i = 0; i< 100; i++) {
            gameButtons[i] = new JButton(ice);
            this.add(gameButtons[i]);
            revalidate();   
        }
        repaint();
    }
}

更新:

代码语言:javascript
复制
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;


public class GameUI extends JFrame {

ImageIcon i;
Image jFrameBackground;
JButton startButton;
JButton exitButton;
ImageIcon startIcon; 
ImageIcon exitIcon;


public GameUI() {
    setResizable(false);
    this.setSize(1200, 800);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setLayout(null);








    BackGroundPanel backGroundPanel = new BackGroundPanel();
    GamePanel panel = new GamePanel(this);
    ButtonPanel buttonPanel = new ButtonPanel();

    panel.setSize(500,500);
    panel.setLocation(100, 150);
    backGroundPanel.setSize(this.getWidth(),this.getHeight());
    backGroundPanel.setLocation(0,0);
    buttonPanel.setSize(390,50);
    buttonPanel.setLocation(100,100);
    this.add(backGroundPanel);
    this.add(panel);
    this.add(buttonPanel);




    backGroundPanel.setBackground(Color.BLACK);



}





public static void main(String[] args) throws InvocationTargetException, InterruptedException {







    javax.swing.SwingUtilities.invokeAndWait(
                new Runnable(){

                    @Override
                    public void run() {



                        GameUI ui = new GameUI();
                        ui.setVisible(true);


                    }


                }

            );




}

}

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52804899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档