Write a Java program that displays the GUI depicted below (of a Fubuki puzzle) b
ID: 3818337 • Letter: W
Question
Write a Java program that displays the GUI depicted below (of a Fubuki puzzle) by combining a number of standard Layout Managers, as needed. The GUI layout should be as precise as possible and should not change when the window is resized. Use components and specification as shown below. Note that there is no need to make the app functional i.e., do not code listeners, etc
JButton Hint items (x2) JTextField items (x9), editable. initially empty for the user to input a number JTextField items (x6), non-editable, to display row column totals. JCheckBox item, Reset Rules to toggle the rules display on/off, 19 Instructions JLabel for caption Place each number from 1 to 9 in the 10 grid exactly once, so that the sum of 16 the numbers in each row /column equals the total shown on JLabel item, 15 71 the right below to display game respectively. instructions JLabel item, to display error messagesExplanation / Answer
PROGRAM CODE:
package GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class FubukiPuzzle {
public FubukiPuzzle() {
//creating objects for frame, textfield and buttons
JFrame f;
String instructionsForPuzzle = "<html><body><u> Instructions:</u><br>Place each number<br>from 1 to 9 in the"
+"<br>grid exactly once,<br>so that the sum of<br>the numbers in each" +
"<br>row / column equals<br>the total shown on<br>the right / below" +
"<br>nrespectively" + "</body></html>";
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
Border border = BorderFactory.createLineBorder(Color.black);
JPanel leftPanel = new JPanel();
JPanel rightpanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
rightpanel.setLayout(new BoxLayout(rightpanel,BoxLayout.Y_AXIS));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
JPanel numberPanel = new JPanel(new GridLayout(4, 4, 0, 0));
JLabel rules = new JLabel("Rules");
JLabel empty = new JLabel("<html><body><br><br><br><br></body></html>");
JLabel instructions = new JLabel(instructionsForPuzzle);
instructions.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
instructions.setMaximumSize(new Dimension(160, 180));
instructions.setMaximumSize(new Dimension(160, 180));
instructions.setPreferredSize(new Dimension(160, 180));
//instructions.setSize(10, 50);
JLabel errorMessages = new JLabel("<html><body><br><br><br><br></body></html>");
errorMessages.setPreferredSize(new Dimension(700, 30));
errorMessages.setMinimumSize(new Dimension(700, 30));
errorMessages.setMaximumSize(new Dimension(700, 30));
//errorMessages.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
JTextField bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,right1,right2,right3,down1,down2,down3;
JButton hint,reset;
JCheckBox box = new JCheckBox("");
// initializing the objects with values.
// Frame defines the window and takes a name
// buttons are created for numbers and operations
f=new JFrame("Fubuki Puzzle");
// t=new JTextField();
bt1=new JTextField("");
bt2=new JTextField("");
bt3=new JTextField("");
bt4=new JTextField("");
bt5=new JTextField("");
bt6=new JTextField("");
bt7=new JTextField("");
bt8=new JTextField("");
bt9=new JTextField("");
right1=new JTextField("");
right2=new JTextField("");
right3=new JTextField("");
down1=new JTextField("");
down2=new JTextField("");
down3=new JTextField("");
hint=new JButton("Hint");
reset=new JButton("Reset");
rightpanel.add(empty);
rightpanel.add(instructions);
leftPanel.add(topPanel);
leftPanel.add(numberPanel);
leftPanel.add(new JPanel(new GridLayout(1, 1)).add(errorMessages));
topPanel.add(hint);
topPanel.add(reset);
topPanel.add(box);
topPanel.add(rules);
// setting the width and height for each component
numberPanel.add(bt7);
numberPanel.add(bt8);
numberPanel.add(bt9);
numberPanel.add(right1);
numberPanel.add(bt4);
numberPanel.add(bt5);
numberPanel.add(bt6);
numberPanel.add(right2);
numberPanel.add(bt1);
numberPanel.add(bt2);
numberPanel.add(bt3);
numberPanel.add(right3);
numberPanel.add(down1);
numberPanel.add(down2);
numberPanel.add(down3);
f.add(leftPanel);
f.add(rightpanel);
// finally settings for the frame
f.setLayout(new GridLayout(1, 2,20, 10));
f.setVisible(true);
f.setSize(500,350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(true);
}
public static void main(String args[])
{
new FubukiPuzzle();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.