java - 新创建的 JButton 不会覆盖之前创建的 JButton

标签 java image swing

本质上,我编写的是一个益智游戏。

它包含一个图像,该图像进一步分为 9 block ,放置在包含 3x3 JButton GridLayout 的 JPanel 上。最初,9 个按钮是空的。当用户点击“开始游戏”时,9个按钮将显示按钮上的图像。

我使用 setPreferredSize() 来设置包含 9 个空 JButton 的 JPanel 的大小。之后,我使用 Inset ( 0,0,0,0 ) 使按钮的内容填充整个按钮。

但是现在,当我想添加图像按钮来替换用户单击“开始游戏”时的空按钮时,它不起作用。

我认为这是因为我之前设置的 setPreferredSize() 阻止了 Insets 值的工作。

我插入了一些system.out.println值来检查该方法是否正在运行,它运行了,但是当用户单击“开始游戏”时,图像仍然拒绝出现在按钮上。

    public class GameFrame extends JFrame implements ActionListener {
        private JButton button1;
        private JButton[] button = new JButton[9];
        private Insets buttonMargin;
        private boolean testImageMethod;
        private JPanel puzpiece;

        public GameFrame(){
            //.. coding ..

            // create new buttons - button1
            button1  = new JButton("Start Game");
            // add action event to "Start" button
            button1.addActionListener(this);

            // creates a new panel for the splitted puzzle pieces
            puzpiece = new JPanel();
            puzpiece.setLayout(new GridLayout(3,3));

            // check if testImageMethod boolean ( in setupImage() ) is true, 
            //if it isn't, adds 9 buttons w/o images.
             for(int a=0; a<9; a++){
                 if(testImageMethod){
                 }
                 else{
                     // adds 9 buttons without images 
                   button[a] = new JButton();  
                   puzpiece.add(button[a]);
                   puzpiece.setPreferredSize(new Dimension(500,200));
                 }


             }
             // adds puzpiece panel into the frame 
              this.add(puzpiece,BorderLayout.WEST);     
            //.. coding ..

        }

        public void actionPerformed(ActionEvent e){
            if (e.getSource() == button1){
                // puzpiece.button.setVisible(false);
                //puzpiece.remove(button);

                // call setImage() method
                setImage();

                for(int a=0; a<9; a++){
                // adds the new 9 buttons with images into panel
                puzpiece.add(button[a]);
                // test if method is running
                System.out.println("qq");
                }
            }
            else{
                System.out.println("bbb");
            }
        } 

        // method setImage() divides the image into subimages
        public void setImage(){
          //.. coding ..
          // test if method is running
          System.out.println("a");

            setupImage( count++, sc );
        }

        // method setupImage() adds the subimages to the buttons
        private void setupImage( int a, Image wi )
        { 
          // test if method is running
          System.out.println("d");

            buttonMargin = new Insets( 0, 0, 0, 0 );
            button[a] = new JButton( new ImageIcon( wi ) );
            button[a].setMargin( buttonMargin );

            // test if method is running
            System.out.println("e");

        } // end method setupImage()
    }

最佳答案

我不确定我是否确切知道你在做什么,但是......

  • 看来您正在使用普通 JButton 的 3x3 网格填充 JPanel,
  • 按下按钮时,您将添加显示图像的 JButton。
  • 但我没有看到您在添加新按钮之前删除了原始按钮。
  • 我也没有看到您在更改组件后在 puzpiece JPanel 上调用 revalidate()repaint()
  • 更重要的是,当交换 JButton 中已经由 puzpiece JPanel 保存的 ImageIcons 更容易时,为什么要交换 JButton?这是我 10 分钟前在评论中推荐的内容,但现在正在做出回答。

关于java - 新创建的 JButton 不会覆盖之前创建的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14410892/

相关文章:

java - Java 类 CookieHandler 和 CookieManager 如何工作?

java - 自定义 CursorAdapter 导致 GridView 性能滞后

java - java中将图像旋转指定角度

java - 使用 Cell 渲染器时获取 Click JTable 的位置

java - 如何使 JPanel 居中(包含 JBox,但不一定)?

java - 我怎样才能增加/减少点击事件的窗口大小?

java - 获取文件的 MIME 类型

java - 使用引用文献和新的

html - 重复部分中的图像复选框

java - 在 java 中查找 RGB 的按位版本