JFrame调整大小时显示时钟

时间:2012-11-08 03:47:27

标签: java swing user-interface button

当我按下“绘图”按钮时,时钟不会显示。我知道按钮有效,因为在其中的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);
                }
            }

1 个答案:

答案 0 :(得分:3)

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

clock.setClock(hour, minute);
drawingPanel.add(clock);
drawingPanel.revalidate();
drawingPanel.repaint();