Question

Please fix and test my code so that all the buttons and function are working in...

Please fix and test my code so that all the buttons and function are working in the Sudoku.


CODE:

SudokuLayout.java:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class SudokuLayout extends JFrame{
JTextArea txtArea;//for output
JPanel gridPanel,butPanel;//panels for sudoku bord and buttons
JButton hint,reset,solve,newPuzzel;//declare buttons
JComboBox difficultyBox;
SudokuLayout()
{
setName("Sudoku Bord");//set name of frame
// setTitle("Sudoku Bord");//set title for sudoku frame
setLayout(new BorderLayout());//set border layout to the Sudoku frame
  
  
  
//create grid layout to the Sudoku board
GridLayout gbag=new GridLayout(3,9);
gridPanel=new JPanel(gbag);
  
for (int k = 1; k <= 9; k++)//it repesents the nine 3x3 panels
{
JPanel level2 = new JPanel(new GridLayout(3,3));//create 3x3 panels

for (int i = 1; i <= 9; i++)
{
JTextField text = new JTextField(" "); //create text feilds
level2.add(text);
}
level2.setBorder(BorderFactory.createBevelBorder(1));
level2.setBackground(Color.BLACK);
gridPanel.add(level2);//add 3x3 panel in 3x9 panel
}
  
add(gridPanel, BorderLayout.WEST);
  
//create control buttons
hint=new JButton("Hint");
reset=new JButton("Reset");
solve=new JButton("Solve");
newPuzzel=new JButton("New Puzzel");
//create panel for buttons
butPanel=new JPanel();
butPanel.setLayout(new BoxLayout(butPanel,BoxLayout.Y_AXIS));
butPanel.add(Box.createRigidArea(new Dimension(0,15)));//add empty space between components
butPanel.add(reset);
butPanel.add(Box.createRigidArea(new Dimension(0,15)));//add empty space between components
butPanel.add(hint);
butPanel.add(Box.createRigidArea(new Dimension(0,15)));//add empty space between components
butPanel.add(solve);
butPanel.add(Box.createRigidArea(new Dimension(0,15)));//add empty space between components
butPanel.add(newPuzzel);
butPanel.add(Box.createRigidArea(new Dimension(0,15)));//add empty space between components
butPanel.add(Box.createRigidArea(new Dimension(0,15))); //add empty space between components
  
String[] difficulties = { "Easy", "Medium", "Hard"};
//Create the combo box, select item at index 1.
//Indices start at 0, so 1 specifies Medium.
difficultyBox = new JComboBox(difficulties);
difficultyBox.setSelectedIndex(2);
butPanel.add(difficultyBox);
//add buttons panel on sudoku panel
add(butPanel,BorderLayout.EAST);
  
//add action listeners
reset.addActionListener(new ButtonsAction());
hint.addActionListener(new ButtonsAction());
solve.addActionListener(new ButtonsAction());
newPuzzel.addActionListener(new ButtonsAction());
  
  
  
//create text area for result
txtArea = new JTextArea(4, 20);
txtArea.setBorder(BorderFactory.createTitledBorder("output"));
txtArea.setText("Hello world!");

add(txtArea, BorderLayout.SOUTH);
}

  
  
private class ButtonsAction implements ActionListener{ //private listener class   

@Override
public void actionPerformed(ActionEvent e) {//implements actionPerformed
Object button=e.getSource();
if(button.equals(hint))//check source onject is hint button
{
txtArea.setText("Hint Button clicked!..");
}
else if(button.equals(reset))//else check source onject is reset button
{
txtArea.setText("Hello world!");
}
else if(button.equals(solve))//else check source onject is solve button
{
txtArea.setText("Solve button clicked!");
}
else
{//other wise it was new Puzzel button
txtArea.setText("New Puzzel button clicked!");
}
}
}
  
}

TestSudoku.java:

import java.awt.Color;
import javax.swing.*;

