need help to reconstruct my code to compete this task, its acode for simple java GUI calculator
Question:
need help to reconstruct my code to compete this task, its acode for simple java GUI calculator, cheers
here is the code:
/* CalculatorPanel.java */
import javax.swing.*;import java.awt.*;import java.awt.event.*;
/*** The calculator panel* @author*/public class CalculatorPanel extends JPanel {private static final long serialVersionUID = 1L;private static final int CALC_WIDTH=250;private static final int CALC_HEIGHT=325;
public CalculatorPanel() {setLayout(new BorderLayout());setBackground(Color.lightGray); // create the input/outputpanelresult = 0; // result variable of labellastCommand = "=";start = true;
// add the displayresultLabel = new JLabel("0", SwingConstants.RIGHT); // set theJLabel to RIGHTresultLabel.setFont(new Font("Helvetica", Font.BOLD, 40)); // setthe font sizeadd(resultLabel, BorderLayout.NORTH); // set the border layout toNORTH// Action listener createsActionListener insert = new InsertAction();ActionListener command = new CommandAction();
// CREATE A LAYOUT OF THE BUTTONS 4X4 gridpanel = new JPanel();panel.setLayout(new GridLayout(4, 4)); //set the 4x4 gridpanel.setBackground(Color.DARK_GRAY); //set the backgroundcolor
// add the button text and assign the actionaddButton("7", insert);addButton("8", insert);addButton("9", insert);addButton("/", command);addButton("4", insert);addButton("5", insert);addButton("6", insert);addButton("*", command);addButton("1", insert);addButton("2", insert);addButton("3", insert);addButton("-", command);
addButton("0", insert);addButton(".", insert);addButton("=", command);addButton("+", command);JButton clear = new JButton("Clear");clear.setFont(new Font("Helvetica", Font.BOLD, 15)); // set thefont sizeclear.setForeground(Color.BLUE); //set the font color to blueclear.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {resultLabel.setText("0");}});add(clear,BorderLayout.SOUTH);add(panel, BorderLayout.CENTER); // set the panel to center}// add buttonprivate void addButton(String label, ActionListener listener){JButton button = new JButton(label);button.setPreferredSize(new Dimension(55,30)); //set the buttonsizebutton.setFont(new Font("Helvetica", Font.BOLD, 15)); // set thefont sizebutton.setForeground(Color.BLUE); //set the font color tobluebutton.addActionListener(listener); // add the actionlistenerpanel.add(button); // add the button to the panel}
private class InsertAction implements ActionListener {public void actionPerformed(ActionEvent event) {String input = event.getActionCommand();if (start) {resultLabel.setText("");start = false;} if(!resultLabel.getText().equals("0"))resultLabel.setText(resultLabel.getText() + input); //display thetext to the label when no 0else{resultLabel.setText( input); //display the text to the label when0}}}
private class CommandAction implements ActionListener{public void actionPerformed(ActionEvent event) {String command = event.getActionCommand();
if (start) {
if (command.equals("-")) {resultLabel.setText(command);start = false;} elselastCommand = command;} else {calculate(Double.parseDouble(resultLabel.getText()));lastCommand = command;start = true;}}}
// method that calculate the result of two inputspublic void calculate(double x) {if (lastCommand.equals("+"))result += x; // addelse if (lastCommand.equals("-"))result -= x; // subtractelse if (lastCommand.equals("*"))result *= x; // multiplyelse if (lastCommand.equals("/"))result /= x; // divideelse if (lastCommand.equals("="))result = x; // equalsresultLabel.setText("" + result); // display the result to thelabel}
//variablesprivate JLabel resultLabel;private JPanel panel;private double result;private String lastCommand;private boolean start;
}
/* CalculatorDriver.java*/
import javax.swing.JFrame;import javax.swing.*;import java.awt.event.*;/** Driver Class for the Calcultor** @author*/public class CalculatorDriver {public static void main(String[] args) {// create JFrame objectJFrame frame=new JFrame("Simple Calculator");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// create object of CalculatorPanel classCalculatorPanel panel=new CalculatorPanel();frame.getContentPane().add(panel); // add panel into frameframe.pack();frame.setResizable(false); // frame should not be resizableframe.setLocation(400,300); // set location of frameframe.setVisible(true); // make frame visible}}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill