هذا هو كود ساعة الايقاف الذي أرسله الأخ حسام محمد في وقت سابق
ولدي بعض الأسئلة على هذا الكود ، قد يكون الاخ حسام مشغول ، لذلك أطلب من أي عضو لديه معرفة بكيفية عمل هذا الكود ، الرد على هذه الأسئلة
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
public class stopwatch extends JFrame implements Runnable
{
int second,minute,hour;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
Thread t=new Thread(this);
boolean Threadsuspend;
JTextField jTextField1 = new JTextField();
TitledBorder titledBorder1;
public void run()
{
while(true)
{
try
{
while(Threadsuspend)
{
synchronized(this)
{
wait();
}
}
second++;
Thread.sleep(1000);
jTextField1.setText(second+"");
}
catch(InterruptedException ee)
{}
if(second >59)
{second=0;
minute++;
jTextField2.setText(minute+"");
}
if(minute >59)
{minute=0;
hour++;
jTextField3.setText(hour+"");
}
}
}
public stopwatch() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
stopwatch stopwatch1 = new stopwatch();
stopwatch1.setSize(400,200);
stopwatch1.setVisible(true);
}
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("");
jButton1.setBackground(SystemColor.info);
jButton1.setBounds(new Rectangle(32, 112, 77, 40));
jButton1.setFont(new java.awt.Font("Serif", 1, 14));
jButton1.setForeground(Color.blue);
jButton1.setBorder(BorderFactory.createRaisedBevelBorder());
jButton1.setNextFocusableComponent(this);
jButton1.setText("play");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().setLayout(null);
jButton2.setText("stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton2.setBackground(SystemColor.info);
jButton2.setBounds(new Rectangle(245, 112, 77, 40));
jButton2.setFont(new java.awt.Font("Serif", 1, 14));
jButton2.setForeground(Color.blue);
jButton2.setBorder(BorderFactory.createRaisedBevelBorder());
jButton2.setNextFocusableComponent(this);
jButton3.setText("pause");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
jButton3.setBackground(SystemColor.info);
jButton3.setBounds(new Rectangle(135, 112, 77, 40));
jButton3.setFont(new java.awt.Font("Serif", 1, 14));
jButton3.setForeground(Color.blue);
jButton3.setBorder(BorderFactory.createRaisedBevelBorder());
jButton3.setNextFocusableComponent(this);
jTextField2.setBounds(new Rectangle(155, 33, 47, 33));
jTextField2.setBackground(SystemColor.activeCaption);
jTextField2.setFont(new java.awt.Font("Serif", 1, 16));
jTextField2.setForeground(Color.white);
jTextField2.setBorder(BorderFactory.createRaisedBevelBorder());
jTextField2.setCaretColor(Color.red);
jTextField2.setSelectionColor(UIManager.getColor("EditorPane.caretForeground"));
jTextField2.setText("0");
jTextField3.setBounds(new Rectangle(203, 33, 50, 33));
jTextField3.setBackground(SystemColor.activeCaption);
jTextField3.setFont(new java.awt.Font("Serif", 1, 16));
jTextField3.setForeground(Color.white);
jTextField3.setBorder(BorderFactory.createRaisedBevelBorder());
jTextField3.setCaretColor(Color.red);
jTextField3.setSelectionColor(UIManager.getColor("EditorPane.caretForeground"));
jTextField3.setText("0");
jTextField1.setBackground(SystemColor.activeCaption);
jTextField1.setFont(new java.awt.Font("Serif", 1, 16));
jTextField1.setForeground(Color.white);
jTextField1.setBorder(BorderFactory.createRaisedBevelBorder());
jTextField1.setCaretColor(Color.red);
jTextField1.setFocusAccelerator('1');
jTextField1.setSelectionColor(UIManager.getColor("EditorPane.caretForeground"));
jTextField1.setText("0");
jTextField1.setBounds(new Rectangle(114, 33, 41, 33));
this.getContentPane().setBackground(new Color(98, 144, 177));
this.setCursor(null);
this.setResizable(false);
this.setTitle("StopWatch");
this.getContentPane().add(jTextField2, null);
this.getContentPane().add(jTextField3, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton3, null);
}
synchronized void jButton1_actionPerformed(ActionEvent e) {
jTextField3.setText("0");
jTextField2.setText("0");
jTextField1.setText("0");
second=0;
minute=0;
hour=0;
Threadsuspend=false;
t.start();
notify();
}
synchronized void jButton3_actionPerformed(ActionEvent e) {
Threadsuspend=!Threadsuspend;
if(!Threadsuspend)
{
notify();
}
}
synchronized void jButton2_actionPerformed(ActionEvent e) {
notify();
t.stop();
t=new Thread(this);
notify();
}
}
أنا الآن اشتغل الكود لدي بشكل صحيح وعندما أضغط على الزر play تعمل الساعة
الآن أنا في مكان آخر من البرنامج أكتب الجملة التالية
if ( stopwatch1.second==5) do something ; else if(stopwatch1.second==0) do anathor thing ;
ودائما يظهر لي أن الsecond==0 مع اني أرى قيمة ال second تتغير على الشاشة ، وهذا يعني أن القيمة التي أختبرها ليست هي القيمة المطبوعة على الشاشة .
السؤال هو كيف أصل الى قيمة second المطبوعة على الشاشة .
أرجو ان يكون السؤال واضحا ..................
يا أخوان اذا استطعنا حل هذه المشكلة سأستطيع حل مشاكل أخرى كثيرة
أرجوكم لاتبخلوا بعلمكم .......
وفق الله الجميع لما يحبه ويرضاه ............................