求一个JAVA小程序

2024-12-04 08:50:33
推荐回答(2个)
回答1:

//ClockAnimation.java
package p1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClockAnimation extends JFrame
{
private StillClock clock=new StillClock();
public ClockAnimation()
{
add(clock);
Timer timer=new Timer(1000,new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
clock.setCurrentTime();
clock.repaint();
}
}
public static void main(String[] args) 
{
JFrame frame=new ClockAnimation();
frame.setTitle("DisplayClock");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

//StillClock.java
package p1;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class StillClock extends JPanel
{
private int hour;
private int minute;
private int second;
public StillClock()
{
setCurrentTime();
}
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.3);
int xCenter=getWidth()/2;
int yCenter=getHeight()/2;
g.setColor(Color.black);
g.drawRect(20, 20, getWidth()-70,getHeight()-70);
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);
g.setColor(Color.black);
g.drawRect(20, getHeight()-35, getWidth()-70,30);
g.drawString("Hour", 60, getHeight()-15);
g.setColor(Color.white);
g.fillRect(90, getHeight()-25, 30,15);
g.setColor(Color.black);
g.drawString(""+hour, 95,getHeight()-15 );
g.drawString("Minute", 135, getHeight()-15);
g.setColor(Color.white);
g.fillRect(180, getHeight()-25, 30,15);
g.setColor(Color.black);
g.drawString(""+minute, 190,getHeight()-15 );
g.drawString("Second", 230, getHeight()-15);
g.setColor(Color.white);
g.fillRect(280, getHeight()-25, 30,15);
g.setColor(Color.black);
g.drawString(""+second, 290,getHeight()-15 );

}
public void setCurrentTime()
{
Calendar calendar=new GregorianCalendar();
this.hour=calendar.get(Calendar.HOUR_OF_DAY);
this.minute=calendar.get(Calendar.MINUTE);
this.second=calendar.get(Calendar.SECOND);
}
public Dimension getPreferredSize()
{
return new Dimension(200,200);
}
}//这是我前段时间写的一个时钟动态显示的程序!不懂的地方就直接追问吧!

回答2:

图形界面,自己继续

第1个,可以这样
public class App_555 { class Person{ public String name; public Person (String name){ this.name=name; System.out.println("the Person of Java"); } } class Student extends Person{ int age; public Student(String name,int age){ super(name); this.age=age; } public void show(){ System.out.println("the Student of person"); } } public static void main(String[] args) { new App_555().test(); } public void test() { Person per=new Person(" m l z"); Student stu=new Student("m l z", 14); stu.show(); }}