Reputation: 21
Hello so apologises if this is a stupid question but i'm struggling to work out why my JoptionPane message is not closing. I'm wanting to use a nested control structure to countup the amount of calculations performed by the user.
I figure it's something to do with how i'm either counting up or how i'm testing to see if the condition has been met to dispose of the calculator frame (i'm going to a new thing that will happen but for now i'm just wanting the whole thing to close)
package ac.uk.valley.forth;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class Calculator extends JFrame {
protected static final String operatorButton = null;
private JPanel contentPane;
private JTextField textField;
private double total = 0.0;
private double actualTotal = 0.0;
private char operator;
public int counter = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 291, 330);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(0, 37, 275, 85);
panel.add(textField);
textField.setColumns(10);
JButton button_10 = new JButton("7");
button_10.setBounds(5, 157, 44, 23);
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("7"));
}
});
panel.add(button_10);
JButton button_11 = new JButton("8");
button_11.setBounds(55, 157, 44, 23);
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("8"));
}
});
panel.add(button_11);
JButton button_12 = new JButton("9");
button_12.setBounds(109, 157, 44, 23);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("9"));
}
});
panel.add(button_12);
JButton btnOff = new JButton("OFF");
btnOff.setBounds(158, 157, 61, 23);
btnOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// setVisible(false);
// dispose();
}
});
panel.add(btnOff);
JButton button_4 = new JButton("4");
button_4.setBounds(5, 185, 44, 23);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("4"));
}
});
panel.add(button_4);
JButton button_5 = new JButton("5");
button_5.setBounds(55, 185, 44, 23);
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("5"));
}
});
panel.add(button_5);
JButton button_6 = new JButton("6");
button_6.setBounds(109, 185, 44, 23);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("6"));
}
});
panel.add(button_6);
JButton button = new JButton("1");
button.setBounds(5, 213, 44, 23);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("1"));
}
});
panel.add(button);
JButton button_1 = new JButton("2");
button_1.setBounds(55, 213, 44, 23);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("2"));
}
});
panel.add(button_1);
JButton button_2 = new JButton("3");
button_2.setBounds(109, 213, 44, 23);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("3"));
}
});
panel.add(button_2);
JButton button_13 = new JButton("0");
button_13.setBounds(5, 240, 44, 23);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("0"));
}
});
panel.add(button_13);
JButton button_14 = new JButton(".");
button_14.setBounds(56, 241, 44, 23);
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("."));
}
});
panel.add(button_14);
JButton btnCe_1 = new JButton("CE");
btnCe_1.setBounds(109, 240, 53, 23);
btnCe_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
panel.add(btnCe_1);
JButton equalsButton = new JButton("=");
equalsButton.setBounds(166, 212, 53, 51);
equalsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calculate();
}
});
panel.add(equalsButton);
final JButton subtractionButton = new JButton("-");
subtractionButton.setBounds(221, 213, 44, 23);
subtractionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("-"));
String operatorText = subtractionButton.getText();
getOperator(operatorText);
}
});
panel.add(subtractionButton);
final JButton moduloButton = new JButton("%");
moduloButton.setBounds(221, 157, 44, 23);
moduloButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("%"));
String operatorText = moduloButton.getText();
getOperator(operatorText);
}
});
panel.add(moduloButton);
final JButton multiplicationButton = new JButton("*");
multiplicationButton.setBounds(167, 185, 44, 23);
multiplicationButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("*"));
String operatorText = multiplicationButton.getText();
getOperator(operatorText);
}
});
panel.add(multiplicationButton);
final JButton divideButton = new JButton("/");
divideButton.setBounds(221, 185, 44, 23);
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("/"));
String operatorText = divideButton.getText();
getOperator(operatorText);
}
});
panel.add(divideButton);
final JButton additionButton = new JButton("+");
additionButton.setBounds(221, 240, 44, 23);
additionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String operatorText = additionButton.getText();
getOperator(operatorText);
}
});
panel.add(additionButton);
}
private void getOperator(String operatorButton) {
operator = operatorButton.charAt(0);
total = total + Double.parseDouble(textField.getText());
textField.setText("");
}
public void Calculate() {
switch (operator) {
case '+':
actualTotal = total + Double.parseDouble(textField.getText());
break;
case '-':
actualTotal = total - Double.parseDouble(textField.getText());
break;
case '/':
actualTotal = total / Double.parseDouble(textField.getText());
break;
case '*':
actualTotal = total * Double.parseDouble(textField.getText());
break;
case '%':
actualTotal = total % Double.parseDouble(textField.getText());
break;
}
textField.setText(Double.toString(actualTotal));
total = 0;
counter++;
other(counter);
}
public void other(int counter) {
boolean onGoing = true;
String infoMessage = Integer.toString(counter);
do
{
JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + "Calculations Performed",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.getRootFrame().dispose();
if (counter > 2)
{
onGoing = false;
setVisible(false);
}
} while (onGoing == true);
}
}
Upvotes: 0
Views: 107
Reputation: 2652
As you have mentioned that you want to display how many calculations have been performed when user closes the JFrame
, here is my solution:
I have removed other(int counter)
method which used to display message dialog from inside the loop. I have added a windowClosing
listener which displays one and only message when user clicks on cancel
button.
Please have a look at the following:-
package ac.uk.valley.forth;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
protected static final String operatorButton = null;
private JPanel contentPane;
private JTextField textField;
private double total = 0.0;
private double actualTotal = 0.0;
private char operator;
private int calcCount = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
JOptionPane.showMessageDialog(null, "Total Calculation performed: " + frame.getCalcCount(),
"Calculations Performed", JOptionPane.INFORMATION_MESSAGE);
}
});
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 291, 330);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(0, 37, 275, 85);
panel.add(textField);
textField.setColumns(10);
JButton button_10 = new JButton("7");
button_10.setBounds(5, 157, 44, 23);
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("7"));
}
});
panel.add(button_10);
JButton button_11 = new JButton("8");
button_11.setBounds(55, 157, 44, 23);
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("8"));
}
});
panel.add(button_11);
JButton button_12 = new JButton("9");
button_12.setBounds(109, 157, 44, 23);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("9"));
}
});
panel.add(button_12);
JButton btnOff = new JButton("OFF");
btnOff.setBounds(158, 157, 61, 23);
btnOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// setVisible(false);
// dispose();
}
});
panel.add(btnOff);
JButton button_4 = new JButton("4");
button_4.setBounds(5, 185, 44, 23);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("4"));
}
});
panel.add(button_4);
JButton button_5 = new JButton("5");
button_5.setBounds(55, 185, 44, 23);
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("5"));
}
});
panel.add(button_5);
JButton button_6 = new JButton("6");
button_6.setBounds(109, 185, 44, 23);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("6"));
}
});
panel.add(button_6);
JButton button = new JButton("1");
button.setBounds(5, 213, 44, 23);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("1"));
}
});
panel.add(button);
JButton button_1 = new JButton("2");
button_1.setBounds(55, 213, 44, 23);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("2"));
}
});
panel.add(button_1);
JButton button_2 = new JButton("3");
button_2.setBounds(109, 213, 44, 23);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("3"));
}
});
panel.add(button_2);
JButton button_13 = new JButton("0");
button_13.setBounds(5, 240, 44, 23);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("0"));
}
});
panel.add(button_13);
JButton button_14 = new JButton(".");
button_14.setBounds(56, 241, 44, 23);
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat("."));
}
});
panel.add(button_14);
JButton btnCe_1 = new JButton("CE");
btnCe_1.setBounds(109, 240, 53, 23);
btnCe_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
panel.add(btnCe_1);
JButton equalsButton = new JButton("=");
equalsButton.setBounds(166, 212, 53, 51);
equalsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calculate();
}
});
panel.add(equalsButton);
final JButton subtractionButton = new JButton("-");
subtractionButton.setBounds(221, 213, 44, 23);
subtractionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("-"));
String operatorText = subtractionButton.getText();
getOperator(operatorText);
}
});
panel.add(subtractionButton);
final JButton moduloButton = new JButton("%");
moduloButton.setBounds(221, 157, 44, 23);
moduloButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("%"));
String operatorText = moduloButton.getText();
getOperator(operatorText);
}
});
panel.add(moduloButton);
final JButton multiplicationButton = new JButton("*");
multiplicationButton.setBounds(167, 185, 44, 23);
multiplicationButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("*"));
String operatorText = multiplicationButton.getText();
getOperator(operatorText);
}
});
panel.add(multiplicationButton);
final JButton divideButton = new JButton("/");
divideButton.setBounds(221, 185, 44, 23);
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// textField.setText(textField.getText().concat("/"));
String operatorText = divideButton.getText();
getOperator(operatorText);
}
});
panel.add(divideButton);
final JButton additionButton = new JButton("+");
additionButton.setBounds(221, 240, 44, 23);
additionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String operatorText = additionButton.getText();
getOperator(operatorText);
}
});
panel.add(additionButton);
}
private void getOperator(String operatorButton) {
operator = operatorButton.charAt(0);
total = total + Double.parseDouble(textField.getText());
textField.setText("");
}
public void Calculate() {
switch (operator) {
case '+':
actualTotal = total + Double.parseDouble(textField.getText());
break;
case '-':
actualTotal = total - Double.parseDouble(textField.getText());
break;
case '/':
actualTotal = total / Double.parseDouble(textField.getText());
break;
case '*':
actualTotal = total * Double.parseDouble(textField.getText());
break;
case '%':
actualTotal = total % Double.parseDouble(textField.getText());
break;
}
textField.setText(Double.toString(actualTotal));
total = 0;
calcCount++;
}
public int getCalcCount() {
return calcCount;
}
}
Please tell me whether it solves your problem or not.
Upvotes: 0
Reputation: 2652
I have modified the function public void other(int counter)
a bit, now the JOptionPane
and JFrame
both are closing. The int
variable counter
was always 1
. So you needed to increment the value of counter
inside the while
loop. The String
variable infoMessage
was required to be inside the loop to reflect the current value of counter
. Please have a look at the following:-
public void other(int counter) {
boolean onGoing = true;
do{
String infoMessage = Integer.toString(counter);
JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + "Calculations Performed",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.getRootFrame().dispose();
if (counter >= 2){
onGoing = false;
this.dispose();// JFrame exits
}
counter ++;
} while (onGoing == true);
}
Hope, it serves your purpose.
Upvotes: 0
Reputation: 285405
You've got a while (true)
block completely stomping on the Swing event thread and preventing the JOptionPane from closing:
while (onGoing == true);
Solution: get rid of it; you don't need it, since this is an event-driven program. Instead put that code in your events (your ActionListeners).
Some asides:
setBounds()
might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.Upvotes: 2