-2

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 http://www.websudoku.com/ for the puzzle. Any help would be appreciated. Thanks.

Main Class

import javax.swing.JOptionPane; 

public class Game{ 

    public static void main(String[] args) { 

        JOptionPane.showMessageDialog(null, "Hello. Welcome to a game of Sudoku by #### ####."); 

        Level select = new Level();  

    }    
}

Difficulty Select Class

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

import 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;
        }

    }

}

Timer Class

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);



    }

    public class event implements ActionListener{
        public void actionPerformed(ActionEvent e){

            int count = 0;
            timeDisplay.setText("Elapsed Time in Seconds: " + count);

            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            timer.start();


        }
    }

    public class TimeClass implements ActionListener{
        int counter;

        public TimeClass(int counter){

            this.counter = counter;

        }

        public void actionPerformed(ActionEvent e){

            counter++;
            timeDisplay.setText("Elapsed Time in Seconds: " + counter);

        }
    }

    class event1 implements ActionListener{
        public void actionPerformed (ActionEvent c){

            timer.stop();

        }
    }
}
user3512387
  • 69
  • 1
  • 1
  • 6

1 Answers1

1

I want to know how to make it so that the user can only input one character and only numbers.

There are several ways, all easily discoverable by searching this site, as this problem is a common one. Two I'll mention: use a JFormattedTextField, or use a JTextField whose Document has a DocumentFilter added to it that prevents wrong input from being entered.

i would also like to know how to make some textboxes jdialogs displaying uneditable numbers that im taking from http://www.websudoku.com/ for the puzzle. Any help would be appreciated.

Several possible solutions to this one including

  • Set the component's enabled property to false.
  • Set the component's focusable property to false.

Some issues with your post:

  • You mention use of "textboxes". I recommend you avoid using non-Java non-Swing terms like "textboxes" as they only will serve to confuse. If you are using a specific type of text component and are asking about it, mention the component type by name as solutions may differ depending on the type.
  • You have posted a wall of code, most of it unrelated to your problem at hand. Please remember that we are volunteers, and please be kind to us. One way is to avoid posting too much unrelated code. Best would be to post a small minimal example program for questions that need this.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373