写一个不会动的时钟

    xiaoxiao2021-03-26  31

    package thinking; import java.awt.*; import javax.swing.*; import java.util.*; public class My extends JFrame{ public My(){ StillClock clock = new StillClock(); MessagePanel messagePanel = new MessagePanel("" + clock.getHour() + ":" + clock.getMinute() + ":" + clock.getSecond()); messagePanel.setCenter(true); messagePanel.setForeground(Color.blue); messagePanel.setFont(new Font("Courier",Font.BOLD,16)); setLayout(new GridLayout(0,1,5,5)); add(messagePanel); add(clock); } public static void main(String[] args){ My frame = new My(); frame.setTitle("DisplayClock"); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MessagePanel extends JPanel{ private String message = "welcome to Java!"; private boolean centered; private int xCoodinate = 20; private int yCoodinate = 20; public MessagePanel(){ } public MessagePanel(String message){ this.message = message; } public String getMessage(){ return message; } public void setMessage(String message){ this.message = message; repaint(); } public boolean isCenter(){ return centered; } public void setCenter(boolean centered){ this.centered = centered; } protected void paintComponent(Graphics g){ super.paintComponent(g); if(centered){ FontMetrics fm = g.getFontMetrics(); int stringWidth = fm.stringWidth(message); int stringAscent = fm.getAscent(); xCoodinate = getWidth()/2 - stringWidth/2; yCoodinate = getHeight()/2 - stringAscent/2; } g.drawString(message, xCoodinate, yCoodinate); } } 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.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 * (2 * Math.PI/60))); int yHour = (int)(yCenter - hLength * Math.cos(hour * (2 * Math.PI/60))); g.setColor(Color.BLACK); g.drawLine(xCenter, yCenter, xHour, yHour); } 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); } }
    转载请注明原文地址: https://ju.6miu.com/read-658760.html

    最新回复(0)