qq_30941763
2017-08-27 03:31
采纳率: 100%
浏览 4.2k
已采纳

java中panel的背景图片总是挡住按钮怎么解决?

package ShootGame;

import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ShootGame extends JFrame
{
GamePanel jp;
JButton beginButton;
JButton settingsButton;
JButton helpButton;

public static BufferedImage startBackGound;
static
{
try {
startBackGound=ImageIO.read(ShootGame.class.getResource("startBackGround.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ShootGame()
{
super("飞机大战");
setSize(1920,821);
jp=new GamePanel();
getContentPane().add(jp);

beginButton=new JButton("开始游戏");
settingsButton=new JButton("设置");
helpButton=new JButton("帮助");

jp.add(beginButton);
jp.add(settingsButton);
jp.add(helpButton);

beginButton.setBounds(500,500,100,20);
settingsButton.setBounds(500,530,100,20);
helpButton.setBounds(500,560,100,20);

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

class GamePanel extends JPanel
{
GamePanel()
{
setLayout(null);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(startBackGound, 0, 0, null);
}
}
public static void main(String[] args)
{
new ShootGame();
}
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题