第六题:小车来回运动,一个按钮控制开动停止
public class CH01 extends JFrame {
private JPanel contentPane;
private JLabel car;
private JButton bt1;
Thread t=new Thread(new CarThread());
int x=0;
int flag=1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CH01 frame = new CH01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CH01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
car = new JLabel("小车");
car.setFont(new Font("宋体", Font.BOLD, 25));
car.setOpaque(true);
car.setBackground(Color.RED);
car.setBounds(0, 170, 58, 27);
contentPane.add(car);
t.start();
bt1 = new JButton("开动");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(bt1.getText().equals("开动")){
bt1.setText("停止");
x=10;
}else{
bt1.setText("开动");
x=0;
}
}
});
bt1.setBounds(282, 346, 113, 27);
contentPane.add(bt1);
}
class CarThread extends Thread{
public void run(){
while(true){
try {
if(car.getX()+car.getWidth()>contentPane.getWidth()) flag=-1;
else if(car.getX()<0)flag=1;
car.setLocation(car.getX()+(x*flag),car.getY());
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
第七题:小车循环运动,两个按钮控制开动和停止
public class CH01 extends JFrame {
private JPanel contentPane;
private JLabel car;
private JButton bt1;
Thread t=new Thread(new CarThread());
int x=0;
private JButton bt2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CH01 frame = new CH01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CH01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
car = new JLabel("小车");
car.setFont(new Font("宋体", Font.BOLD, 25));
car.setOpaque(true);
car.setBackground(Color.RED);
car.setBounds(78, 170, 58, 27);
contentPane.add(car);
t.start();
bt1 = new JButton("开动");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
x=10;
}
});
bt1.setBounds(282, 346, 113, 27);
contentPane.add(bt1);
bt2 = new JButton("停止");
bt2.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0) {
x=0;
}
});
bt2.setBounds(409, 346, 113, 27);
contentPane.add(bt2);
}
class CarThread extends Thread{
public void run(){
while(true){
if(car.getX()>contentPane.getWidth()) car.setLocation(0-car.getWidth(), car.getY());
else car.setLocation(car.getX()+x,car.getY());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
第八题:每单击按钮一次出现一辆小车
@SuppressWarnings("serial")
public class CH01 extends JFrame {
private JPanel contentPane;
private JLabel car;
private JButton bt1;
int x;
Random rand=new Random();
int hight;
int h;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CH01 frame = new CH01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CH01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
bt1 = new JButton("开动");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
car = new JLabel("小车");
car.setFont(new Font("宋体", Font.BOLD, 25));
car.setOpaque(true);
car.setBackground(Color.RED);
car.setBounds(0-car.getWidth(),0-car.getHeight(), 58, 27);
contentPane.add(car);
x=0-car.getWidth();
hight=contentPane.getHeight();
h=rand.nextInt(hight);
new CarThread(car,x,hight,h).start();
}
});
bt1.setBounds(282, 346, 113, 27);
contentPane.add(bt1);
}
}
class CarThread extends Thread{
JLabel car;
int x,hight,h;
CarThread(JLabel car,int x,int hight,int h){
this.car=car;
this.x=x;
this.hight=hight;
this.h=h;
}
public void run(){
while(true){
car.setLocation(x++, h);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
第九题:标签实时显示时间,点击按钮可以切换
@SuppressWarnings("serial")
public class CH01 extends JFrame {
private JPanel contentPane;
private JLabel car;
private JButton bt1;
Thread t=new Thread(new TimeThread());
int x=0;
int flag=24;
String tt;
int h,m,s;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CH01 frame = new CH01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CH01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
car = new JLabel();
car.setFont(new Font("宋体", Font.BOLD, 25));
car.setOpaque(true);
car.setBackground(Color.RED);
car.setBounds(80, 170, 200, 27);
contentPane.add(car);
t.start();
bt1 = new JButton("12制");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(bt1.getText().equals("12制")){
bt1.setText("24制");
flag=12;
}else {
bt1.setText("12制");
flag=24;
}
}
});
bt1.setBounds(282, 346, 113, 27);
contentPane.add(bt1);
}
class TimeThread extends Thread{
public void run(){
for(int i = 0; i > -1; i++) {
try{
TimeThread.sleep(1000);
Calendar cal = Calendar.getInstance();
StringBuilder builder = new StringBuilder();
h=cal.get(Calendar.HOUR_OF_DAY);
m=cal.get(Calendar.MINUTE);
s=cal.get(Calendar.SECOND);
if(h>12 && h<18){
tt="下午";
}else if(h==12){
tt="中午";
}else if(h>5 && h<12){
tt="上午";
}else if(h>18 && h<24){
tt="晚上";
}else {
tt="凌晨";
}
if(flag==12){
h=h%12;
}
if(flag==12){
builder.append(tt+h+":"+m+":"+s);
}else {
builder.append(h+":"+m+":"+s);
}
car.setText( builder.toString());
} catch(InterruptedException e) {
e.printStackTrace();
} }
}
}
}
第十题:计时器和倒计时功能
public class CH01 extends JFrame {
private JPanel contentPane;
private JButton bt2;
private JButton bt1;
private JTextField text;
private JLabel label1;
private JLabel label2;
private static final String str = "00:00:00";
private timer t=new timer();
// 记录程序开始时间
private long kaishi = System.currentTimeMillis();
// 程序一开始就是暂停的
private long pauseStart = kaishi;
// 程序暂停的总时间
private long pauseCount = 0;
int h,m,rs,seconds;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CH01 frame = new CH01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CH01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
t.start();
bt1 = new JButton("启动");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
seconds=Integer.valueOf(text.getText());
h= seconds / 60 / 60;
m = seconds / 60 % 60;
rs = seconds % 60;
label2.setText(String.valueOf(h)+":"+String.valueOf(m)+":"+String.valueOf(rs));
if (t.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
t.stopped = false;
bt1.setText("继续");
}
}
});
bt1.setBounds(56, 203, 93, 23);
contentPane.add(bt1);
bt2 = new JButton("停止");
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
pauseStart = System.currentTimeMillis();
t.stopped = true;
}
});
bt2.setBounds(241, 203, 93, 23);
contentPane.add(bt2);
text = new JTextField();
text.setFont(new Font("宋体", Font.PLAIN, 20));
text.setBounds(53, 10, 198, 42);
contentPane.add(text);
text.setColumns(10);
label1 = new JLabel(str);
label1.setFont(new Font("宋体", Font.PLAIN, 20));
label1.setBounds(56, 77, 198, 34);
contentPane.add(label1);
label2 = new JLabel("倒计时");
label2.setFont(new Font("宋体", Font.PLAIN, 20));
label2.setBounds(56, 133, 198, 42);
contentPane.add(label2);
}
private class timer extends Thread {
public boolean stopped = true;
private timer() {
setDaemon(true);
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - kaishi - pauseCount;
label1.setText(format(elapsed));
rs--;
if (rs<0) {
m--;
rs=59;
}
if (m<0) {
h--;
m=59;
}
if (h<0) {
break;
}
label2.setText(String.valueOf(h)+":"+String.valueOf(m)+":"+String.valueOf(rs));
}
try {
sleep(1000); // 1毫秒更新一次显示
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
// 将毫秒数格式化
private String format(long elapsed) {
int hour, minute, second;
elapsed = elapsed / 1000;
second = (int) (elapsed % 60);
elapsed = elapsed / 60;
minute = (int) (elapsed % 60);
elapsed = elapsed / 60;
hour = (int) (elapsed % 60);
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
}