【发布时间】:2018-11-14 09:17:19
【问题描述】:
我正在尝试让图像显示在我正在创建的游戏的框架上。我没有扩展任何 JPanel 或 JFrame,而是创建了多个框架类和一个动作类。我目前无法在我创建的测试框架上显示图像。我的代码可以编译,但是当我单击框架时它会关闭。谢谢!
P.S 这只是我的代码的一部分。听说把总代码发到网上不好。
代码:
public void testframe(){
testframe = new Frame("Main Menu");
testframe.setSize(1600,1600);
try{
myPicture = ImageIO.read(new File("basic.png"));
}
catch(IOException e){
e.printStackTrace();
//FirstFrame.setVisible(true);
//testframe.setVisible(false);
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
testframe.add(picLabel);
//testframe.drawImage(myPicture,"png", );
testframe.setVisible(true);
}
private void showButtonDemo(){
Font myFont = new Font("Serif", Font.ITALIC | Font.BOLD, 12);
Font newFont = myFont.deriveFont(50F);
headerLabel.setText("SNAKE GAME");
headerLabel.setFont(newFont);
statusLabel.setText("By Tejas and Ashwin");
statusLabel.setFont(newFont);
Button startButton = new Button("Start Game");//begins game
Button snakecolorButton = new Button("Choose Snake Color");//selects snake color
Button themeColorButton = new Button("Choose Theme");//selects two colors(1 primary and 1 secondary)
Button InstructionsButton = new Button("Instructions");
Button testbutton = new Button("test");
testbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//testframe();
//testframe.setVisible(true);
FirstFrame.setVisible(false);
}
});
【问题讨论】:
-
发布minimal reproducible example 以获得最佳帮助。
-
为什么要使用 AWT 组件,例如 Frame 和 Button 与 Swing组件?
-
这对我来说更容易,因为我没有完全理解所有的 Swing 和 AWT 组件。这不是一个最小的、完整的和可验证的例子吗?我应该如何添加?
-
请阅读MCVE链接
-
Swing 组件名称以
J开头(JFrame、JButton等)。 AWT 组件(Frame、Button等)较旧,一般不应再使用。