So here is what i have so far for my sudoku game. Now my grid only displays textboxes where a user can input numbers but nothing will happen yet. I want to know how to make it so that the user can only input one character and only numbers. i would also like to know how to make some textboxes jdialogs displaying uneditable numbers that im taking from a sudoku website for the puzzle. Any help would be appreciated. Thanks.
Main Class
Difficulty Select Classimport javax.swing.JOptionPane; public class Game{ public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello. Welcome to a game of Sudoku by Ezra Zike."); Level select = new Level(); } }
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Level extends JFrame { private JLabel tag1; private JButton butt1; private JButton butt2; private JButton butt3; private JButton butt4; public Level(){ super("Difficulty"); setLayout(new FlowLayout()); setSize(250,130); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tag1 = new JLabel("Please Select Your Difficulty."); add(tag1); butt1 = new JButton("Easy."); butt1.setToolTipText("For players new to Sudoku."); add(butt1); butt2 = new JButton("Medium."); butt2.setToolTipText("Your abilites will be tested."); add(butt2); butt3 = new JButton("Hard."); butt3.setToolTipText("Your skills will be strained."); add(butt3); butt4 = new JButton("Evil!"); butt4.setToolTipText("You will not survive."); add(butt4); thehandler handler = new thehandler(); butt1.addActionListener(handler); butt2.addActionListener(handler); butt3.addActionListener(handler); butt4.addActionListener(handler); } private class thehandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String string = ""; if(event.getSource()==butt1){ dispose(); string = "Have fun!"; SudokuPanel Squares = new SudokuPanel(); } else if(event.getSource()==butt2){ dispose(); string = "Good luck!"; SudokuPanel Squares = new SudokuPanel(); } else if(event.getSource()==butt3){ dispose(); string = "Try to keep your head from exploding..."; SudokuPanel Squares = new SudokuPanel(); } else if(event.getSource()==butt4){ dispose(); string = "Please refrain from smashing any keyboards."; SudokuPanel Squares = new SudokuPanel(); } JOptionPane.showMessageDialog(null, string); } } }
Sudoku Panel Class
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; public class SudokuPanel extends JFrame { public final int SQUARE_COUNT = 9; public Squares [] squares = new Squares[SQUARE_COUNT]; public SudokuPanel(){ super("Sudoku"); setSize(600,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(3,3)); for(int i=0; i<SQUARE_COUNT; i++){ squares[i] = new Squares(); panel.add(squares[i]); } JPanel panel2 = new JPanel(); JButton startTime = new JButton(); JButton stop = new JButton(); JButton resetTimer = new JButton(); MyTimer timer = new MyTimer(); startTime = new JButton("Start Timer"); stop = new JButton("Stop Timer"); final JLabel timerLabel = new JLabel(" ...Waiting... "); resetTimer = new JButton("Reset"); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("Menu"); menuBar.add(menu); JMenuItem newDifficulty = new JMenuItem("Select New Difficulty"); menu.add(newDifficulty); JMenuItem reset = new JMenuItem("Reset Puzzle"); menu.add(reset); JMenuItem exit = new JMenuItem("Exit"); menu.add(exit); exitaction e = new exitaction(); newDifficultyaction d = new newDifficultyaction(); resetaction f = new resetaction(); reset.addActionListener(f); exit.addActionListener(e); newDifficulty.addActionListener(d); panel2.add(timer); add(panel, BorderLayout.CENTER); add(panel2, BorderLayout.PAGE_END); setVisible(true); setLocationRelativeTo(null); } public class exitaction implements ActionListener{ public void actionPerformed (ActionEvent e){ System.exit(0); } } public class newDifficultyaction implements ActionListener{ public void actionPerformed (ActionEvent e){ dispose(); Level select = new Level(); } } public class resetaction implements ActionListener{ public void actionPerformed (ActionEvent e){ dispose(); SudokuPanel Squares = new SudokuPanel(); } } }
Squares/Cells class
Timer Classimport java.awt.*; import javax.swing.*; import javax.swing.border.*; public class Squares extends JPanel { public final int CELL_COUNT = 9; public Cell [] cells = new Cell[CELL_COUNT]; public Squares(){ this.setLayout(new GridLayout(3,3)); this.setBorder(new LineBorder(Color.BLACK,2)); for(int i =0; i<CELL_COUNT; i++){ cells[i] = new Cell(); this.add(cells[i]); } } public class Cell extends JTextField{ private int number; public Cell(){ } public void setNumber(int number){ this.number = number; this.setText("1"); } public int getNumber(){ return number; } } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyTimer extends Panel { private JLabel timeDisplay; private JButton resetButton; private JButton startButton; private JButton stopButton; Timer timer; public MyTimer(){ startButton = new JButton("Start Timer"); stopButton = new JButton("Stop Timer"); timeDisplay = new JLabel("...Waiting..."); this.add(startButton); this.add(stopButton); this.add(timeDisplay); event e = new event(); startButton.addActionListener(e); event1 c = new event1(); stopButton.addActionListener(c); }