實驗的時候要求做一個走馬燈程序,寫完順便發上來填補下博客的空白,日後有空還會發更多自己編寫的程序,讓大家指教指教……
package clock;
import Java.awt.*;
import Java.awt.event.*;
import Javax.swing.*;
import Java.util.Calendar;
import Java.util.Date;
import Java.text.*;
public class removingLight extends JFrame {
public removingLight() {
Font font1 = new Font("幼圓", Font.BOLD, 16);
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(
"EEEE,MMMMdd日,yyyy年 HH:mm:ss");
String mDateTime = formatter.format(cal.getTime());
MovingMessagePanel messagePanel = new MovingMessagePanel(mDateTime);
messagePanel.setFont(font1);
messagePanel.setBackground(Color.BLACK);
messagePanel.setForeground(Color.PINK);
add(messagePanel);
}
public static void main(String[] args) {
removingLight frame = new removingLight();
JLabel label = new JLabel("開始調試時間:5月5日 結束調試時間:5月6日");
label.setBackground(Color.black);
frame.setTitle("軟件1班 XXX 3107006757");
frame.setLocationRelativeTo(null);
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 120);
frame.setVisible(true);
frame.add(label, BorderLayout.SOUTH);
}
static class MovingMessagePanel extends JPanel {
private String message = " ";
private int xCoordinate = 0;
private int yCoordinate = 40;
public MovingMessagePanel(String message) {
this.message = message;
Timer timer = new Timer(100, new TimerListener());
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) {
xCoordinate = -100;
}
xCoordinate += 5;
g.drawString(message, xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
}