public class TestSudoku
{
public static void main(String args[])
{
SudokuLayout frame=new SudokuLayout();//create oject to SudokuLayout class
frame.setUndecorated(true); // Remove title bar
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setSize(450,350);//set size
//frame.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));//it sets the border to frame
frame.setResizable(false);
frame.setVisible(true);
frame.pack(); //set size correct
frame.setLocationRelativeTo(null);//show frame center of the screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here, I have following modified java program as shown below

SudokuLayout.java:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class SudokuLayout extends JFrame{

JTextArea txtArea;

JPanel gridPanel,butPanel;

JButton hint,reset,solve,newPuzzel;

JComboBox difficultyBox;

SudokuLayout()

{

setName("Sudoku Bord");

setLayout(new BorderLayout());

GridLayout gbag=new GridLayout(3,9);

gridPanel=new JPanel(gbag);

for (int k = 1; k <= 9; k++)

{

JPanel level2 = new JPanel(new GridLayout(3,3));

for (int i = 1; i <= 9; i++)

{

JTextField text = new JTextField(" ");

level2.add(text);

}

level2.setBorder(BorderFactory.createBevelBorder(1));

level2.setBackground(Color.BLACK);

gridPanel.add(level2);

}

add(gridPanel, BorderLayout.WEST);

hint=new JButton("Hint");

reset=new JButton("Reset");

solve=new JButton("Solve");

newPuzzel=new JButton("New Puzzel");

butPanel=new JPanel();

butPanel.setLayout(new BoxLayout(butPanel,BoxLayout.Y_AXIS));

butPanel.add(Box.createRigidArea(new Dimension(0,15)));

butPanel.add(reset);

butPanel.add(Box.createRigidArea(new Dimension(0,15)));

butPanel.add(hint);

butPanel.add(Box.createRigidArea(new Dimension(0,15)));

butPanel.add(solve);

butPanel.add(Box.createRigidArea(new Dimension(0,15)));

butPanel.add(newPuzzel);

butPanel.add(Box.createRigidArea(new Dimension(0,15)));

butPanel.add(Box.createRigidArea(new Dimension(0,15))); //add empty space between components

String[] difficulties = { "Easy", "Medium", "Hard"};

difficultyBox = new JComboBox(difficulties);

difficultyBox.setSelectedIndex(2);

butPanel.add(difficultyBox);

//add buttons panel on sudoku panel

add(butPanel,BorderLayout.EAST);

reset.addActionListener(new ButtonsAction());

hint.addActionListener(new ButtonsAction());

solve.addActionListener(new ButtonsAction());

newPuzzel.addActionListener(new ButtonsAction());

txtArea = new JTextArea(4, 20);

txtArea.setBorder(BorderFactory.createTitledBorder("output"));

txtArea.setText("Hello world!");

add(txtArea, BorderLayout.SOUTH);

}

private class ButtonsAction implements ActionListener{

@Override

public void actionPerformed(ActionEvent e) {

Object button=e.getSource();

if(button.equals(hint))

{

txtArea.setText("Hint Button clicked!..");

}

else if(button.equals(reset))

{

txtArea.setText("Reset Button clicked!");

}

else if(button.equals(solve))

{

txtArea.setText("Solve button clicked!");

}

else

{

txtArea.setText("New Puzzel button clicked!");

}

}

}

}

TestSudoku.java:

import java.awt.Color;

import javax.swing.*;

public class TestSudoku

{

public static void main(String args[])

{

SudokuLayout frame=new SudokuLayout();

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

frame.setSize(450,350);

frame.setResizable(false);

frame.setVisible(true);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Add a comment
Know the answer?
Add Answer to:
Please fix and test my code so that all the buttons and function are working in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • tart from the following code, and add Action Listener to make it functional to do the...

    tart from the following code, and add Action Listener to make it functional to do the temperature conversion in the direction of the arrow that is clicked. . (Need about 10 lines) Note: import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import java.text.DecimalFormat; public class temperatureConverter extends JFrame { public static void main(String[] args) {     JFrame frame = new temperatureConverter();     frame.setTitle("Temp");     frame.setSize(200, 100);     frame.setLocationRelativeTo(null);     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setVisible(true); }    public temperatureConverter() {     JLabel lblC = new...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • I have this problem for a final thats in my book thats i'm stuck on ....

    I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated. Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: ⦁ For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • Any thoughts on this, i can get it to run but can't pass code around and...

    Any thoughts on this, i can get it to run but can't pass code around and the button doesnt move around. i didnt pass the student, or the app.java. What will you learn Combined use of Timer and the tracking of user interactions Deliverables app.java myJFrame.java myJPanel.java and other necessary Java files Contents The objective of the lab is to create a game in which the player has to click on a moving student-button to score. The student button has...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel...

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel titleLabel;       private JTextField titleTxtFd;       private JComboBox typeCmb;       private ButtonGroup ratingGP;       private JButton processBnt;       private JButton endBnt;       private JButton clearBnt;       private JTextArea entriesTxtAr;       private JRadioButton excellentRdBnt;       private JRadioButton veryGoodRdBnt;       private JRadioButton fairRdBnt;       private JRadioButton poorRdBnt;       private String ratingString;       private final String EXCELLENT = "Excellent";       private final String VERYGOOD = "Very Good";       private final String FAIR = "Fair";       private final String POOR = "Poor";       String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT