📄 race.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Race extends JFrame
{
String winer="WINNER IS ";
JButton b_go=new JButton("GO...");
JButton b_r1=new JButton("R1");
JButton b_r2=new JButton("R2");
JLabel lalwin=new JLabel("");
//定义界面
public Race()
{
super("Race");
JPanel p = new JPanel();
p.setLayout(null);
p.setBackground(Color.PINK);
getContentPane().add(p);
b_go.setBounds(200,50,150,20);
p.add(b_go);
lalwin.setBounds(200,80,150,20);
p.add(lalwin);
b_r1.setBounds(485,100,62,20);
b_r1.setBackground(Color.CYAN);
p.add(b_r1);
b_r2.setBounds(485,230,62,20);
b_r2.setBackground(Color.MAGENTA);
p.add(b_r2);
this.setResizable(false);
setSize(580,500);
show();
b_go.addActionListener(new Start_Listener());
}
//线程Runner1
class Runner1 extends Thread
{
public Runner1()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
b_r1.setLocation(b_r1.getX()-15,100);
try
{
this.sleep(300);
if(b_r1.getX()<=50)
{
b_r1.setBackground(Color.YELLOW);
this.destroy();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
//线程Runner2
class Runner2 extends Thread
{
int displacement=485;
public Runner2()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
b_r2.setLocation(b_r2.getX()-15,230);
try
{
this.sleep(300);
if(b_r2.getX()<=50)
{
b_r2.setBackground(Color.RED);
this.destroy();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
//按妞b_go的侦听类
class Start_Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
new Runner1();
new Runner2();
}
}
public static void main(String args[])
{
new Race();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -