实现如图所示的界面,以及界面上的按钮,点击相应按钮实现按钮功能。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TimeF extends JFrame {
static TimeF frm = null;
static Timer timer, timerl, timerr, timet, timerd;
static JLabel lbl = new JLabel("滚动的字体");
static JButton btnt = new JButton("向上");
static JButton btnl = new JButton("向左");
static JButton btnr = new JButton("向右");
static JButton btnd = new JButton("向下");
static JButton btne = new JButton("停止");
static int i=1;
static int movex, movey;
public static void main(String[] args) {
frm = new TimeF();
timer = new Timer(10, new TimeListener());
timerl = new Timer(10, new TimeListenerL());
timerr = new Timer(10, new TimeListenerR());
timerd = new Timer(10, new TimeListenerD());
timet = new Timer(10, new TimeListenerT());
frm.setLayout(null);
frm.setBounds(100, 100, 500, 400);
frm.add(btnt);
frm.add(btnl);
frm.add(btnr);
frm.add(btne);
frm.add(btnd);
frm.add(lbl);
btnl.setBounds(190, 150, 80, 30);
btnt.setBounds(290, 100, 80, 30);
btne.setBounds(290, 150, 80, 30);
btnr.setBounds(390, 150, 80, 30);
btnd.setBounds(290, 200, 80, 30);
lbl.setBounds(100, 150, 150, 30);
btnt.addActionListener(new TimeListenerT());
btne.addActionListener(new TimeListener());
btnr.addActionListener(new TimeListenerR());
btnl.addActionListener(new TimeListenerL());
btnd.addActionListener(new TimeListenerD());
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
movex = lbl.getLocation().x;
movey = lbl.getLocation().y;
}
private static class TimeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btne) {
timerl.stop();
timerr.stop();
timer.stop();
timet.stop();
timerd.stop();
lbl.setForeground(Color.black);
}
}
}
private static class TimeListenerL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnl) {
timer.stop();
timet.stop();
timerd.stop();
timerr.stop();
timerl.start();
} else {
if (movex <= frm.getWidth() && movex > -lbl.getWidth()) {
movex -= 1;
} else if (movex <= -lbl.getWidth()) {
movex = frm.getWidth();
}
i++;
if(i>200)
i=1;
lbl.setLocation(movex, movey);
lbl.setForeground(Color.getHSBColor(i, i - 1, i + 50));
}
}
}
private static class TimeListenerR implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnr) {
timerl.stop();
timer.stop();
timerd.stop();
timet.stop();
timerr.start();
} else {
if (movex < frm.getWidth()) {
movex += 1;
} else {
movex = -lbl.getWidth();
}
i++;
if(i>200)
i=1;
lbl.setForeground(Color.getHSBColor(i, i - 1, i + 50));
lbl.setLocation(movex, movey);
}
}
}
private static class TimeListenerT implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnt) {
timerl.stop();
timer.stop();
timerd.stop();
timerr.stop();
timet.start();
} else {
if (movey > 0) {
movey -= 1;
} else {
movey = frm.getHeight();
}
i++;
if(i>200)
i=1;
lbl.setForeground(Color.getHSBColor(i, i - 1, i + 50));
lbl.setLocation(movex, movey);
}
}
}
private static class TimeListenerD implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnd) {
timerl.stop();
timer.stop();
timerr.stop();
timet.stop();
timerd.start();
} else {
if (movey < frm.getHeight()) {
movey += 1;
} else {
movey = 0;
}
i++;
if(i>200)
i=1;
lbl.setForeground(Color.getHSBColor(i, i - 1, i + 50));
lbl.setLocation(movex, movey);
}
}
}
}