📄 saima.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.util.*;
class MyPanel extends JPanel {
int x1=50;
int x2=50;
int x3=50;
int x4=50;
MyPanel(){
this.setBackground(Color.white);
}
public void rep(){
this.repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img=Toolkit.getDefaultToolkit().getImage("1.gif");
g.drawString("1号马",10,50);
g.drawImage(img,x1,20,this);
g.drawString("2号马",10,100);
g.drawImage(img,x2,70,this);
g.drawString("3号马",10,150);
g.drawImage(img,x3,120,this);
g.drawString("4号马",10,200);
g.drawImage(img,x4,170,this);
}
}
class LayoutPanel extends JPanel implements ActionListener
{
JButton mb1=new JButton("开始");
JButton mb2=new JButton("充值");
JRadioButton rb1=new JRadioButton("1号马");
JRadioButton rb2=new JRadioButton("2号马");
JRadioButton rb3=new JRadioButton("3号马");
JRadioButton rb4=new JRadioButton("4号马");
ButtonGroup bg=new ButtonGroup();
JLabel mlb1=new JLabel("请选择一匹赛马:");
JTextArea mta=new JTextArea(); //显示比赛结果
JLabel mlb2=new JLabel("您可以下注的总金额:");
JLabel mlb3=new JLabel(); // 显示下注的金额
int count=10000; //下注金额的初始值
JLabel mlb4=new JLabel(); // 显示下注输赢情况
JLabel mlb5=new JLabel("请下注:");
JTextField mtf=new JTextField(); // 下注金额
Thread[] horse;
//设置布局
LayoutPanel(){
this.setLayout(null);
this.add(mta);
mta.setBounds(10,10,520,80);
this.add(mlb1);
mlb1.setBounds(10,90,120,25);
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
this.add(rb1);
this.add(rb2);
this.add(rb3);
this.add(rb4);
rb1.setSelected(true);
rb1.setBounds(130,90,60,30);
rb2.setBounds(200,90,60,30);
rb3.setBounds(270,90,60,30);
rb4.setBounds(340,90,60,30);
this.add(mlb2);
mlb2.setBounds(10,130,160,25);
this.add(mlb3);
mlb3.setText(count+"");
mlb3.setBounds(200,130,50,25);
this.add(mlb4);
mlb4.setBounds(270,130,150,25);
this.add(mlb5);
mlb5.setBounds(100,165,80,25);
this.add(mtf);
mtf.setText("0");
mtf.setBounds(200,165,200,25);
this.add(mb1);
mb1.setBounds(180,200,60,30);
this.add(mb2);
mb2.setBounds(260,200,60,30);
mb1.addActionListener(this);
mb2.addActionListener(this);
}
//引入线程
public void setThread(Thread[] horse )
{
this.horse=horse;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="开始")
{
if(Integer.parseInt(mtf.getText())<=0)
{
mlb4.setText("不能为0,请下注!");
return;
}
else
{
mlb4.setText("");
count-=Integer.parseInt(mtf.getText());
mlb3.setText(count+"");
horse[0].start();
horse[1].start();
horse[2].start();
horse[3].start();
mb1.setLabel("继续");
//该位置插入判断结果
}
}
else if(e.getActionCommand()=="继续")
{
count-=Integer.parseInt(mtf.getText());
mlb3.setText(count+"");
horse[0].resume();
horse[1].resume();
horse[2].resume();
horse[3].resume();
mb1.setLabel("继续");
//同上一位置
}
else if(e.getSource()==mb2)
{ count+=1000;
mlb3.setText(count+"");
}
}
}
class Horse1 extends Thread
{
MyPanel mp;
int steep;
public void setMP(MyPanel mp)
{
this.mp=mp;
}
public void run(){
while(true)
{
steep=(int)(Math.random()*4)+1;
mp.x1+=steep;
if(mp.x1>=600)
{
mp.x1=50;
this.suspend();
}
try{
Thread.sleep(50);
}catch(Exception ex){
}
}
}
}
class Horse2 extends Thread
{
MyPanel mp;
int steep;
public void setMP(MyPanel mp)
{
this.mp=mp;
}
public void run(){
while(true)
{
steep=(int)(Math.random()*4)+1;
mp.x2+=steep;
if(mp.x2>=600)
{
mp.x2=50;
this.suspend();
}
mp.rep();
try{
Thread.sleep(50);
}catch(Exception ex){
}
}
}
}
class Horse3 extends Thread
{
MyPanel mp;
int steep;
public void setMP(MyPanel mp)
{
this.mp=mp;
}
public void run(){
while(true)
{
steep=(int)(Math.random()*4)+1;
mp.x3+=steep;
if(mp.x3>=600)
{
mp.x3=50;
this.suspend();
}
mp.rep();
try{
Thread.sleep(50);
}catch(Exception ex){
}
}
}
}
class Horse4 extends Thread
{
MyPanel mp;
int steep;
public void setMP(MyPanel mp)
{
this.mp=mp;
}
public void run(){
while(true)
{
steep=(int)(Math.random()*4)+1;
mp.x4+=steep;
if(mp.x4>=600)
{
mp.x4=50;
this.suspend();
}
mp.rep();
try{
Thread.sleep(50);
}catch(Exception ex){
}
}
}
}
class MainFrame extends JFrame
{ MyPanel mp=new MyPanel();
LayoutPanel lp=new LayoutPanel();
//设置主界面
MainFrame(){
super("赛马游戏");
this.setSize(600,480);
this.setDefaultCloseOperation(3);
this.setLayout(new GridLayout(2,1));
Horse1 h1=new Horse1();
h1.setMP(mp);
Horse2 h2=new Horse2();
h2.setMP(mp);
Horse3 h3=new Horse3();
h3.setMP(mp);
Horse4 h4=new Horse4();
h4.setMP(mp);
Thread[] horse={h1,h2,h3,h4};
lp.setThread(horse);
this.add(mp);
this.add(lp);
this.setVisible(true);
}
public static void main(String arg[])
{
new MainFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -