简体   繁体   English

添加JFrame背景图片后,JPanel / JButtons消失

[英]JPanel/JButtons disappear after JFrame addition of background image

Java newbie here... I am trying to add an image to the background of my JFrame. Java新手...我正在尝试将图像添加到JFrame的背景。 I already had buttons added to the panel, on that JFrame. 我已经在该JFrame的面板上添加了按钮。 When I added the image to the JFrame, my buttons disappeared. 当我将图像添加到JFrame时,我的按钮消失了。

public class BackPanel extends JFrame { 公共类BackPanel扩展了JFrame {

JFrame frame = new JFrame("Blackjack Game");

ImageIcon background;
JLabel backgroundLbl;

JButton newRoundButton;
JButton endButton;

ImageIcon image;

public BackPanel(GameConsole game){

    final GameConsole game1 = game;
    frame.setSize(800, 600);
  //frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

  //frame.setContentPane(new JLabel(new ImageIcon(getClass().getResource("cardBackground.jpg"))));
    ImagePanel panel = new ImagePanel("cardBackground.jpg");
    panel.setPreferredSize(new Dimension(800, 600));



    //put frame in the middle of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);


    //create 2 buttons on the panel

    panel.add(endButton);
    panel.add(newRoundButton);
    frame.getContentPane().add(panel, BorderLayout.CENTER);   //add the panel to the frame

//  frame.pack();
    frame.setVisible(true);
    }
}

I have a feeling it has something to do with layout, but when I use other layouts it doesn't seem to help, or it also makes the image disappear. 我感觉它与布局有关,但是当我使用其他布局时,它似乎无济于事,否则也会使图像消失。

I have changed the file from "cardBackground.jpg" to "" in order to see if the buttons are somehow underneath the image. 我将文件从“ cardBackground.jpg”更改为“”,以查看按钮是否在图像下方。 They are not. 他们不是。

I have tried to widen the frame to see if the buttons are next to the image but they aren't. 我试图加宽框架以查看按钮是否在图像旁边,但实际上不是。 (Which wouldn't make sense anyways because they are not on the same panel). (无论如何,因为它们不在同一面板上,所以这毫无意义)。

What am I missing on this? 我在这方面想念什么?

EDIT: Updated code that is not working: Displays buttons but no background image. 编辑:更新的代码不起作用:显示按钮,但没有背景图像。

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BackPanel{

    public BackPanel(GameConsole game){
        final GameConsole game1 = game;

        JFrame frame = new JFrame("Blackjack Game");
        ImagePanel panel = new ImagePanel("cardBackground.jpg");
        panel.setPreferredSize(new Dimension(800, 600));


        //create 2 buttons on the panel
        JButton newRoundButton= new JButton("Start another round");
        newRoundButton.setPreferredSize(new Dimension(150, 50));
        newRoundButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                game1.userTurn();
            }
        });

        JButton endButton = new JButton("End Game");
        endButton.setPreferredSize(new Dimension(150, 50));
        endButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "You have chosen to quit.\n"
                        + "Thank you for playing Blackjack.");
                System.exit(0);
            }
        });

        panel.add(endButton);
        panel.add(newRoundButton);

        frame.add(panel);
        frame.setSize(800, 600);

        frame.setLocationRelativeTo(null);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setVisible(true);
        }
    }

Instead of adding an image background like this, make a new JPanel class that has an overloaded paintComponent() function where you draw an image. 与其添加这样的图像背景,不如创建一个新的JPanel类,该类具有在其中绘制图像的重载paintComponent()函数。 Here is a link that exemplifies this solution: 以下是示例此解决方案的链接:

Adding an Image to a JPanel Background 将图像添加到JPanel背景

Happy coding! 编码愉快!

Edit: 编辑:

I whipped up an example in Eclipse and everything worked fine for me. 我在Eclipse中举了一个例子,一切对我来说都很好。 Here is the code I used: 这是我使用的代码:

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {
    public static void main(String[] args) throws IOException {
        JFrame myFrame = new JFrame();

        //Make a background panel
        ImagePanel panel = new ImagePanel("back.jpg");
        panel.setPreferredSize(new Dimension(800, 600));

        //Make buttons
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");

        //Add components
        panel.add(button1);
        panel.add(button2);

        myFrame.add(panel);

        //Set window size
        myFrame.setSize(800, 600);

        //Center it
        myFrame.setLocationRelativeTo(null);

        //Make it close normally
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Make it visible (finalization)
        myFrame.setVisible(true);
    }
}

Here is the ImagePanel code from before as well: 这也是之前的ImagePanel代码:

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

    private Image image = null;

    public ImagePanel(String filename) {
        this.image = new ImageIcon(filename).getImage();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
    }
}

And here are the results: http://postimg.org/image/93k3j5f43/ 结果如下: http : //postimg.org/image/93k3j5f43/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM