我尝试在环境Eclipse的JFrame中将.jpg导入到JPanel的背景中。有人可以告诉我我在这里做错了什么吗?


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;    
import java.io.*;
import java.util.*;
import java.lang.String;
public class Game extends JFrame
{
    public IntroPanel introduction;
    public InstructPanel instructions;
    public GamePanel gameboard;
    public ScorePanel highscore;
    public QuestionPanel questions;
    public AnswerPanel right;
    public FailPanel wrong;
    public Container c;
    public CardLayout cards;
    private boolean runStandAlone = true;
    public static void main (String [] args)
    {
        Game gem = new Game();
    }
    public Game()
    {
        setVisible(true); // allows JFrame to be seen
        setSize(800, 800); // sets the size
        setTitle("Biopardy"); // sets the title
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // Closing is taken care of 
        // by the following WindowListener, there's no default close operation.
        addWindowListener(new WindowAdapter() 
        { // lets the program run without having to import all WindowListener methods
            public void windowClosing(WindowEvent evt) 
            { // this gives the user a chance to stop the close operation
                int response = JOptionPane.showConfirmDialog(null, "Do you really want to quit?");
                if(response == JOptionPane.YES_OPTION) 
                {
                    dispose();
                    if(runStandAlone)
                        System.exit(0);
                }
            }
        });
        c = this.getContentPane();
        cards = new CardLayout();
        c.setLayout(cards);
        introduction = new IntroPanel(); // initializes new panel
        add(introduction, BorderLayout.CENTER); // adds the panel to the center
        instructions = new InstructPanel(); // initializes new panel
        c.add(instructions, "Instructions"); // string identifier (call on panel by using this name)
        gameboard = new GamePanel(); // initializes new panel
        c.add(gameboard, "Start"); // string identifier (call on panel by using this name)
        highscore = new ScorePanel(); // initializes new panel
        c.add(highscore, "High Score"); // string identifier (call on panel by using this name)
        questions = new QuestionPanel(); // initializes new panel
        right = new AnswerPanel(); // initializes new panel
        wrong = new FailPanel(); // initializes new panel
    }
class IntroPanel extends JPanel implements ActionListener
{
    private JButton start, record, instruct;
    private JTextField username;
    private Image jeopardy;
    public IntroPanel(Image jeopardy)
    {
        setBackground(Color.white); // sets background color
        this.setLayout(new FlowLayout());
        this.jeopardy = Toolkit.getDefaultToolkit().getImage("jeopardy.jpg");
        start = new JButton("Start");
        start.addActionListener(this);
        this.add(start);
        instruct = new JButton("Instructions");
        instruct.addActionListener(this);
        this.add(instruct);
        record = new JButton("High Score");
        record.addActionListener(this);
        this.add(record);
        username = new JTextField("User Name");
        username.addActionListener(this);
        this.add(username);
    }
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(command.equals("Start"))
            cards.show(c, "Start");
        else if(command.equals("Instructions"))
            cards.show(c, "Instructions");
        else if(command.equals("High Score"))
            cards.show(c, "High Score");
    }
    public void WaitForImage(JApplet component, Image image)   
    {
        MediaTracker tracker = new MediaTracker(component);
        try 
        {
            tracker.addImage(image, 0);
            tracker.waitForID(0);
        }
        catch(InterruptedException evt)  
        {
            evt.printStackTrace();   
        }
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(jeopardy, 0, 0, null);
    }
}

我唯一看到的是它应该是

this.jeopardy = Toolkit.getDefaultToolkit().getImage("jeopardy.jpg");

你只是在设置方法变量。

编辑:你还调用空构造函数。请确保为面板调用了正确的构造函数。

相关内容

  • 没有找到相关文章

最新更新