【发布时间】:2014-04-05 05:21:07
【问题描述】:
我正在尝试使用 Java 在 Eclipse 中设置背景图像,我认为我已经完成了大部分工作。
我正在尝试制作 2D 游戏,我想将背景图片添加到我的menuJFrame,您将在下面看到。
我已经有了这个代码:
这是我的主要课程JFrames:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrames extends JPanel implements ActionListener {
JFrame menuJFrame,howToPlayJFrame, level1JFrame;
JPanel menuJPanel,howToPlayJPanel;
JButton howToPlayButton,backToMainMenuButton,startGameButton, quitProgramButton;
JLabel howToPlayLabel;
public static void main(String [] args){
JFrames jframes = new JFrames();
}
public JFrames(){
//menuJFrame
menuJFrame = new JFrame("SquareRun/Menu");
menuJFrame.setVisible(true);
menuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuJFrame.setSize(800, 600);
//menuJPanel
menuJPanel = new JPanel();
menuJFrame.add(menuJPanel);
howToPlayButton = new JButton("How To Play");
menuJPanel.add(howToPlayButton);
howToPlayButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == howToPlayButton){
howToPlayJFrame.setVisible(true);
}
}});
startGameButton = new JButton("Start Game");
menuJPanel.add(startGameButton);
startGameButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startGameButton)
level1JFrame.setVisible(true);
menuJFrame.setVisible(false);
}});
quitProgramButton = new JButton("Quit Game");
menuJPanel.add(quitProgramButton);
quitProgramButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == quitProgramButton){
menuJFrame.dispose();
}
}});
//howToPlayJFrame
howToPlayJFrame = new JFrame("SquareRun/HowToPlay");
howToPlayJFrame.setVisible(false);
howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
howToPlayJFrame.setSize(600, 100);
//howToPlayJPanel
howToPlayJPanel = new JPanel();
howToPlayJFrame.add(howToPlayJPanel);
howToPlayLabel = new JLabel("Use the arrow keys to move, Up= jump, Down= down, Right= right, Left= left");
howToPlayJPanel.add(howToPlayLabel);
backToMainMenuButton = new JButton("Close Window");
howToPlayJPanel.add(backToMainMenuButton);
backToMainMenuButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == backToMainMenuButton){
howToPlayJFrame.setVisible(false);
howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}});
//level1JFrame
level1JFrame = new JFrame("Level 1");
level1JFrame.setVisible(false);
level1JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
level1JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public void actionPerformed(ActionEvent e) {
}
public Object getCurrentLevel() {
return null;
}
}
这是我的背景类:
import java.awt.Image;
import javax.swing.ImageIcon;
public class Background extends JFrames {
private JFrames game;
private Image image;
public Background(JFrames game){
this.game = game;
image = (new ImageIcon("Image001.png")).getImage();
}
}
【问题讨论】:
-
你的问题是?
-
看看这个问题,尤其是 Michael Myers 的回答:stackoverflow.com/questions/1064977/… Background 类不是正确的方法,而是使用
JComponent之后设置的JFrame#setContentPane方法。
标签: java eclipse image swing background