【问题标题】:BufferedImage won't showBufferedImage 不会显示
【发布时间】:2016-03-11 18:03:36
【问题描述】:

我一直在尝试创建一个其中包含 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);
}
} 

【问题讨论】:

    标签: user-interface graphics paint bufferedimage paintcomponent


    【解决方案1】:

    改成这样:

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

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

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多