【发布时间】:2016-01-14 14:49:56
【问题描述】:
我正在尝试制作一个轨道模拟器,但我遇到了这个问题。我已经检查了整个 Stack Overflow,但我找不到解决方案。我只是想手动绘制到JPanel,但它没有出现在上面。我已将布局设置为 null,使其可见,将其添加到 JFrame,将正文添加到 Plane,以及您通常会做的所有事情。
这是 Body 类:
package viperlordx.orbitsimulator;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class Body extends JLabel {
private double mass;
private Point location;
private Vector velocity;
private Color color;
private Plane plane;
public Body(double mass, Point location, Vector velocity, Color color) {
this.mass = mass;
this.location = location;
this.velocity = velocity;
this.color = color;
this.setVisible(true);
this.setBounds(location.x, location.y, 100, 100);
}
public void moveTick() {
velocity.addTo(location);
}
public void setPlane(Plane plane) {
this.plane = plane;
}
public Plane getPlane() {
return plane;
}
@Override
public void paintComponent(Graphics g) {
System.out.println("Painting");
super.paintComponent(g);
if (plane != null && g != null) {
g.setColor(color);
g.fillOval(location.x, location.y, getWidth(), getHeight());
}
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
public Vector getVelocity() {
return velocity;
}
public void setVelocity(Vector vector) {
velocity = vector;
}
}
还有平面类:
package viperlordx.orbitsimulator;
import java.awt.Graphics;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Plane extends JPanel {
private HashSet<Body> bodies = new HashSet<Body>();
public void addBody(Body body) {
this.add(body);
bodies.add(body);
}
public Plane() {
this.setLayout(null);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (bodies != null) {
for (Body body : bodies) {
body.paintComponent(g);
}
}
}
public Set<Body> getBodies() {
return bodies;
}
}
现在是主类:
package viperlordx.orbitsimulator;
import java.awt.Color;
import java.awt.Point;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 1000);
frame.setLocationRelativeTo(null);
Plane plane = new Plane();
frame.add(plane);
plane.setVisible(true);
frame.setLayout(null);
plane.setBounds(0, 0, 1000, 1000);
plane.setBackground(Color.WHITE);
plane.addBody(new Body(10.0, new Point(10, 10), new Vector(0, 0), Color.GREEN));
frame.setVisible(true);
frame.setTitle("Orbit");
plane.repaint();
}
}
【问题讨论】:
-
在哪里实例化 Plane,在哪里以及如何将它添加到容器中?
-
我会在主课上折腾。
-
尝试删除“frame.setLayout(null);”
-
我添加了这个,因为我认为它会有所帮助。这不是问题的根源。它发生在我添加之前。执行 frame.setLayout(null) 只允许您使用绝对坐标而不是限制性布局。
-
刚刚用普通的JPanel而不是Plane进行了测试,它可以工作。您能否更改背景颜色以检查面板是否可见?
标签: java swing jpanel paintcomponent