这里是SudokuMain类的第一部分(我不小心把“SudokuMain2”而不是“SudokuMain”在MenuAtTop,所以忽略了2 ):
// Allow short name access to following classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class SudokuMain extends JComponent {
/**
* The application method.
*
* @param args The command-line arguments.
*/
public static void main(String[] args) {
new SudokuMain();
}
// this field refers to the SudokuBase class to access information
// of the board.
private SudokuBase board;
// this field refers to SudokuView object to access its information
// and provide output
private SudokuView view;
// the window all the components are contained in
private JFrame win;
// center JPanel object in window
private JPanel center;
// left JPanel object in window
private JPanel west;
// right JPanel object in window
private JPanel east;
// bottom JPanel object in window
private JPanel south;
// JPanel object to hold graphic "buttons"
private JPanel symbols;
// JPanel object to show "states" of Sudoku board
private JPanel rowColRegStates;
// the first set-up window (cannot be changed once
// instantiated)
private final Dialog1 setWin1;
/**
* Constructs SudokuMain object.
*/
public SudokuMain() {
// start game
board = makeBoard();
view = new SudokuView(board);
win = new JFrame("Sudoku Game");
center = new JPanel();
west = new JPanel();
east = new JPanel();
south = new JPanel();
// graphic "buttons" for current Sudoku board
symbols = new SetSymbols(view);
// "states" of current Sudoku board
rowColRegStates = new ShowStates(view);
// the first set-up window
setWin1 = new Dialog1(this, "New Game", true);
// create menu bar
final MenuAtTop menuBar = new MenuAtTop(this);
win.setJMenuBar(menuBar);
// display game mode
JLabel mode = new JLabel("Normal Play Mode");
mode.setHorizontalAlignment(JLabel.CENTER);
Font modeFont = new Font("Arial", Font.BOLD, 14);
mode.setFont(modeFont);
// // set selected cell at (0, 0)
// view.setSelected(0, 0);
// add window focus listener
win.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
win.pack();
// set selected cell at (0, 0)
view.setSelected(0, 0);
}
public void windowLostFocus(WindowEvent e) {
}
});
// add window listener
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
menuBar.closePrompt();
}
});
win.setLayout(new BorderLayout());
west.setLayout(new BorderLayout());
east.setLayout(new BorderLayout());
center.setLayout(new FlowLayout());
west.add(symbols);
east.add(view, BorderLayout.CENTER);
south.add(rowColRegStates);
center.add(west);
center.add(east);
win.add(south, BorderLayout.NORTH);
win.add(center, BorderLayout.CENTER);
win.add(mode, BorderLayout.SOUTH);
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
win.pack();
win.setVisible(true);
}
// this inner class constructs graphic "buttons" to set desired
// cells of board
class SudokuControlButton extends JPanel {
// row of selected cell
private int selRow;
// column of selected cell
private int selCol;
// the value that corresponds with the desired symbol
private int value;
/**
* Constructs SudokuControlButton object; the graphic "button"
* to control the board.
*
* @param view The SudokuView object to be controlled.
* @param v The value that corresponds to the desired symbol.
*/
public SudokuControlButton(final SudokuView view, int v) {
// set characteristics of graphic "button"
setPreferredSize(new Dimension(50, 50));
setBackground(Color.LIGHT_GRAY);
value = v;
addMouseListener(new MouseListener() {
/**
* This method selects a "button" and puts it in focus when the mouse
* is clicked on it.
*
* @param event Captures information on the mouse button being
* clicked (pressed and released) on a component.
*/
public void mouseClicked(MouseEvent e) {
selRow = view.getSelectedRow();
selCol = view.getSelectedColumn();
if(!board.isGiven(selRow, selCol)) {
board.setValue(selRow, selCol, value);
view.new SudokuCell(selRow, selCol, board);
// set to "highlighted" color
setBackground(Color.WHITE);
view.repaint();
}
else { // have system beep sound
getToolkit().beep();
}
repaint();
}
/**
* This method handles behavior when the mouse enters a graphic
* "button".
*
* @param event Captures information on the mouse button being
* entered over a component.
*/
public void mouseEntered(MouseEvent e){
// set to "highlighted" color
setBackground(Color.WHITE);
repaint();
}
/**
* This method handles behavior when the mouse exits a graphic
* "button".
*
* @param event Captures information on the mouse button being
* exited from a component.
*/
public void mouseExited(MouseEvent e){
// set to default color
SudokuControlButton button = (SudokuControlButton) e.getSource();
setBackground(Color.LIGHT_GRAY);
repaint();
}
/**
* This method handles behavior when the mouse is pressed on a
* graphic "button".
*
* @param event Captures information on the mouse button being
* pressed on a component.
*/
public void mousePressed(MouseEvent e){
// set to "active" color
setBackground(Color.YELLOW);
repaint();
}
/**
* This method handles behavior when the mouse is released on a
* graphic "button".
*
* @param e Captures information on the mouse button being
* released on a component.
*/
public void mouseReleased(MouseEvent e){
}
});
}
/**
* This method draws the graphic "button" associated with
* each numeric value, 0 to 12.
*
* @param g The drawing mechanism.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
switch(value) {
case 0:
drawSymbol(g, 0);
break;
case 1:
drawSymbol(g, 1);
break;
case 2:
drawSymbol(g, 2);
break;
case 3:
drawSymbol(g, 3);
break;
case 4:
drawSymbol(g, 4);
break;
case 5:
drawSymbol(g, 5);
break;
case 6:
drawSymbol(g, 6);
break;
case 7:
drawSymbol(g, 7);
break;
case 8:
drawSymbol(g, 8);
break;
case 9:
drawSymbol(g, 9);
break;
case 10:
drawSymbol(g, 10);
break;
case 11:
drawSymbol(g, 11);
break;
case 12:
drawSymbol(g, 12);
break;
}
}
/**
* This method draws the symbol that corresponds with
* the specified value (0-12).
*
* @param g The drawing mechanism.
* @param value The specified value.
*/
public void drawSymbol(Graphics g, int value) {
if(value < 0 || value > 12) {
String msg = "Value cannot be less than 0 or greater than 12.";
throw new IllegalArgumentException(msg);
}
// enable drawing with "thick" lines
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
switch(value) {
case 0:
// draw borders
g.drawRect(0, 0, 50, 50);
break;
case 1:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
break;
case 2:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
g2.drawLine(10, 5, 10, 45);
break;
case 3:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
g2.drawLine(10, 5, 10, 45);
g2.drawLine(15, 5, 15, 45);
break;
case 4:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
g2.drawLine(10, 5, 10, 45);
g2.drawLine(15, 5, 15, 45);
g2.drawLine(20, 5, 20, 45);
break;
case 5:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
g2.drawLine(10, 5, 10, 45);
g2.drawLine(15, 5, 15, 45);
g2.drawLine(20, 5, 20, 45);
g2.drawLine(25, 5, 25, 45);
break;
case 6:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 45);
g2.drawLine(10, 5, 10, 45);
g2.drawLine(15, 5, 15, 45);
g2.drawLine(20, 5, 20, 45);
g2.drawLine(25, 5, 25, 45);
g2.drawLine(30, 5, 30, 45);
break;
case 7:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 20);
g2.drawLine(10, 5, 10, 20);
g2.drawLine(15, 5, 15, 20);
g2.drawLine(20, 5, 20, 20);
g2.drawLine(25, 5, 25, 20);
g2.drawLine(30, 5, 30, 20);
g2.drawLine(5, 30, 5, 45);
break;
case 8:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 20);
g2.drawLine(10, 5, 10, 20);
g2.drawLine(15, 5, 15, 20);
g2.drawLine(20, 5, 20, 20);
g2.drawLine(25, 5, 25, 20);
g2.drawLine(30, 5, 30, 20);
g2.drawLine(5, 30, 5, 45);
g2.drawLine(10, 30, 10, 45);
break;
case 9:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g2.drawLine(5, 5, 5, 20);
g2.drawLine(10, 5, 10, 20);
g2.drawLine(15, 5, 15, 20);
g2.drawLine(20, 5, 20, 20);
g2.drawLine(25, 5, 25, 20);
g2.drawLine(30, 5, 30, 20);
g2.drawLine(5, 30, 5, 45);
g2.drawLine(10, 30, 10, 45);
g2.drawLine(15, 30, 15, 45);
break;
case 10:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g.drawLine(5, 5, 5, 20);
g.drawLine(10, 5, 10, 20);
g.drawLine(15, 5, 15, 20);
g.drawLine(20, 5, 20, 20);
g.drawLine(25, 5, 25, 20);
g.drawLine(30, 5, 30, 20);
g.drawLine(5, 30, 5, 45);
g.drawLine(10, 30, 10, 45);
g.drawLine(15, 30, 15, 45);
g.drawLine(20, 30, 20, 45);
break;
case 11:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g.drawLine(5, 5, 5, 20);
g.drawLine(10, 5, 10, 20);
g.drawLine(15, 5, 15, 20);
g.drawLine(20, 5, 20, 20);
g.drawLine(25, 5, 25, 20);
g.drawLine(30, 5, 30, 20);
g.drawLine(5, 30, 5, 45);
g.drawLine(10, 30, 10, 45);
g.drawLine(15, 30, 15, 45);
g.drawLine(20, 30, 20, 45);
g.drawLine(25, 30, 25, 45);
break;
case 12:
// draw borders
g.drawRect(0, 0, 50, 50);
// draw symbol
g.drawLine(5, 5, 5, 20);
g.drawLine(10, 5, 10, 20);
g.drawLine(15, 5, 15, 20);
g.drawLine(20, 5, 20, 20);
g.drawLine(25, 5, 25, 20);
g.drawLine(30, 5, 30, 20);
g.drawLine(5, 30, 5, 45);
g.drawLine(10, 30, 10, 45);
g.drawLine(15, 30, 15, 45);
g.drawLine(20, 30, 20, 45);
g.drawLine(25, 30, 25, 45);
g.drawLine(30, 30, 30, 45);
break;
}
}
}
// this inner class provides a JMenuBar object at the top of
// the board
class MenuAtTop extends JMenuBar implements ActionListener{
// SudokuMain object we are dealing with
private SudokuMain main;
// the "File" menu
private JMenu fileMenu;
// the "New Game" option
private JMenuItem newGame;
// the "Open" option
private JMenuItem open;
// the "Save" option
private JMenuItem save;
// the "Save As" option
private JMenuItem saveAs;
// the "Reset" option
private JMenuItem reset;
// the "Quit" option
private JMenuItem quit;
// the ability to choose files
private JFileChooser choose;
// the saved file
// // compiler would not allow "static" keyword
private File fileSaved = null;
private Object opener;
// JDialog object to create a dialog box to prompt
// user for new game information
private JDialog createNewWin;
/**
* Constructs MenuAtTop object.
*
* @param m The SudokuMain object to be referred to.
*/
public MenuAtTop(final SudokuMain m) {
main = m;
opener = null;
choose = new JFileChooser();
// instantiate and bind to reference
fileMenu = new JMenu("File");
add(fileMenu);
// instantiate and bind to reference
newGame = new JMenuItem("New Game");
newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.CTRL_MASK));
fileMenu.add(newGame);
newGame.addActionListener(this);
open = new JMenuItem("Open");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
ActionEvent.CTRL_MASK));
fileMenu.add(open);
// add action listener to "Open" option
open.addActionListener(this);
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
ActionEvent.CTRL_MASK));
fileMenu.add(save);
// add action listener to "Save" option
save.addActionListener(this);
saveAs = new JMenuItem("Save As");
saveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
ActionEvent.CTRL_MASK));
fileMenu.add(saveAs);
// add action listener to "Save As" option
saveAs.addActionListener(this);
reset = new JMenuItem("Reset");
reset.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
ActionEvent.CTRL_MASK));
fileMenu.add(reset);
// add action listener to "Reset" option
reset.addActionListener(this);
quit = new JMenuItem("Quit");
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
ActionEvent.CTRL_MASK));
fileMenu.add(quit);
// add action listener to "Quit" option
quit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(newGame)) {
setEnabled(false);
// create dialog box prompting for the new board information
createNewWin = new Dialog1(main, "Create New Board", true);
// make it visible
createNewWin.setVisible(true);
fileSaved = null;
} else if(e.getSource().equals(open)) {
int returnVal = choose.showOpenDialog(main.win);
if(returnVal == JFileChooser.APPROVE_OPTION) {
boolean error = false;
File openFile = choose.getSelectedFile();
try {
FileInputStream fin = new FileInputStream(openFile);
ObjectInputStream ois = new ObjectInputStream(fin);
opener = ois.readObject();
} catch (Exception exc) {
JOptionPane.showMessageDialog(main.win, "Error opening file.");
error = true;
}
// "opener" reads something and it is of type SudokuBase
if(opener != null && opener instanceof SudokuBase){
main.west.remove(main.symbols);
main.east.remove(main.view);
main.south.remove(main.rowColRegStates);
main.view = new SudokuView((SudokuBase) opener);
main.symbols = new SetSymbols(main.view);
main.rowColRegStates = new ShowStates(main.view);
main.west.add(main.symbols);
main.east.add(main.view);
main.south.add(main.rowColRegStates);
main.win.requestFocus();
fileSaved = openFile;
} else {
if(error) {
JOptionPane.showMessageDialog(main.win, "Incorrect file type.");
}
}
}
} else if(e.getSource().equals(save)) {
if(fileSaved == null) {
saveAsPrompt();
} else {
try {
board.writeToStream(new FileOutputStream(fileSaved));
} catch (Exception exc) {
JOptionPane.showMessageDialog(main.win, "Error saving file.");
}
}
} else if(e.getSource().equals(saveAs)) {
saveAsPrompt();
} else if(e.getSource().equals(reset)) {
int n = JOptionPane.showConfirmDialog(main.win,
"Any player values will" +
" be lost. Proceed?",
"Warning!", 2);
if(n == JOptionPane.OK_OPTION) {
main.board.reset();
main.view.repaint();
}
} else if(e.getSource().equals(quit)) {
closePrompt();
}
}
// This method prompts the user to choose a file to save to,
// and then saves the file.
private int saveAsPrompt() {
boolean saveError;
int rtn = choose.showSaveDialog(main.win);
if(rtn == JFileChooser.APPROVE_OPTION) {
saveError = false;
File fileSaveAs = choose.getSelectedFile();
try {
board.writeToStream(new FileOutputStream(fileSaveAs));
} catch (Exception e) {
JOptionPane.showMessageDialog(main.win, "Error saving file.");
saveError = true;
}
if(!saveError) {
fileSaved = fileSaveAs;
}
}
return rtn;
}
// This method prompts the user whether they want to save before
// closing, only if changes occurred.
private void closePrompt() {
if(true) {
int n = JOptionPane.showConfirmDialog(main.win, "Save game?");
if(n == JOptionPane.YES_OPTION) {
int saved = saveAsPrompt();
if(saved != JFileChooser.CANCEL_OPTION){
main.win.dispose();
}
} else if(n == JOptionPane.NO_OPTION) {
main.win.dispose();
}
}
else { // no changes were made
main.win.dispose();
}
}
}
...
什么是你的错误? – tbodt
*“我得到一个错误。”*您打算与我们分享错误,还是应该猜测?好吧,对于'TooManyKittensError',请从隔壁的tomcat中取出。 –
包含要保存的信息的对象是类“SudokuMain2”的一个实例?向我们展示该课程... –