1

当我按下“绘图”按钮时,时钟不显示。我知道该按钮可以正常工作,因为在其中的 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);
                }
            }
4

1 回答 1

3

每当您在实现 GUI 后将组件添加到容器或从容器中删除组件时,您需要告诉该容器重新布置其组件并重新绘制它们。这是通过调用revalidate()然后repaint()在容器上完成的,这里是drawingPanel:

clock.setClock(hour, minute);
drawingPanel.add(clock);
drawingPanel.revalidate();
drawingPanel.repaint();
于 2012-11-08T03:51:30.830 回答