出于某种原因,当我将 ClockComponent 对象添加到 JPanel 对象时,它没有显示出来。当我将它添加到 JFrame 对象而不是 JPanel 对象时,它确实会显示出来......我不明白为什么,我在这里发布这个问题之前已经查过了,但无济于事。有什么方法可以让我的从 JComponent 继承的 ClockComponent 对象显示在 JPanel 上?非常感谢您提前提供的帮助。
这是我的 ClockViewer 类,它在运行时不会显示时钟:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ClockViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Clock viewer");
frame.setSize(500, 500);
JPanel panel = new JPanel();
ClockComponent comps = new ClockComponent(7, 40);
panel.add(comps);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
这是我的 ClockComponent 类供参考:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
public class ClockComponent extends JComponent
{
final int CENTER = 150;
final double HOUR_HAND_LENGTH = 20;
final double MINUTE_HAND_LENGTH = 40;
private Line2D.Double hourHand;
private Line2D.Double minuteHand;
public ClockComponent(double hour, double minute)
{
hourHand = new Line2D.Double(CENTER, CENTER,
CENTER, CENTER - HOUR_HAND_LENGTH);
minuteHand = new Line2D.Double(CENTER, CENTER,
CENTER, CENTER - MINUTE_HAND_LENGTH);
final int HOURS_ON_CLOCK = 12;
final int MINUTES_IN_HOUR = 60;
final double HOURS_TO_RADIANS = 2 * Math.PI / HOURS_ON_CLOCK;
final double MINUTES_TO_RADIANS = 2 * Math.PI / MINUTES_IN_HOUR;
hourHand.setLine(CENTER, CENTER, CENTER + HOUR_HAND_LENGTH *
Math.sin((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS),
CENTER - HOUR_HAND_LENGTH *
Math.cos((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS));
minuteHand.setLine(CENTER, CENTER,
CENTER + MINUTE_HAND_LENGTH * Math.sin(minute * MINUTES_TO_RADIANS),
CENTER - MINUTE_HAND_LENGTH * Math.cos(minute * MINUTES_TO_RADIANS));
}
public void paintComponent(Graphics g)
{
final int DIAMETER = 100;
final int POS_X_Y = CENTER - DIAMETER / 2;
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double clockFace =
new Ellipse2D.Double(POS_X_Y, POS_X_Y, DIAMETER, DIAMETER);
g2.draw(clockFace);
g2.draw(hourHand);
g2.draw(minuteHand);
}
}