尝试为Minesweeper编写计时器时遇到问题。基本上,我有这个单独的计时器代码,在此代码中,我在构造函数外部声明了CountTimer ct,并且工作正常。但是,当我尝试在Minesweeper代码中执行相同的操作时,编译器说CountTimer无法解析为类型。有谁可以帮助我吗?
//////////This is my timer code.//////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CountTimerGUI2 extends JFrame implements ActionListener {
JPanel pan1 = new JPanel ();
JLabel timeLabel = new JLabel ();
JButton start = new JButton ("Start");
JButton stop = new JButton ("Stop");
CountTimer ct;
public CountTimerGUI2 () {
pan1.setLayout (new BorderLayout ());
timeLabel.setBorder (BorderFactory.createRaisedBevelBorder ());
pan1.add (timeLabel, BorderLayout.NORTH);
start.addActionListener (this);
stop.addActionListener (this);
JPanel pan2 = new JPanel ();
pan2.setLayout (new GridLayout ());
pan2.add (start);
pan2.add (stop);
pan1.add (pan2, BorderLayout.SOUTH);
setContentPane (pan1);
setVisible (true);
pack ();
ct = new CountTimer ();
}
private void setTimerText (String sTime) {
timeLabel.setText (sTime);
}
public void actionPerformed (ActionEvent e) {
JButton button = (JButton)e.getSource ();
if (button.equals (start)) {
ct.start ();
}
else if (button.equals (stop)) {
ct.stop ();
}
}
public static void main (String args []) {
java.awt.EventQueue.invokeLater (new Runnable () {
public void run () {
new CountTimerGUI2 ();
}
});
}
private class CountTimer implements ActionListener {
private static final int ONE_SECOND = 1000;
private int count = 0;
private boolean isTimerActive = false;
private Timer timer = new Timer (ONE_SECOND, this);
public CountTimer () {
count = 0;
setTimerText (TimeFormat (count));
}
public void actionPerformed (ActionEvent e) {
if (isTimerActive) {
count++;
setTimerText (TimeFormat (count));
}
}
public void start () {
count = 0;
isTimerActive = true;
timer.start ();
}
public void stop () {
timer.stop ();
}
}
private String TimeFormat (int count) {
int hours = count / 3600;
int minutes = (count - hours * 3600) / 60;
int seconds = count - minutes * 60;
return String.format ("%02d", hours) + " : " + String.format ("%02d", minutes) + " : " + String.format ("%02d", seconds);
}
}
//////////This is my Minesweeper code.//////////
import java.util.Scanner;
import java.util.Random;
//import java.util.*;
import java.awt.*;
import java.awt.event.*;
//import java.io.*;
//import java.io.FileWriter;
//import java.io.PrintWriter;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
public class Minesweeper extends JFrame implements ActionListener {
public static Scanner myScanner = new Scanner (System.in);
public static Random random = new Random ();
JPanel pan1 = new JPanel ();
JPanel pan2 = new JPanel ();
JPanel pan3 = new JPanel ();
JPanel pan1_1 = new JPanel ();
JPanel pan1_2 = new JPanel ();
JPanel pan1_3 = new JPanel ();
JPanel pan1_3_D = new JPanel ();
JButton button [][];
JButton Save = new JButton ("Save");
JButton Load = new JButton ("Load");
JButton New = new JButton ("New");
JButton One = new JButton ("1");
JButton Two = new JButton ("2");
JButton Three = new JButton ("3");
JButton R = new JButton ("R");
JLabel instruction = new JLabel ("Instruction Panel");//changeable
JLabel timeLabel = new JLabel ();
JLabel score = new JLabel ("Score");//it is only temporary
CountTimer ct;
public Minesweeper (int row, int column) {
button = new JButton [row][column];
GridLayout gl_pan1 = new GridLayout (1, 3);
GridLayout gl_pan2 = new GridLayout (row, column);
GridLayout gl = new GridLayout (2, 1);//gl is for the 3 sub-panels in pan1
GridLayout gl_pan1_3_D = new GridLayout (1, 4);
pan1.setLayout (gl_pan1);
pan2.setLayout (gl_pan2);
pan1_1.setLayout (gl);
pan1_2.setLayout (gl);
pan1_3.setLayout (gl);
pan1_3_D.setLayout (gl_pan1_3_D);
setTitle ("Minesweeper");
JPanel frameBorder = (JPanel)getContentPane ();
frameBorder.setLayout (new BoxLayout (getContentPane (), BoxLayout.Y_AXIS));
frameBorder.setBorder (new BevelBorder (BevelBorder.LOWERED));
pan1.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
pan2.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
pan3.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
Save.setMinimumSize (new Dimension (25, 50));
Save.setPreferredSize (new Dimension (25, 50));
Save.setMaximumSize (new Dimension (25, 50));
Load.setMinimumSize (new Dimension (25, 50));
Load.setPreferredSize (new Dimension (25, 50));
Load.setMaximumSize (new Dimension (25, 50));
New.setMinimumSize (new Dimension (25, 50));
New.setPreferredSize (new Dimension (25, 50));
New.setMaximumSize (new Dimension (25, 50));
for (int r = 0; r < row; r++) {
for (int c = 0; c < column; c++) {
button [r][c] = new JButton ();
button [r][c].setMinimumSize (new Dimension (20, 40));
button [r][c].setPreferredSize (new Dimension (20, 40));
button [r][c].setMaximumSize (new Dimension (20, 40));
pan2.add (button [r][c]);
//button [r][c].addActionListener (this);
}
}
pan1_1.add (Save);
pan1_1.add (Load);
pan1_2.add (timeLabel);
pan1_2.add (score);
pan1_3.add (New);
pan1_3_D.add (One);
pan1_3_D.add (Two);
pan1_3_D.add (Three);
pan1_3_D.add (R);
pan1_3.add (pan1_3_D);
/*Save.addActionListener (this);
Load.addActionListener (this);
New.addActionListener (this);
One.addActionListener (this);
Two.addActionListener (this);
Three.addActionListener (this);
R.addActionListener (this);*/
pan1.add (pan1_1);
pan1.add (pan1_2);
pan1.add (pan1_3);
pan3.add (instruction);
frameBorder.add (pan1);
frameBorder.add (pan2);
frameBorder.add (pan3);
setContentPane (frameBorder);
pack ();
setVisible (true);
}
public void actionPerformed (ActionEvent event) {
}
public static void main (String args []) throws Exception {
int row = 10;
int column = 10;
new Minesweeper (row, column);
int current_row = 5;
int current_col = 5;
int percentage = 10; //this is subject to change
int number_of_mines = (int)(row*column*percentage*0.01);
int mine_position [] = new int [number_of_mines];
boolean revealed [][] = new boolean [row][column]; //whether the button is revealed or not
boolean mines [][] = new boolean [row][column]; //whether there is a mine or not
int revealed_number [][] = new int [row][column]; //the number that should appear on the revealed button
int counter = 0; //this is for the gameEnd method
boolean end = false; //whether the game is ended or not
}
最佳答案
类的名称是CountTimerGUI2
,这也是创建该对象的实例的方式:
CountTimerGUI2 ctGUI;
这应该可以解决问题。
还要注意,在
CountTimerGUI2
中,您有一个私有类CountTimer
。如果您试图访问该内部对象,请在该类的声明之前删除private
类访问修饰符(这将使其具有程序包级访问权限)...class CountTimer implements ActionListener {
...
}
然后通过在两个文件的顶部添加
CountTimerGUI2
来在同一程序包中同时创建Minesweeper
和package package_name;
类。然后,您将可以访问
CountTimerGUI2
的ct对象:ctGUI.ct.start();
或在Minesweeper类中创建该内部类对象的实例:
import package_name.CountTimerGUI2.CountTimer;
...
CountTimer ct;
...
ct.start();