你好,如果这是一个愚蠢的问题,我很抱歉,但我正在努力解决为什么我的JoptionPane消息没有关闭。我想使用嵌套控件结构来计算用户执行的计算量。
我认为这与我如何计算或我如何测试以确定是否已经满足处理计算器框架(i'我将会发生一件新事物,但现在我只是希望整件事情能够结束)
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);
}
}
答案 0 :(得分:2)
你有一个while (true)
块完全踩踏Swing事件线程并阻止JOptionPane关闭:
while (onGoing == true);
解决方案:摆脱它;你不需要它,因为这是一个事件驱动的程序。而是将该代码放入您的事件(您的ActionListeners)。
一些旁白:
setBounds()
似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI会越多,你会遇到更严重的困难使用它们时当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。答案 1 :(得分:0)
我稍微修改了函数JOptionPane
,现在JFrame
和int
都关闭了。 counter
变量1
始终为counter
。因此,您需要在while
循环内增加String
的值。 infoMessage
变量counter
必须位于循环内以反映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);
}
的当前值。请看看以下内容: -
<DOCTYPE HTML>
<html>
<body>
<p>Please enter your name below.</p>
<form method="post" action="/cgi-bin/test2.cgi">
First:<br>
<input type="text" name="firstname">
<br>
Last:<br>
<input type="text" name="lastname"><br>
<br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
希望,它符合你的目的。
答案 2 :(得分:0)
正如您所提到的,您想要显示用户关闭JFrame
时执行了多少计算,这是我的解决方案:
我删除了用于在循环内显示消息对话框的other(int counter)
方法。我添加了一个windowClosing
监听器,当用户点击cancel
按钮时,该监听器会显示一条且唯一的消息。
请查看以下内容: -
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;
}
}
请告诉我它是否能解决您的问题。