BufferedImage不会显示

时间:2016-03-11 18:03:37

标签: user-interface graphics paint bufferedimage paintcomponent

我一直在尝试创建一个带有BufferedImage的JPanel,paintComponent方法运行并编译初始化BufferedImage的构造函数代码。但是,当我运行GUI时,JFrame中没有显示任何内容。有任何想法吗?请随意指出我的代码中的任何错误!

注意:您可能会注意到我的所有JButton都在运行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);
}
} 

1 个答案:

答案 0 :(得分:0)

将其更改为:

    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);

}

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