静的ストップウォッチを作成するためにスイングライブラリを使用する非常に簡単で小さな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言語の単語を予約することが?
'JButton JbtnStart'そして' jbtnStart'を使用しようとしています... – Shark
'JbtnStart!= jbtnStart'は、大文字で始まり、もう一つは大文字で始まり、Javaは大文字小文字を区別する言語です。 'jbtnStart'は宣言されていません。 –