我的JFrame

时间:2017-03-26 00:53:38

标签: java jframe jcomponent

我正在使用java制作游戏,但我无法在jframe中添加jcomponents。我已经尝试将它添加到jpanel和容器中。我甚至试图让Border类扩展JPanel而不是JComponent。提前谢谢。

问题图片

enter image description here

主类

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class game implements KeyListener {
public static JFrame window = new JFrame("Escape");
public static int velX = 0, velY = 0, count = 0;
public static Boolean paused = false, leftLock = false;
public Player player, playerTwo;
public static Movement playerMove;
public static Collision coll;
public static Area playerArea, playerTwoArea;
public Border b;

game() {
    player = new Player();
    playerTwo = new Player();
    b = new Border(10,10);
    window.setSize(1000, 500);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    window.addKeyListener(this);
    window.add(new Border(0,0));
    window.add(new Border(200,200));
    window.add(new Border(400,400));
    window.setVisible(true);
    playerMove = new Movement();
    coll = new Collision();
}
public static void main(String[] args) {
    new game();
    }
}

我的边境班

import java.awt.*;
import javax.swing.*;

public class Border extends JComponent {
    private int x, y;

Border(int x, int y) {
    this.x = x;
    this.y = y;
}
public void paintComponent(Graphics g) {
    g.setColor(Color.BLUE);
    g.fillOvel(x,y,50,50);
    }
}

1 个答案:

答案 0 :(得分:0)

如果要绘制任意(和许多)形状,您可能不应该使用JComponents。我只需在你的窗口中放置一个JPanel,覆盖它上面的paintComponents方法,然后用这一种方法为你的所有圆圈完成所有绘图。