实现JFrame和JPanel之间的区别

时间:2016-01-13 15:22:51

标签: java swing user-interface jframe jpanel

我试图了解JFrame和JPanel之间的区别。我倾向于使用JFrame的子类而不是JPanel,但人们总是告诉我最好使用JPanel的子类。以下是我使用JFrame的示例:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable {
int x, y, xCoord, yCoord;
private Image dbImage;
private Graphics dbg;

public void move() {
    x += xCoord;
    y += yCoord;
    if (x <= 20) {
        x = 20;
    }
    if (x >= 480) {
        x = 480;
    }
    if (y <= 40) {
        y = 40;
    }
    if (y >= 480) {
        y = 480;
    }
}

public void setXCoord(int xcoord) {
    xCoord = xcoord;
}

public void setYCoord(int ycoord) {
    yCoord = ycoord;
}

public class AL extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == e.VK_LEFT) {
            setXCoord(-1);
        }
        if (keyCode == e.VK_RIGHT) {
            setXCoord(+1);
        }
        if (keyCode == e.VK_UP) {
            setYCoord(-1);
        }
        if (keyCode == e.VK_DOWN) {
            setYCoord(+1);
        }
        Game.this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == e.VK_LEFT) {
            setXCoord(0);
        }
        if (keyCode == e.VK_RIGHT) {
            setXCoord(0);
        }
        if (keyCode == e.VK_UP) {
            setYCoord(0);
        }
        if (keyCode == e.VK_DOWN) {
            setYCoord(0);
        }
        Game.this.repaint();

    }

}

public static void main(String[] args) {
    Game game = new Game();
    Thread t = new Thread(game);
    t.start();
}

public Game() {
    addKeyListener(new AL());
    setTitle("Game");
    setSize(500, 500);
    setResizable(true);
    setVisible(true);
    setBackground(Color.BLACK);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    x = 250;
    y = 250;

}

public void paintComponent(Graphics g) {
    g.setColor(Color.GREEN);
    g.fillOval(x, y, 15, 15);
}

@Override
public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}

@Override
public void run() {
    try {
        while (true) {
            move();
            Thread.sleep(30);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

这很好(除了按住其中一个按钮时有一点延迟),但当我尝试通过实现JPanel而不是JFrame来更改我的代码时,没有任何显示...这是代码JPanel子类:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable {
int x, y, xCoord, yCoord;
private Image dbImage;
private Graphics dbg;
JFrame frame;

public void move() {
    x += xCoord;
    y += yCoord;
    if (x <= 20) {
        x = 20;
    }
    if (x >= 480) {
        x = 480;
    }
    if (y <= 40) {
        y = 40;
    }
    if (y >= 480) {
        y = 480;
    }
}

public void setXCoord(int xcoord) {
    xCoord = xcoord;
}

public void setYCoord(int ycoord) {
    yCoord = ycoord;
}

public class AL extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == e.VK_LEFT) {
            setXCoord(-1);
        }
        if (keyCode == e.VK_RIGHT) {
            setXCoord(+1);
        }
        if (keyCode == e.VK_UP) {
            setYCoord(-1);
        }
        if (keyCode == e.VK_DOWN) {
            setYCoord(+1);
        }
        Game.this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == e.VK_LEFT) {
            setXCoord(0);
        }
        if (keyCode == e.VK_RIGHT) {
            setXCoord(0);
        }
        if (keyCode == e.VK_UP) {
            setYCoord(0);
        }
        if (keyCode == e.VK_DOWN) {
            setYCoord(0);
        }
        Game.this.repaint();

    }

}

public static void main(String[] args) {
    Game game = new Game();
    Thread t = new Thread(game);
    t.start();
}

public Game() {
    frame = new JFrame();
    frame.addKeyListener(new AL());
    frame.setTitle("Game");
    frame.setSize(500, 500);
    frame.setResizable(true);
    frame.setVisible(true);
    frame.setBackground(Color.BLACK);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    x = 250;
    y = 250;

}

@Override
public void paintComponent(Graphics g) {
    g.setColor(Color.GREEN);
    g.fillOval(x, y, 15, 15);
}

@Override
public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}

@Override
public void run() {
    try {
        while (true) {
            move();
            Thread.sleep(30);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

2 个答案:

答案 0 :(得分:1)

如代码所示,您需要自定义JPanel,因为您想要更改某些方法的行为,例如paintComponent

但是,您不需要自定义 JFrame,因此无需创建扩展它的类。

最后,您的主要课程不需要成为您的小组课程。

这是一个示例类,我将框架内容从Game的构造函数移动到此主类。

 public class MainClass {

    public static void main(String[] args) {

    Game game = new Game();

    JFrame frame = new JFrame();

    frame.addKeyListener(new AL());
    frame.setTitle("Game");
    frame.setSize(500, 500);
    frame.setResizable(true);
    frame.getContentPane().add(game);
    frame.setBackground(Color.BLACK);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    Thread t = new Thread(game);
    t.start();

    }


    }

答案 1 :(得分:1)

JFrame是一个比你想象的要复杂得多的组件,对于初学者来说,它有一个JRootPane作为它的主要容器,其中包含contentPane,{ {1}}并控制JMenuBar

RootPane

有关详细信息,请参阅How to Use Root Panes

glassPane也有装饰(边框),这些边框是在窗口本身的范围内绘制的,然后内容会在这些边框内展开,因此边框不会在其上绘制。< / p>

当您覆盖顶级容器的JFrame时,如paint,您遇到了许多问题:

一般来说,从OOP的角度来看,您实际上并没有在课程中添加任何新功能(或者至少没有任何新功能可以通过更好的方法生成)。

当您使用paint之类的内容时,上述所有其他内容都不再受到关注:

  • 默认情况下,它们是双缓冲的
  • 如果您使用布局管理器(在内容窗格中),该组件将在框架的装饰中布局
  • 组件的JPanelwidth代表整个可见区域
  • 您可以将此组件添加到您想要的容器

您还应该覆盖height的{​​{1}}方法并返回您希望面板通常的首选尺寸,然后您可以JPanel使用&#34 ;包&#34;在它周围的窗口,这将使窗口大于内容区域,但意味着你不会挠头,想知道为什么你将窗口设置为一定的大小,但你的组件较小