our professor gave us a template and here it is: import java.awt.BorderLayout; i
ID: 3828325 • Letter: O
Question
our professor gave us a template and here it is:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Lab14 extends JFrame {
public Lab14() {
// TODO Set the title of the Frame to CS251 Calculator
// TODO set the size of the Frame to 600 x 600
// TODO Make the Frame non-resizable
// TODO place the frame in the middle of the screen
// TODO Terminate the program, if we click on the close button of the titlebar.
createComponents();
// TODO set the frame to visible
}
public void createComponents() {
JPanel numKeysPanel = new JPanel(/* TODO set the layout to a grid layout 4x3*/);
JPanel operationKeysPanel = new JPanel(/*TODO set the layout to a grid layout 2x2*/);
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JLabel emptyCell1 = new JLabel("");
JLabel emptyCell2 = new JLabel("");
JButton decimalPoint = new JButton(".");
// TODO Add num1, num2,....decimalPoint to the numKeysPanel panel
// TODO Create four JButtons with + - * /
// TODO Add the JButtons you have created to the operationKeysPanel panel
//TODO Add numKeysPanel to the center region of the JFrame
//TODO Add operationKeysPanel to the East region of the JFrame
//TODO Create a JTextField object with text "0" and make the JTextField disabled
//TODO add the JTextField object to the North region of the JFrame
}
public static void main(String[]args){
new Lab14();
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab14;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Dimension;
/**
*
* @author sivaram
*/
public class Lab14 extends JFrame
{
public static void main(String[] args)
{
new Lab14();
}
public Lab14() {
JFrame jframe = new JFrame("CS251 Calculator");// TODO Set the title of the Frame to CS251 Calculator
jframe.setPreferredSize(new Dimension(600,600)); // TODO set the size of the Frame to 600 x 600
jframe.setResizable(false); // TODO Make the Frame non-resizable
jframe.setLocationRelativeTo(null); // TODO place the frame in the middle of the screen
/*Sets the location of the window relative to the specified component. If the component is not currently showing, or c (the Component) is null, the window is centered on the screen.*/
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE); // TODO Terminate the program, if we click on the close button of the titlebar.
createComponents();
}
public void createComponents() {
JPanel numKeysPanel = new JPanel();
numKeysPanel.setLayout(new GridLayout(4,3));/* TODO set the layout to a grid layout 4x3*/
JPanel operationKeysPanel = new JPanel();
operationKeysPanel.setLayout(new GridLayout(2,2));/*TODO set the layout to a grid layout 2x2*/
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JLabel emptyCell1 = new JLabel("");
JLabel emptyCell2 = new JLabel("");
JButton decimalPoint = new JButton(".");
numKeysPanel.add(num1);
numKeysPanel.add(num2);
numKeysPanel.add(num3);
numKeysPanel.add(num4);
numKeysPanel.add(num5);
numKeysPanel.add(num6);
numKeysPanel.add(num7);
numKeysPanel.add(num8);
numKeysPanel.add(num9);
numKeysPanel.add(emptyCell1);
numKeysPanel.add(emptyCell2);
numKeysPanel.add(decimalPoint);
JButton plus = new JButton("+");
JButton minus= new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");// TODO Create four JButtons with + - * /
operationKeysPanel.add(plus);
operationKeysPanel.add(minus);
operationKeysPanel.add(mul);
operationKeysPanel.add(div); // TODO Add the JButtons you have created to the operationKeysPanel panel
JTextField zero = new JTextField("0");
zero.setEnabled(false);
zero.setText("Disabled");//TODO Create a JTextField object with text "0" and make the JTextField disabled
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.