为什么我的JPanel或JFrame没有出现?

时间:2014-02-08 18:57:49

标签: java swing netbeans jframe jpanel

我正在制作游戏。项目中有3个不同的文件,有一个名为Dude,Frame和Board的文件。所以有人可以帮助我吗?代码: 董事会代码:
包我们的游戏;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener{
Dude p;
Image img;
Timer time;

    public Board() {
    p = new Dude();
    addKeyListener(new AL());
    setFocusable(true);
    ImageIcon i = new ImageIcon("/Users/appleuser/Desktop");
    img = i.getImage();
    time = new Timer(5, this);
    time.start();
    }
    public void actionPerformed(ActionEvent e) {
        p.move();
    repaint();
    }

    public void paint(Graphics g) {
    super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.drawImage(img, 0, 0, null);
        g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    }

    private class AL extends KeyAdapter {
    public void keyReleased(KeyEvent e) {
    p.keyReleased(e);
    }
    public void keyPressed(KeyEvent e) {
    p.keyPressed(e);
    }
    }
}

Dude的代码:

    package Ourgame;

    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;

    public class Dude {
        int x, dx, y;
        Image still;

    public Dude() {
        ImageIcon i = new ImageIcon("/Users/appleuser/Desktop/the man.bmp");
        still = i.getImage();
        x = 10;
        y = 172;
    }

    public void move() {
        x = x + dx;

    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }

    public Image getImage() {
        return still;
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if(key == KeyEvent.VK_LEFT);
        dx = -1;

        if(key == KeyEvent.VK_RIGHT)
        dx = 1;

    }

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT);
    dx = 0;

    if(key == KeyEvent.VK_RIGHT)
    dx = 0;

}



}

Frame的代码:

package Ourgame;

import javax.swing.JFrame;

public class Frame {

    public static void main(String[] args){
    JFrame frame = new JFrame("2D game");
    frame.add(new Board());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1200, 365);
    frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:0)

您可能只需要调用JFrame.pack()。布局管理器可能没有设置董事会的界限。尝试:

frame.getContentPane().add(new Board());
frame.pack();
frame.setVisible(true);

如果这不起作用,那么我建议你按照这里的其他建议:使用调试器和/或检查你的图像路径。 “/ Users / appleuser / Desktop”绝对不是图像。

答案 1 :(得分:0)

尝试在EDT上调用Swing代码:

package Ourgame;

import javax.swing.JFrame;

public class Frame
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater( new Runnable()
    {
      @Override
      public void run()
      {
        JFrame frame = new JFrame("2D game");
        frame.add(new Board());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1200, 365);
        frame.setVisible(true);
      }
    });
  }
}