Java 背景不显示
我做的背景没有出现,这对我来说是个大问题。代码如下:Java 背景不显示,java,swing,background,jframe,jpanel,Java,Swing,Background,Jframe,Jpanel,我做的背景没有出现,这对我来说是个大问题。代码如下: //Package and imports... public class startScreen extends JFrame { public static asteriod asteriod = new asteriod(); static JFrame Screen = new JFrame("Stars"); static boolean isShown = (boolean) Screen.isVisib
//Package and imports...
public class startScreen extends JFrame
{
public static asteriod asteriod = new asteriod();
static JFrame Screen = new JFrame("Stars");
static boolean isShown = (boolean) Screen.isVisible();
static heart heart = new heart();
public static JPanel jpanel = new JPanel();
static Random rand = new Random();
static int suptogus = rand.nextInt(20)+1;
static enemy enemy = new enemy();
static player player = new player();
static JLabel creatoranddate = new JLabel("Copyright 2013© All rights reserved.");
public static int HEALTH = 100;
public static int health = 50;
public startScreen()
{
super("Stars");
setLayout(new FlowLayout());
Screen.setContentPane(new JPanel()
{
Image image = ImageIO.read(new File("src/stars/optionsBackground.png"));
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, 700, 600, this);
}
});
creatoranddate.setFont(new Font("Arial", Font.BOLD, 14));
creatoranddate.setForeground(Color.WHITE.brighter());
Point location = creatoranddate.getLocation();
System.out.println("Located JLabel, at position " + location);
add(creatoranddate);
//adds background picture
try{
//adds logo
JLabel logo = (new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/logo.png")))));
add(logo);
System.out.println("Successfully added logo.\n");
}catch(IOException e){
System.out.println("Failed to display logo. This is prone for the Stars.jar program.\n");
e.printStackTrace();
}
try{
JLabel enemyLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemyLabel.png"))));
JLabel playerLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerLabel.png"))));
playerLabel.setForeground(null);
enemyLabel.setForeground(null);
enemyLabel.setBounds(0, 0, 700, 600);
playerLabel.setBounds(20, 20, 700, 600);
JLabel player = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerSprite.png"))));
JLabel enemy = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemySprite.png"))));
player.setForeground(null);
enemy.setOpaque(true);
player.setBounds(0, 0, 700, 600);
enemy.setBounds(0, -32, 700, 600);
}catch (Exception e){
e.printStackTrace();
}
}
public String getVersion(String str)
{
return str = "Version InDev 2.0";
}
public static void main(String[]args)
{
}
}
问题在于
super(“Stars”)声明下方的所有代码
至paintComponent
方法的末尾。我不知道发生了什么,即使我抛出IOException,它仍然不会显示背景图像。Tristen:你会发现,在一个简单的程序中单独测试问题代码要好得多。通过这种方式,您可以查看问题是在图像文件中读取还是在渲染中读取。因此,我建议您通过划分和征服来进行一些调试。使用标准Java命名约定。你的代码太难读了。类名应以大写字符开头。变量名称应以小写字符开头。您的代码不一致。您也不应该使用静态变量。像“HEALTH”和“HEALTH”这样的变量名也很容易混淆。不要用case来区分变量。正如上面的评论所说,这太正确了。此外,您是否尝试覆盖,以及paintComponent()
。让它返回一些有效的维度对象,比如repturn(newdimension(300300))代码>。我想,如果您在项目中添加了正确意义上的资源,就可以了:-)此外,您正在通过编写static JFrame Screen=new JFrame(“Stars”)来初始化JFrame
代码>,即使您的类扩展了JFrame
本身。因此,您应该删除该行并使用setContentPane(new JPanel(){…})
,而不是Screen.setContentPane(new JPanel(){…})