猿问

Java GridLayout 和添加按钮的问题

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


    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();

    }

}



千巷猫影
浏览 207回答 1
1回答

杨__羊羊

正如我在评论中提到的,您使用的是空布局,这是问题的根源。您正在使用 null 布局尝试将 JPanel 分层,一个在另一个之上,这不是它的使用方式或用途,也不是您应该如何创建背景。这具有背景覆盖按钮的效果,直到您的鼠标悬停在按钮上。相反,如果您想创建背景图像,我建议您:创建一个 JPanel,比如说叫 BackgroundPanel,覆盖其paintComponent方法,super.paintComponent(g);在方法的第一行调用它然后绘制它应该显示的图像然后给它一个像样的布局管理器并添加您的GUI组件给它确保添加到它的任何 JPanels 都是透明的&nbsp;.setOpaque(false)其他选项包括使用 JLayeredPane,但您真的不需要它只是为了有背景。例如,以下代码产生:import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.*;public class GameUI2 {&nbsp; &nbsp; private static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/3/3f/"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "Butterfly_Nebula_in_narrow_band_Sulfur%2C_Hydrogen_and_Oxygen_Stephan_Hamel.jpg";&nbsp; &nbsp; private static final String BTN_IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/5/54/Crystal_Project_Games_kids.png";&nbsp; &nbsp; private static void createAndShowGui() {&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage bgImg = null;&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage btnImg = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL bgImgUrl = new URL(IMG_PATH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL btnImgUrl = new URL(BTN_IMG_PATH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bgImg = ImageIO.read(bgImgUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnImg = ImageIO.read(btnImgUrl);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(-1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; BackgroundPanel2 mainPanel = new BackgroundPanel2(bgImg);&nbsp; &nbsp; &nbsp; &nbsp; mainPanel.setLayout(new GridBagLayout());&nbsp; &nbsp; &nbsp; &nbsp; GamePanel2 gamePanel = new GamePanel2(btnImg);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mainPanel.add(gamePanel);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Game");&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(mainPanel);&nbsp; &nbsp; &nbsp; &nbsp; frame.setResizable(false);&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(() -> createAndShowGui());&nbsp; &nbsp; }}@SuppressWarnings("serial")class BackgroundPanel2 extends JPanel {&nbsp; &nbsp; private Image backgroundImg;&nbsp; &nbsp; public BackgroundPanel2(Image backgroundImg) {&nbsp; &nbsp; &nbsp; &nbsp; this.backgroundImg = backgroundImg;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; if (backgroundImg != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawImage(backgroundImg, 0, 0, this);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dimension getPreferredSize() {&nbsp; &nbsp; &nbsp; &nbsp; if (isPreferredSizeSet() || backgroundImg == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.getPreferredSize();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int w = backgroundImg.getWidth(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int h = backgroundImg.getHeight(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(w, h);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}@SuppressWarnings("serial")class GamePanel2 extends JPanel {&nbsp; &nbsp; public static final int MAX_BUTTONS = 100;&nbsp; &nbsp; private static final int IMG_WIDTH = 40;&nbsp; &nbsp; JButton[] gameButtons = new JButton[MAX_BUTTONS];&nbsp; &nbsp; public GamePanel2(Image buttonImg) {&nbsp; &nbsp; &nbsp; &nbsp; setOpaque(false);&nbsp; &nbsp; &nbsp; &nbsp; if (buttonImg.getWidth(this) > IMG_WIDTH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttonImg = buttonImg.getScaledInstance(IMG_WIDTH, IMG_WIDTH, Image.SCALE_SMOOTH);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Icon icon = new ImageIcon(buttonImg);&nbsp; &nbsp; &nbsp; &nbsp; setLayout(new GridLayout(10, 10, 4, 4));&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < gameButtons.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int finalIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JButton btn = new JButton(icon);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btn.addActionListener(e -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = String.format("Button: %02d", finalIndex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(btn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameButtons[i] = btn;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答