将图像放在背景中

时间:2015-04-29 05:25:53

标签: java swing

我正在尝试将图像作为背景。 当我运行我的代码时,屏幕上没有任何显示,另一个屏幕显示2个按钮和黄色背景。如何更换黄色并放入我想要的图像?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GameFrame extends JFrame {

    public BufferedImage image;
    public JFrame gameframe;
    public JPanel panel;
    public JButton b1;
    public JButton b2;

    public JLabel lab;

    public GameFrame() {
        //SETTING GAMEFRAME
        gameframe = new JFrame("Poop MAN");

        gameframe.setVisible(true);
        gameframe.setSize(1350, 1000);
        gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();

        panel.setBackground(Color.yellow);

        //BUTTONS
        panel.setLayout(null);
        b1 = new JButton("CONTROLS");
        b2 = new JButton("START");
        b1.setBounds(500, 200, 150, 150);
        b2.setBounds(500, 400, 150, 150);

        b1.setPreferredSize(new Dimension(50, 50));
        b2.setPreferredSize(new Dimension(30, 50));

        panel.add(b1, BorderLayout.SOUTH);
        panel.add(b2, BorderLayout.SOUTH);

        gameframe.add(panel);
        gameframe.repaint();

    }

    public void ImagePanel() {
        try {
            image = ImageIO.read(new File("unnamed"));
        } catch (IOException ex) {

        }
    }

    public BufferedImage getImg() {
        return image;
    }
}

1 个答案:

答案 0 :(得分:-1)

它不那么直接让它看起来不错。看一下背景专家组https://tips4java.wordpress.com/2008/10/12/background-panel/