📄 sudokudemo.java
字号:
import java.awt.*; import java.awt.event.*;import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import CLIPSJNI.*;/* TBD Allow tabbing between different grids. *//* TBD Allow arrow keys to move between different grids. *//* TBD Web links for techniques. */class SudokuDemo implements ActionListener, FocusListener, KeyListener { JFrame jfrm; JPanel mainGrid; JButton clearButton; JButton resetButton; JButton solveButton; JButton techniquesButton; Object resetValues[][][] = new Object[9][3][3]; boolean solved = false; Environment clips; /**************/ /* SudokuDemo */ /**************/ SudokuDemo() { JTable theSubGrid; int r, c; /*===================================*/ /* Create the main JFrame container. */ /*===================================*/ jfrm = new JFrame("Sudoku Demo"); jfrm.getContentPane().setLayout(new FlowLayout()); jfrm.setSize(350,285); /*=============================================================*/ /* Terminate the program when the user closes the application. */ /*=============================================================*/ jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*=======================================================*/ /* Create the JPanel which will contain the sudoku grid. */ /*=======================================================*/ mainGrid = new JPanel(); GridLayout theLayout = new GridLayout(3,3); theLayout.setHgap(-1); theLayout.setVgap(-1); mainGrid.setLayout(theLayout); mainGrid.setOpaque(true); /*=================================================*/ /* Create a renderer based on the default renderer */ /* that will center the text within the cell. */ /*=================================================*/ DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() { public Component getTableCellRendererComponent( JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column); if (comp instanceof JLabel) { ((JLabel) comp).setHorizontalAlignment(JLabel.CENTER); if (value instanceof String) { if ("?".equals(value)) { ((JLabel) comp).setForeground(Color.red); } else if (((String) value).length() > 1) { ((JLabel) comp).setForeground(Color.green.darker()); } else { ((JLabel) comp).setForeground(Color.black); } } } return comp; } }; /*========================================*/ /* Create each of the nine 3x3 grids that */ /* will go inside the main sudoku grid. */ /*========================================*/ for (r = 0; r < 3; r++) { for (c = 0; c < 3; c++) { theSubGrid = new JTable(3,3) { public boolean isCellEditable(int rowIndex,int vColIndex) { return false; } }; theSubGrid.setRowSelectionAllowed(false); theSubGrid.setShowGrid(true); theSubGrid.setRowHeight(25); theSubGrid.setGridColor(Color.black); theSubGrid.setBorder(BorderFactory.createLineBorder(Color.black,2)); theSubGrid.setDefaultRenderer(Object.class,renderer); theSubGrid.addFocusListener(this); theSubGrid.addKeyListener(this); TableColumn column = null; for (int i = 0; i < 3; i++) { column = theSubGrid.getColumnModel().getColumn(i); column.setMaxWidth(25); } mainGrid.add(theSubGrid); } } /*========================================*/ /* Set up the panel containing the Clear, */ /* Reset, Solve, and Techniques buttons. */ /*========================================*/ JPanel buttonGrid = new JPanel(); theLayout = new GridLayout(4,1); buttonGrid.setLayout(theLayout); buttonGrid.setOpaque(true); clearButton = new JButton("Clear"); buttonGrid.add(clearButton); clearButton.addActionListener(this); clearButton.setToolTipText("Clear the sudoku grid."); resetButton = new JButton("Reset"); resetButton.setEnabled(false); buttonGrid.add(resetButton); resetButton.addActionListener(this); resetButton.setToolTipText("Reset to state before last solution."); solveButton = new JButton("Solve"); buttonGrid.add(solveButton); solveButton.addActionListener(this); solveButton.setToolTipText("Solve the puzzle."); techniquesButton = new JButton("Techniques"); techniquesButton.setEnabled(false); buttonGrid.add(techniquesButton); techniquesButton.addActionListener(this); techniquesButton.setToolTipText("Show techniques used to solve puzzle."); /*=============================================*/ /* Add the grid and button panels to the pane. */ /*=============================================*/ jfrm.getContentPane().add(mainGrid); jfrm.getContentPane().add(buttonGrid); JLabel instructions = new JLabel("<html><p style=\"font-size:95%\">Select cell and enter digit 1-9 or press backspace/delete.</p>"); jfrm.getContentPane().add(instructions); /*==========================*/ /* Load the sudoku program. */ /*==========================*/ clips = new Environment(); clips.load("sudoku.clp"); clips.load("solve.clp"); /**********************/ /* Display the frame. */ /**********************/ jfrm.setVisible(true); } /********/ /* main */ /********/ public static void main( String args[]) { /*===================================================*/ /* Create the frame on the event dispatching thread. */ /*===================================================*/ SwingUtilities.invokeLater( new Runnable() { public void run() { new SudokuDemo(); } }); } /*########################*/ /* ActionListener Methods */ /*########################*/ /*******************/ /* actionPerformed */ /*******************/ public void actionPerformed( ActionEvent ae) { /*==========================*/ /* Handle the Clear button. */ /*==========================*/ if (ae.getActionCommand().equals("Clear")) { solved = false; solveButton.setEnabled(true); techniquesButton.setEnabled(false); for (int i = 0; i < 9; i++) { JTable theTable = (JTable) mainGrid.getComponent(i); for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { theTable.setValueAt("",r,c); } } } } /*==========================*/ /* Handle the Reset button. */ /*==========================*/ else if (ae.getActionCommand().equals("Reset")) { solved = false; solveButton.setEnabled(true); techniquesButton.setEnabled(false); for (int i = 0; i < 9; i++) { JTable theTable = (JTable) mainGrid.getComponent(i); for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { theTable.setValueAt(resetValues[i][r][c],r,c); } } } } /*==========================*/ /* Handle the Solve button. */ /*==========================*/ else if (ae.getActionCommand().equals("Solve")) { /*==============*/ /* Reset CLIPS. */ /*==============*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -