| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 794 人关注过本帖
标题:求助,一个图形界面的小程序
只看楼主 加入收藏
xiaoyaotian
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-5-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
求助,一个图形界面的小程序
(设置时钟时间)编写一个程序,显示时钟时间并通过在三个文本域中输入小时,分钟,秒来设置时钟的时间

时钟显示不出来,哪边错啦?帮忙修改
程序代码:
[color=#0000FF]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
      
public class SetClockTime extends JFrame{
    int hour,minute,second;
  
  StillClock clock = new StillClock(hour,minute,second);
  
    JTextField jtf1 = new JTextField(5);
    JTextField jtf2 = new JTextField(5);
    JTextField jtf3 = new JTextField(5);
  
    public static void main(String[] args) {
        SetClockTime frame = new SetClockTime();
        frame.setTitle("indicate the time of the clock");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,200);
        frame.setVisible(true);
     
    }
  
    public SetClockTime(){
      
   
  
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.CENTER));
        p1.add(new JLabel("Hour"));
        p1.add(jtf1);
        p1.add(new JLabel("Minute"));
        p1.add(jtf2);
        p1.add(new JLabel("Second"));
        p1.add(jtf3);
        add(p1,BorderLayout.SOUTH);
      
        add(clock,BorderLayout.NORTH);
      
        //register listenner
        jtf1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
           
               jtf1.requestFocusInWindow();   
            }    
        });
      
        jtf2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            
               jtf2.requestFocusInWindow();   
            }    
        });
      
        jtf3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
             
               jtf3.requestFocusInWindow();
             
              
            }    
        });
   
       addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
             
               hour =Integer.parseInt(jtf1.getText());
               minute =Integer.parseInt(jtf2.getText());
               second =Integer.parseInt(jtf3.getText());
            }
    });
     
    }
}

 class StillClock extends JPanel{
     private int hour;
     private int minute;
     private int second;
   
      public StillClock(int hour,int minute,int second){
          this.hour = hour;
          this.minute = minute;
          this.second = second;
          
      }
    
      public int getHour(){
          return hour;
      }
    
      public void setHour(int hour){
          this.hour = hour;
          repaint();
      }
    
      public int getMinute(){
          return minute;
      }
    
      public void setMinute(int minute){
          this.minute = minute;
          repaint();
      }
    
      public int getSecond(){
          return second;
      }
    
      public void setSecond(int second){
          this.second = second;
          repaint();
      }
   
      protected void paintComponent(Graphics g){
         super.paintComponent(g);
       
         int clockRadius = (int)(Math.min(getWidth(), getHeight())*0.8*0.5);
         int xCenter = getWidth()/2;
         int yCenter = getHeight()/2;
       
         g.setColor(Color.black);
         g.drawOval(xCenter - clockRadius,yCenter - clockRadius, 2*clockRadius,2*clockRadius);
         g.drawString("12",xCenter - 5, yCenter - clockRadius + 12);
         g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
         g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
         g.drawString("6", xCenter - 3, yCenter +clockRadius - 3);
       
       
         int sLength = (int)(clockRadius * 0.8);
         int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI/60)));
         int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI/60 )));
         g.setColor(Color.red);
         g.drawLine(xCenter,yCenter,xSecond,ySecond);
       
       
         int mLength = (int)(clockRadius *0.65);
         int xMinute = (int)(xCenter + mLength * Math.sin(minute*(2*Math.PI/60)) );
         int yMinute = (int)(yCenter - mLength * Math.cos(minute*(2*Math.PI/60)) );
         g.setColor(Color.blue);
         g.drawLine(xCenter,yCenter,xMinute,yMinute);
       
       
         int hLength = (int)(clockRadius * 0.5);
         int xHour = (int)(xCenter +hLength * Math.sin((hour %12 + minute/60.0)*(2*Math.PI/12)));
         int yHour = (int)(yCenter -hLength * Math.cos((hour %12 + minute/60.0)*(2*Math.PI/12)));
         g.setColor(Color.green);
         g.drawLine(xCenter,yCenter, xHour, yHour);
     }

 }
下。
[/color]
搜索更多相关主题的帖子: 时间 时钟 second minute 
2013-05-01 10:37
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
收藏
得分:7 
JPanel 没有加入到JFrame 里,如何显示?加一句 frame.add(p1);
2013-05-01 18:45
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:7 
楼上正解


旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-05 00:36
littleR
Rank: 2
等 级:论坛游民
威 望:1
帖 子:13
专家分:50
注 册:2013-5-6
收藏
得分:7 
2楼正解 frame.add(clock);

相信,一定可以
2013-05-06 18:29
快速回复:求助,一个图形界面的小程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029664 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved