2017-11-15 6 views
-1

静的ストップウォッチを作成するためにスイングライブラリを使用する非常に簡単で小さなJavaプログラムがあります。問題は、 "javac program.java"でコマンドラインでコンパイルしようとするたびに、オブジェクトボタンに関連するステートメントがコンパイラによって認識されなかったような一連のエラーが出るようです。JavaのSwing API実装、オブジェクトJButtonのインスタンスが認識されない


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

class StopWatch implements ActionListener { 

    JLabel jlab; 
    long start; // holds the start time in milliseconds 

    StopWatch() { 
     // Create a new JFrame Container. 
     JFrame jfrm = new JFrame("A Simple StopWatch"); 

     // Specify the FlowLayout for the layout manager. 
     jfrm.setLayout(new FlowLayout()); 

     // Give the frame an initial size. 
     jfrm.setSize(230,90); 

     // Terminates the program when the user closes the application. 
     jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Make two buttons 
     JButton JbtnStart = new JButton("Start"); 
     JButton JbtnStop = new JButton("Stop"); 

     // Add action listeners. 
     jbtnStart.addActionListener(this); 
     jbtnStop.addActionListener(this); 

     // Add the buttons to the content pane 
     jfrm.add(jbtnStart); 
     jfrm.add(jbtnStop); 

     // Create a text-based label 
     jlab = new JLabel("Press Start to begin timing."); 

     // Add the label 
     jfrm.add(jlab); 

     jfrm.setVisible(true); 

    } 

    // Handle button events 
    public void actionPerformed(ActionEvent ae) { 
     Calendar cal = Calendar.getInstance(); // get current system time 

     if (ae.getActionCommand().equals("Start")) { 
      start = cal.getTimeInMillis(); 
      jlab.setText("Stopwatch is Running..."); 
     } 

     else 
      // Compute the elapsed time. 
      jlab.setText("Elapsed time is "+(double)(cal.getTimeInMillis() - start)/1000); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { new StopWatch(); } 
     }); 
    } 

} 

次のエラーメッセージです:

StopWatch.java:30: error: cannot find symbol 
jbtnStart.addActionListener(this); 
     ^
Symbol: variable jbtnStart 
location: class StopWatch 
StopWatch.java:31: error: cannot find symbol 
jbtnStop.addActionListener(this); 
     ^
symbol: variable jbtnStop 
location: class StopWatch 
StopWatch.java:34: error: cannot find symbol 
jfrm.add(jbtnStart); 
       ^
symbol: variable jbtnStart 
location: class StopWatch 
StopWatch.java:35: error: cannot find symbol 
jfrm.add(jbtnStop); 
        ^
symbol: variable jbtnStop 
location: class StopWatch 
4 errors 

うjbtnStopとjbtnStartは、Java言語の単語を予約することが?

+0

'JButton JbtnStart'そして' jbtnStart'を使用しようとしています... – Shark

+1

'JbtnStart!= jbtnStart'は、大文字で始まり、もう一つは大文字で始まり、Javaは大文字小文字を区別する言語です。 'jbtnStart'は宣言されていません。 –

答えて

1

に名前を付けるには注意する必要がありますあなたのコードに変更を加えましたJbtnStartjbtnStartJbtnStopjbtnStop

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

class StopWatch implements ActionListener { 

    JLabel jlab; 
    long start; // holds the start time in milliseconds 

    StopWatch() { 
     // Create a new JFrame Container. 
     JFrame jfrm = new JFrame("A Simple StopWatch"); 

     // Specify the FlowLayout for the layout manager. 
     jfrm.setLayout(new FlowLayout()); 

     // Give the frame an initial size. 
     jfrm.setSize(230,90); 

     // Terminates the program when the user closes the application. 
     jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Make two buttons 
     JButton jbtnStart = new JButton("Start"); 
     JButton jbtnStop = new JButton("Stop"); 

     // Add action listeners. 
     jbtnStart.addActionListener(this); 
     jbtnStop.addActionListener(this); 

     // Add the buttons to the content pane 
     jfrm.add(jbtnStart); 
     jfrm.add(jbtnStop); 

     // Create a text-based label 
     jlab = new JLabel("Press Start to begin timing."); 

     // Add the label 
     jfrm.add(jlab); 

     jfrm.setVisible(true); 

    } 

    // Handle button events 
    public void actionPerformed(ActionEvent ae) { 
     Calendar cal = Calendar.getInstance(); // get current system time 

     if (ae.getActionCommand().equals("Start")) { 
      start = cal.getTimeInMillis(); 
      jlab.setText("Stopwatch is Running..."); 
     } 

     else 
      // Compute the elapsed time. 
      jlab.setText("Elapsed time is "+(double)(cal.getTimeInMillis() - start)/1000); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { new StopWatch(); } 
     }); 
    } 

} 
+0

ありがとう、私は今の場合の不一致のために明白なエラーを見ることができます。 – tk3000

+0

@ user3719700あなたがあなたの答えを親切に回答していただければ、ありがとうございます。プラス私はネット豆、日食、intellijアイデアなど(初心者ではない場合)のようなIDEを使用することをお勧めします –

1

は、これらの変数名を修正:

JButton JbtnStart = new JButton("Start"); 
JButton JbtnStop = new JButton("Stop"); 

彼らは小さな文字から始める必要があります。

JButton jbtnStart = new JButton("Start"); 
JButton jbtnStop = new JButton("Stop"); 

Javaは大文字と小文字を区別する言語であるので、あなたは私

+0

あなたの答えをありがとう。それは愚かな間違いでした。私は代わりにIDEを使うべきです – tk3000

0
JbtnStart != jbtnStart 

Javaは大文字と小文字が区別言語です。

+0

私は彼らが平等ではないことを実現します。スクリーン上の大文字と小文字の違いを気付かなかっただけです。 – tk3000

関連する問題