Topic: 程序JAVA想实现到300减到0后,sum值-5

  Print this page

1.程序JAVA想实现到300减到0后,sum值-5 Copy to clipboard
Posted by: 笑_谈
Posted on: 2005-12-23 14:39


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class ChangeTitle extends JFrame implements ActionListener {
  int x = 0;
  int y = 0;
  int z = 0;
  int zz = 0;
  int sum = 0;
  int yy = 0;
  int tmp = 0;
  Random random = new Random();

  CheckersPanel checkers = new CheckersPanel();

  // 使用布局网格
  JButton b1 = new JButton("出题");
  JButton b2 = new JButton("解答");
  JTextField names3 = new JTextField(3);
  JLabel tink5 = new JLabel("0");
  JLabel tink6 = new JLabel("0");
  JLabel tink8 = new JLabel("您的成绩");
  JLabel tink9 = new JLabel("0");
  JLabel tink1 = new JLabel("题目");
  JLabel tink2 = new JLabel("+");
  JLabel tink3 = new JLabel("=");
  JLabel tink10 = new JLabel("剩余时间");

  public ChangeTitle() {
    super("云海软件小铙钹工作室 name");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b1.addActionListener(this);
    b2.addActionListener(this);
    JPanel pane = new JPanel();
    pane.add(tink1);
    pane.add(tink5);
    pane.add(tink2);
    pane.add(tink6);

    pane.add(tink3);
    pane.add(names3);
    pane.add(b1);
    pane.add(b2);

    pane.add(tink8);
    pane.add(tink9);
    pane.add(tink10);
    pane.add(checkers.ji);
    setContentPane(pane);
  }

  public static void main(String[] arg) {
    ChangeTitle frame = new ChangeTitle();

    frame.names3.setText("0");
    //frame.show();
    frame.pack();
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source == b2) {

      yy = Integer.parseInt(names3.getText());
      // z=x+y;

      if (yy == z) {
        sum += 5;
        tink9.setText("" + sum);
        JOptionPane.showMessageDialog(null, "计算正确!");
        z = 0;
        tink5.setText("" + x);// 此处标签反了,以后注意!!数据紧贴
        tink6.setText("" + y);
      } else {
        sum -= 5;
        tink9.setText("" + sum);
        JOptionPane.showMessageDialog(null, "计算不正确!");
        z = 0;
        tink5.setText("" + x);// 此处标签反了,以后注意!!数据紧贴
        tink6.setText("" + y);
      }
      b2.setVisible(false);
      b1.setVisible(true);
      checkers.stopAnimation(); // 计时
      checkers.jis = 300;
      checkers.ji.setText("300");

    }

    if (source == b1) {

      x = 0;
      y = 0;
      x = random.nextInt(100);
      y = random.nextInt(100);
      zz = random.nextInt(4);

      if (zz == 0) {
        z = x + y;
        tink2.setText("+");
        tink5.setText("" + x);// 此处标签反了,以后注意!!数据紧贴
        tink6.setText("" + y);
      }
      if (zz == 1) {
        tink2.setText("-");
        if (x < y) {
          tmp = y;
          y = x;
          x = tmp;
          z = x - y;
          tink5.setText("" + x);// 此处标签反了,以后注意!!
          tink6.setText("" + y);
          x = 0;
          y = 0;
        }
        if (x >= y) {
          z = x - y;
          tink5.setText("" + x);// 此处标签反了,以后注意!!
          tink6.setText("" + y);
          x = 0;
          y = 0;
        }

      }
      if (zz == 2) {
        z = x * y;
        tink2.setText("*");
        tink5.setText("" + x);// 此处标签反了,以后注意!!
        tink6.setText("" + y);
      }
      x = 0;
      y = 0;
      if (zz == 3) {
        if (y != 0) {
          z = x % y;
          // 余数不为0的挑出
          if (z == 0) {
            tink2.setText("/");
            z = x / y;
            tink5.setText("" + x);// 此处标签反了,以后注意!!
            tink6.setText("" + y);
            x = 0;
            y = 0;
          }
        }
      }
      b1.setVisible(false);
      b2.setVisible(true);
      checkers.playAnimation();
    }

    repaint();
  }

  class CheckersPanel extends JPanel implements Runnable {
    // ChangeTitle frame;
    private Thread runner;

    JLabel ji = new JLabel("300");

    int jis = 300;

    void playAnimation() {
      if (runner == null)
        ;
      {
        runner = new Thread(this);
        runner.start();
      }
    }

    void stopAnimation() {
      if (runner != null)
        ;
      {
        runner = null;
      }
    }

    public void run() {
      Thread thisThread = Thread.currentThread();
      while (runner == thisThread) {
        // repaint();

        if (jis >= 0)
          jis -= 1;
        if (jis == 0) {
          JOptionPane.showMessageDialog(null, "时间到!");
          // ChangeTitle.sum=ChangeTitle.sum-5;
          // ji.setText(""+ChangeTitle.sum);
          jis = 300;
          stopAnimation();
        }

        ji.setText("" + jis);
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
      }
    }

  }

}

