缓冲图像不会显示



我一直在尝试创建一个带有 BufferedImage 的 JPanel,paintComponent 方法与初始化 BufferedImage 的构造函数代码一起运行和编译。尽管如此,当我运行 GUI 时,JFrame 中没有显示任何内容。有什么想法吗?随时指出我的代码中的任何错误!

注意:您可能会注意到我所有的JButtons都执行System.exit,我知道这一点,它仅用于测试目的。

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sun.audio.*;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
public class IntroductionComponent extends JPanel {
    /**
    * Generated SVUID
    */
    private static final long serialVersionUID = -1355536543860254435L;
private BufferedImage img;
private Audio audio;
public IntroductionComponent() {
    try {
    img = ImageIO.read(new File("C:/Users/KChel-2/Downloads/HauntedHouse.jpg"));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
JButton start = new JButton("Start");
start.addActionListener(e -> System.exit(0));
JButton opt = new JButton("Options");
opt.addActionListener(e -> System.exit(0));
JButton cred = new JButton("Credits");
cred.addActionListener(e -> System.exit(0));
JButton exit = new JButton("Exit");
exit.addActionListener(e -> System.exit(0));
setLayout(new GridLayout(1, 2));
JPanel buttons = new JPanel();
buttons.add(start);
buttons.add(opt);
buttons.add(cred);
buttons.add(exit);
add(buttons);
}
protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.drawImage(img, 0, 0, null);           
       }
//Audio.sound1.loop();
}

import javax.swing.JFrame;
public class IntroductionGUI
{
    public static void main(String[] args)
    {
    IntroductionComponent comp = new IntroductionComponent();
    JFrame frame = new JFrame("The Story");
    frame.setSize(1600, 850);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comp);
    frame.setVisible(true);
}
} 

将其更改为如下所示:

    public static void main(String[] args)
    {
    IntroductionComponent comp = new IntroductionComponent();
    JFrame frame = new JFrame("The Story");
    frame.setSize(1600, 850);
    frame.setLayout(new GridLayout(1, 2));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comp);
    JPanel buttons = new JPanel();
    buttons.setLayout(new GridLayout(2, 2));
    JButton start = new JButton("Start");
    start.addActionListener(e -> System.exit(0));
    JButton opt = new JButton("Options");
    opt.addActionListener(e -> System.exit(0));
    JButton cred = new JButton("Credits");
    cred.addActionListener(e -> System.exit(0));
    JButton exit = new JButton("Exit");
    exit.addActionListener(e -> System.exit(0));
    buttons.add(start);
    buttons.add(opt);
    buttons.add(cred);
    buttons.add(exit);
    frame.add(buttons);
    frame.setVisible(true);
}

或者,您可以创建一个按钮面板类。

相关内容

  • 没有找到相关文章

最新更新