「描画」ボタンを押しても時計が表示されません。ボタン内のtextFieldオブジェクトでsetTextメソッドを呼び出すとうまくいくので、ボタンが機能することはわかっています。いろいろ試してみましたが、うまくいくものが見つかりませんでした。私も教授に近づき、彼も大変な時間を過ごしました。しかし、フレームのサイズを変更すると、突然表示されました...理由がわかりません。「描画」ボタンを押すだけで、時計を表示する方法を見つけようとしています。どんな助けでもありがたいです、そして、事前にどうもありがとうございました!
自分で実行したい場合に備えて、クラス全体を以下に示しますが、簡潔にするために、ClockFrameクラス内の内部クラスに注目してください。
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 HOURS_ON_CLOCK = 12;
final double MINUTES_IN_HOUR = 60;
final double HOUR_LENGTH = .4;
final double MINUTE_LENGTH = .9;
private double radius;
private double posXY;
private double hourHandLength;
private double minuteHandLength;
private double center;
private Line2D.Double hourHand;
private Line2D.Double minuteHand;
public ClockComponent(double rad, double xy)
{
radius = rad;
posXY = xy;
center = posXY + radius;
hourHandLength = radius * HOUR_LENGTH;
minuteHandLength = radius * MINUTE_LENGTH;
hourHand = new Line2D.Double(center, center,
center, center - hourHandLength);
minuteHand = new Line2D.Double(center, center,
center, center - minuteHandLength);
}
public void setClock(int hour, int minute)
{
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 + hourHandLength *
Math.sin((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS),
center - hourHandLength *
Math.cos((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS));
minuteHand.setLine(center, center,
center + minuteHandLength * Math.sin(minute * MINUTES_TO_RADIANS),
center - minuteHandLength * Math.cos(minute * MINUTES_TO_RADIANS));
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
double diameter = 2 * radius;
Ellipse2D.Double clockFace =
new Ellipse2D.Double(posXY, posXY, diameter, diameter);
g2.draw(clockFace);
g2.draw(hourHand);
g2.draw(minuteHand);
}
}
ActionListener内部クラスを持つClockFrameクラス。ActionListener内部クラスは私が何かを見逃しているところだと感じますが、正確に何を理解することはできません...
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
public class ClockFrame extends JFrame
{
final int FIELD_SIZE = 10;
final int FRAME_WIDTH = 450;
final int FRAME_HEIGHT = 450;
final int FIELD_WIDTH = 5;
private JPanel frameFace;
private JPanel fieldPanel;
private JPanel drawingPanel;
private JLabel hourLabel;
private JLabel minuteLabel;
private JTextField hourField;
private JTextField minuteField;
private JButton drawButton;
private ClockComponent clock;
public ClockFrame(double rad, double xy)
{
setTitle("Clock viewer");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
frameFace = new JPanel(new BorderLayout());
fieldPanel = new JPanel();
drawingPanel = new JPanel();
drawingPanel.setBorder(new EtchedBorder());
hourLabel = new JLabel();
hourLabel.setText("Hour ");
minuteLabel = new JLabel();
minuteLabel.setText("Minute ");
hourField = new JTextField(FIELD_SIZE);
minuteField = new JTextField(FIELD_SIZE);
drawButton = new JButton("Draw");
drawingPanel.setLayout(new GridLayout(1, 1));
clock = new ClockComponent(rad, xy);
fieldPanel.add(hourLabel);
fieldPanel.add(hourField);
fieldPanel.add(minuteLabel);
fieldPanel.add(minuteField);
fieldPanel.add(drawButton);
ActionListener clockListener = new DrawListener();
drawButton.addActionListener(clockListener);
frameFace.add(fieldPanel, BorderLayout.NORTH);
frameFace.add(drawingPanel, BorderLayout.CENTER);
add(frameFace);
}
class DrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int hour = Integer.parseInt(hourField.getText());
int minute = Integer.parseInt(minuteField.getText());
clock.setClock(hour, minute);
drawingPanel.add(clock);
}
}
}
そして主な方法
import javax.swing.JFrame;
public class ClockViewer
{
public static void main(String[] args)
{
JFrame frame = new ClockFrame(75, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}