2.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: asus526
Posted on: 2005-12-23 16:40

公共类型ChangeTtile必须在它自己的文件中定义

3.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-23 19:54

Please use your editor to format the code properly before you post...

no-brainer way to do the job (don't care about CheckersPanel is an inner class):
pass the instance of ChangeTitle into CheckersPanel checkers (uncomment ChangeTitle frame;)
and manipulate this ChangeTitle object in the run method.
frame.sum = frame.sum - 5;
frame.tinkXX.setText("" + frame.sum);
// I just try to reuse your commented out code since I have no interest in dealing with this mess

4.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: 笑_谈
Posted on: 2005-12-26 10:22

不好使啊!?各位大虾,反这个错误:
java.lang.NullPointerException
at ChangeTitle$CheckersPanel.run(ChangeTitle.java:197)
at java.lang.Thread.run(Unknown Source)
急急急!谢谢

5.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-26 11:34

笑_谈 wrote:
不好使啊!?各位大虾,反这个错误:
java.lang.NullPointerException
at ChangeTitle$CheckersPanel.run(ChangeTitle.java:197)
at java.lang.Thread.run(Unknown Source)
急急急!谢谢

Do you think anyone could tell what's wrong without looking at the source code?
Do you think anyone other than you could access the source and know what is on line 197 of ChangeTitle.java?

6.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: 笑_谈
Posted on: 2005-12-26 12:49

新代码不好使如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class ChangeTitle extends JFrame implements ActionListener{
int x=0;
int y=0;
int z=0;
int zz=0;
int sum=0;
int yy=0;
int tmp=0;
Random random = new Random();
CheckersPanel checkers = new CheckersPanel();
//使用布局网格
JButton b1 = new JButton("出题");
JButton b2 = new JButton("解答");
JTextField names3=new JTextField(3);
JLabel tink5=new JLabel("0");
JLabel tink6=new JLabel("0");

JLabel tink8=new JLabel("您的成绩");
JLabel tink9=new JLabel("0");
JLabel tink1=new JLabel("题目");
JLabel tink2=new JLabel("+");
JLabel tink3=new JLabel("=");
JLabel tink10=new JLabel("剩余时间");
public ChangeTitle() {
super("云海软件小铙钹工作室");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
JPanel pane = new JPanel();
pane.add(tink1);
pane.add(tink5);
pane.add(tink2);
pane.add(tink6);

pane.add(tink3);
pane.add(names3);
pane.add(b1);
pane.add(b2);

pane.add(tink8);
pane.add(tink9);
pane.add(tink10);
pane.add(checkers.ji);
setContentPane(pane);
}

public static void main(String[] arg) {
ChangeTitle frame = new ChangeTitle();


frame.names3.setText("0");
frame.show();
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();

if (source == b2)
{

yy=Integer.parseInt(names3.getText());
//z=x+y;


if (yy==z)
{
sum+=5;
tink9.setText(""+sum);
JOptionPane.showMessageDialog(null,"计算正确!");
z=0;
tink5.setText(""+x);//此处标签反了,以后注意!!数据紧贴
tink6.setText(""+y);
}
else
{
sum-=5;
tink9.setText(""+sum);
JOptionPane.showMessageDialog(null,"计算不正确!");
z=0;
tink5.setText(""+x);//此处标签反了,以后注意!!数据紧贴
tink6.setText(""+y);
}
b2.setVisible(false);
b1.setVisible(true);
checkers.stopAnimation(); //计时
checkers.jis=300;
checkers.ji.setText("300");


}

if (source == b1)
{


x=0;
y=0;
x = random.nextInt(100);
y = random.nextInt(100);
zz = random.nextInt(4);


if (zz==0)
{z=x+y;
tink2.setText("+");
tink5.setText(""+x);//此处标签反了,以后注意!!数据紧贴
tink6.setText(""+y);}
if (zz==1)
{ tink2.setText("-");
if (x<y)
{
tmp=y;
y=x;
x=tmp;
z=x-y;
tink5.setText(""+x);//此处标签反了,以后注意!!
tink6.setText(""+y);
x=0;
y=0;
}
if (x>=y)
{ z=x-y;
tink5.setText(""+x);//此处标签反了,以后注意!!
tink6.setText(""+y);
x=0;
y=0;
}


}
if (zz==2)
{z=x*y;
tink2.setText("*");
tink5.setText(""+x);//此处标签反了,以后注意!!
tink6.setText(""+y); }
x=0;
y=0;
if (zz==3)
{
if (y!=0)
{z=x%y;
//余数不为0的挑出
if (z==0)
{tink2.setText("/");
z=x/y;
tink5.setText(""+x);//此处标签反了,以后注意!!
tink6.setText(""+y);
x=0;
y=0;
}}}
b1.setVisible(false);
b2.setVisible(true);
checkers.playAnimation();
}


repaint();

}
class CheckersPanel extends JPanel implements Runnable {
ChangeTitle frame;
private Thread runner;
JLabel ji=new JLabel("300");
int jis=300;

void playAnimation() {
if (runner == null); {
runner = new Thread(this);
runner.start();
}
}

void stopAnimation() {
if (runner != null); {
runner = null;
}
}

public void run() {
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {
//repaint();

if (jis>=0)
jis-=1;
if (jis==0)
{
JOptionPane.showMessageDialog(null,"时间到!");
frame.sum=frame.sum-5;

jis=300;
stopAnimation();
}

ji.setText(""+jis);
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
}

}

不好使啊!?各位大虾,反这个错误:
java.lang.NullPointerException
at ChangeTitle$CheckersPanel.run(ChangeTitle.java:197)
at java.lang.Thread.run(Unknown Source)
急急急!谢谢

}
}

7.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-26 18:49

Please use [code ] tag and tell us which is line 197.

I think you haven't initialized the frame field of the CheckersPanel instance, have you?
I don't want to read the mess again, but I guess this's the problem -- you should set it to the ChangeTitle object, i.e. "this".

8.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: 笑_谈
Posted on: 2005-12-27 15:15

NG啊

9.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-27 21:50

笑_谈 wrote:
NG啊

請問NG是甚麼意思?

无意义的短句留言━━例如"頂"和"UP"都会被刪除哦!
如果要表达谢意,敬请认真一点、用心一点回应,单单一句"谢啦","Thanks" 或"3q"是不行的啊!

10.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: 笑_谈
Posted on: 2005-12-28 10:57

NG-no good. 用THIS指的是当前对象-CheckersPanel吧,它还是不存在SUM变量?

11.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-28 11:07

笑_谈 wrote:
NG-no good. 用THIS指的是当前对象-CheckersPanel吧,它还是不存在SUM变量?

Show us your modified code -- though I really don't want to read that.Sad
Please comment on the changes you made if possible.

I guess I could save some time to modify your code than to read your new one... so here's it is:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class ChangeTitle extends JFrame implements ActionListener {
  int x = 0;
  int y = 0;
  int z = 0;
  int zz = 0;
  int sum = 0;
  int yy = 0;
  int tmp = 0;
  Random random = new Random();

  CheckersPanel checkers = new CheckersPanel();

  // ??????
  JButton b1 = new JButton("New Q");
  JButton b2 = new JButton("Answer");
  JTextField names3 = new JTextField(3);
  JLabel tink5 = new JLabel("0");
  JLabel tink6 = new JLabel("0");
  JLabel tink8 = new JLabel("Your score");
  JLabel tink9 = new JLabel("0");
  JLabel tink1 = new JLabel("Question");
  JLabel tink2 = new JLabel("+");
  JLabel tink3 = new JLabel("=");
  JLabel tink10 = new JLabel("Time left");

  public ChangeTitle() {
    super("some name");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b1.addActionListener(this);
    b2.addActionListener(this);
    JPanel pane = new JPanel();
    pane.add(tink1);
    pane.add(tink5);
    pane.add(tink2);
    pane.add(tink6);

    pane.add(tink3);
    pane.add(names3);
    pane.add(b1);
    pane.add(b2);

    pane.add(tink8);
    pane.add(tink9);
    pane.add(tink10);
    pane.add(checkers.ji);
    setContentPane(pane);
    
    checkers.setFrame(this); // pass this to "checkers"
  }

  public static void main(String[] arg) {
    ChangeTitle frame = new ChangeTitle();

    frame.names3.setText("0");
    //frame.show();
    frame.pack();
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();

    if (source == b2) {

      yy = Integer.parseInt(names3.getText());
      // z=x+y;

      if (yy == z) {
        sum += 5;
        tink9.setText("" + sum);
        JOptionPane.showMessageDialog(null, "Correct");
        z = 0;
        tink5.setText("" + x);// ??????,??????????
        tink6.setText("" + y);
      } else {
        sum -= 5;
        tink9.setText("" + sum);
        JOptionPane.showMessageDialog(null, "Incorrect");
        z = 0;
        tink5.setText("" + x);// ??????,??????????
        tink6.setText("" + y);
      }
      b2.setVisible(false);
      b1.setVisible(true);
      checkers.stopAnimation(); // ??
      checkers.jis = 100;
      checkers.ji.setText("100");

    }

    if (source == b1) {

      x = 0;
      y = 0;
      x = random.nextInt(100);
      y = random.nextInt(100);
      zz = random.nextInt(4);

      if (zz == 0) {
        z = x + y;
        tink2.setText("+");
        tink5.setText("" + x);// ??????,??????????
        tink6.setText("" + y);
      }
      if (zz == 1) {
        tink2.setText("-");
        if (x < y) {
          tmp = y;
          y = x;
          x = tmp;
          z = x - y;
          tink5.setText("" + x);// ??????,??????
          tink6.setText("" + y);
          x = 0;
          y = 0;
        }
        if (x >= y) {
          z = x - y;
          tink5.setText("" + x);// ??????,??????
          tink6.setText("" + y);
          x = 0;
          y = 0;
        }

      }
      if (zz == 2) {
        z = x * y;
        tink2.setText("*");
        tink5.setText("" + x);// ??????,??????
        tink6.setText("" + y);
      }
      x = 0;
      y = 0;
      if (zz == 3) {
        if (y != 0) {
          z = x % y;
          // ????0???
          if (z == 0) {
            tink2.setText("/");
            z = x / y;
            tink5.setText("" + x);// ??????,??????
            tink6.setText("" + y);
            x = 0;
            y = 0;
          }
        }
      }
      b1.setVisible(false);
      b2.setVisible(true);
      checkers.playAnimation(this);
    }

    repaint();
  }

  class CheckersPanel extends JPanel implements Runnable {
    ChangeTitle frame;

    void setFrame(ChangeTitle frame) {
      this.frame = frame;
    }
    
    private Thread runner;

    JLabel ji = new JLabel("100");

    int jis = 100;

    void playAnimation(ChangeTitle frame) {
      this.frame = frame;
      if (runner == null)
      {
        runner = new Thread(this);
        runner.start();
      }
    }

    void stopAnimation() {
      if (runner != null)
      {
        runner = null;
      }
    }

    public void run() {
      Thread thisThread = Thread.currentThread();
      while (runner == thisThread) {
        // repaint();

        if (jis >= 0)
          jis -= 1;
        if (jis == 0) {
          // better put the following into a method in the outer class
// note that the only differences of 3 sections are the scores and message texts

          frame.sum = frame.sum - 5;
          frame.tink9.setText("" + frame.sum);
          JOptionPane.showMessageDialog(null, "Time's up");
          frame.b2.setVisible(false);
          frame.b1.setVisible(true);
          stopAnimation();
          jis = 100;
          ji.setText("100");
        }

        ji.setText("" + jis);
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
      }
    }

  }

}

12.Re:程序JAVA想实现到300减到0后,sum值-5 [Re: 笑_谈] Copy to clipboard
Posted by: why
Posted on: 2005-12-29 05:18

Actually, it is unnecessary to pass the ChangeTitle to CheckersPanel since CheckersPanel is an inner class of ChangeTitle.
The members of the outer class could be accessed directly, i.e.

      while (runner == thisThread) {
        if (jis >= 0)
          jis -= 1;
        if (jis == 0) {
          sum = sum - 5;
          tink9.setText("" + sum);
          JOptionPane.showMessageDialog(null, "Time's up");
          b2.setVisible(false);
          b1.setVisible(true);
          stopAnimation();
          jis = 100;
        }


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923