I have an error in the program could someone help me fix it? & if possible could
ID: 3626611 • Letter: I
Question
I have an error in the program could someone help me fix it?& if possible could you show me how to add action to the button(like number 2)?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
*/
/**
* @author mma216
*
*/
public class Calculator extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -2629194855334324353L;
/**
* @param args
*/
protected JTextField results;
public Calculator(){
results = new JTextField();
results.setText("0");
results.setEditable(false);
results.setHorizontalAlignment(JTextFiel…
getContentPane().add(results,BorderLayou…
getContentPane().add(new ButtonPanel(this),BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_…
setLocationRelativeTo(null);
pack();
}
public static void main(String[] args) {
(new Calculator()).setVisible(true);
// TODO Auto-generated method stub
}
protected class ButtonPanel extends JPanel{
public ButtonPanel(ActionListener listener){
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("7");
button.addActionListener(listener);
c.gridx = 0;
c.gridy = 0;
add(button, c);
c.gridx = 1;
button = new JButton("8");
button.addActionListener(listener);
add(button,c);
c.gridx = 2;
button = new JButton("9");
button.addActionListener(listener);
add(button,c);
c.gridx = 3;
button = new JButton("/");
button.addActionListener(listener);
add(button,c);
c.gridx = 0;
c.gridy = 1;
button = new JButton("4");
button.addActionListener(listener);
add(button,c);
c.gridx = 1;
button = new JButton("5");
button.addActionListener(listener);
add(button,c);
c.gridx = 2;
button = new JButton("6");
button.addActionListener(listener);
add(button,c);
c.gridx = 3;
button = new JButton("*");
button.addActionListener(listener);
add(button,c);
c.gridx = 0;
c.gridy = 2;
button = new JButton("1");
button.addActionListener(listener);
add(button,c);
c.gridx = 1;
button = new JButton("2");
button.addActionListener(listener);
add(button,c);
c.gridx = 2;
button = new JButton("3");
button.addActionListener(listener);
add(button,c);
c.gridx = 3;
button = new JButton("-");
button.addActionListener(listener);
add(button,c);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
button = new JButton("0");
button.addActionListener(listener);
add(button,c);
c.gridx = 2;
button = new JButton(".");
button.addActionListener(listener);
c.gridwidth = 1;
add(button,c);
c.gridx = 3;
button = new JButton("+");
button.addActionListener(listener);
add(button,c);
c.gridx = 4;
c.gridheight = 2;
button = new JButton("=");
button.addActionListener(listener);
Dimension s = button.getPreferredSize();
s.height = 2+s.height;
button.setPreferredSize(s);
add(button,c);
}
}
public void actionPerforne(ActionEvent arg0) {
System.out.println("hhhfhdfhdh");
}
}
Explanation / Answer
You can contact me for any questions you have. package calculator; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /** * */ /** * @author mma216 * */ public class Calculator extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = -2629194855334324353L; /** * @param args */ protected JTextField results; public Calculator() { results = new JTextField(); results.setText("0"); results.setEditable(false); // results.setHorizontalAlignment(defaultCloseOperation); getContentPane().add(results,BorderLayout.AFTER_LAST_LINE); operands = new ArrayList(); getContentPane().add(new ButtonPanel(this),BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); pack(); } public static void main(String[] args) { (new Calculator()).setVisible(true); // TODO Auto-generated method stub } protected class ButtonPanel extends JPanel { public ButtonPanel(ActionListener listener) { super(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; JButton button = new JButton("7"); button.addActionListener(listener); c.gridx = 0; c.gridy = 0; add(button, c); c.gridx = 1; button = new JButton("8"); button.addActionListener(listener); add(button, c); c.gridx = 2; button = new JButton("9"); button.addActionListener(listener); add(button, c); c.gridx = 3; button = new JButton("/"); button.addActionListener(listener); add(button, c); operands.add("/"); c.gridx = 0; c.gridy = 1; button = new JButton("4"); button.addActionListener(listener); add(button, c); c.gridx = 1; button = new JButton("5"); button.addActionListener(listener); add(button, c); c.gridx = 2; button = new JButton("6"); button.addActionListener(listener); add(button, c); c.gridx = 3; button = new JButton("*"); button.addActionListener(listener); add(button, c); operands.add("*"); c.gridx = 0; c.gridy = 2; button = new JButton("1"); button.addActionListener(listener); add(button, c); c.gridx = 1; button = new JButton("2"); button.addActionListener(listener); add(button, c); c.gridx = 2; button = new JButton("3"); button.addActionListener(listener); add(button, c); c.gridx = 3; button = new JButton("-"); button.addActionListener(listener); add(button, c); operands.add("-"); c.gridx = 0; c.gridy = 3; c.gridwidth = 2; button = new JButton("0"); button.addActionListener(listener); add(button, c); c.gridx = 2; button = new JButton("."); button.addActionListener(listener); c.gridwidth = 1; add(button, c); c.gridx = 3; button = new JButton("+"); button.addActionListener(listener); add(button, c); operands.add("+"); c.gridx = 4; c.gridy = 0; c.gridheight = 4; c.gridwidth = 1; button = new JButton("="); button.addActionListener(listener); // Dimension s = button.getPreferredSize(); // s.height = 2 + s.height; // button.setPreferredSize(s); add(button, c); operands.add("="); } } private List operands; private boolean reset = true; public void actionPerformed(ActionEvent arg0) { String buttonPressed = arg0.getActionCommand(); String toBeCalculated = results.getText(); String result = results.getText(); String [] enteredOperands = toBeCalculated.split(" "); if(operands.contains(buttonPressed)) { reset = false; if(enteredOperands.length == 3) { result = makeCalculation(enteredOperands[0],enteredOperands[1],enteredOperands[2],buttonPressed); } else if(enteredOperands.length == 2) { } else if(enteredOperands.length == 1 && !buttonPressed.equalsIgnoreCase("=")) { result += " "+ buttonPressed; } } else { if(buttonPressed.equalsIgnoreCase(".") && !result.contains(".") && enteredOperands.length == 1) { reset = false; } if(reset) { result = ""; reset = false; } if(enteredOperands.length == 2) { result += " "+ buttonPressed; } else { result += buttonPressed; } if(result.equals(".")) { result = "0."; } } results.setText(result); } private String makeCalculation(String operand1,String operator1, String operand2, String operator2) { float operandNum1 = Float.parseFloat(operand1); float operandNum2 = Float.parseFloat(operand2); float result = 0; String finalResult; if(operator1.equalsIgnoreCase("+")) { result = operandNum1 + operandNum2; } else if(operator1.equalsIgnoreCase("-")) { result = operandNum1 - operandNum2; } else if(operator1.equalsIgnoreCase("*")) { result = operandNum1 * operandNum2; } else if(operator1.equalsIgnoreCase("/")) { result = operandNum1 / operandNum2; } if(!operator2.equalsIgnoreCase("=")) { finalResult = result + " "+ operator2; reset = false; } else { finalResult = result + ""; reset = true; } return finalResult; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.