我正在尝试制作一个计算器,但是有问题。当我多次显示答案时,程序会将答案一遍又一遍,如下图所示。
这是我的代码,任何人都可以帮我解决这个问题(在显示下一个答案之前清空标签)。 谢谢!
package Räknare;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.LayoutManager;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class huvudklassen extends JFrame {
static GraphicsConfiguration gc = null;
// Variables
static JLabel welcome = new JLabel("Welcome to my calculator");
static JLabel theequation = new JLabel("what do you want to caculate?");
static JFrame frame1 = new JFrame(gc);
static JFrame frame2 = new JFrame(gc);
static JTextField s = new JTextField();
static JButton start = new JButton("start");
static JButton calculate = new JButton("Calculate");
//
public static void frame() {
frame1.setResizable(false);
frame1.setVisible(true);
frame1.setSize(400, 500);
frame1.setTitle("My Calculatur");
frame1.setLayout(null);
frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame1.setLocationRelativeTo(null);
welcome.setBounds(120, 155, 200, 50);;
start.setBounds(150,200,100,50);
frame1.add(welcome);
frame1.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame1.setVisible(false);
frame2.setSize(400, 500);
frame2.setResizable(false);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame2.setLayout(null);
frame2.setTitle("Caculation");
frame2.setLocationRelativeTo(null);
s.setBounds(150,200,100,30);
theequation.setBounds(120,155,200,50);
calculate.setBounds(120, 235, 150, 40);
frame2.add(s);
frame2.add(theequation);
frame2.add(calculate);
calculate.addActionListener(new ActionListener() {
public void actionPerformed1(ActionEvent a) {
String theequation2 = s.getText();
String left = "";
String tecken = "";
String right = "";
// if (theequation2 == null || theequation2.equals(""))
if (theequation2.contains("+")) {
left = left + theequation2.substring(0, theequation2.indexOf("+"));
right = right + theequation2.substring(theequation2.indexOf("+") + 1);
tecken = tecken + "+";
}
if (theequation2.contains("-")) {
left = left + theequation2.substring(0, theequation2.indexOf("-"));
right = right + theequation2.substring(theequation2.indexOf("-") + 1);
tecken = tecken + "-";
}
if (theequation2.contains("*")) {
left = left + theequation2.substring(0, theequation2.indexOf("*"));
right = right + theequation2.substring(theequation2.indexOf("*") + 1);
tecken = tecken + "*";
}
if (theequation2.contains("/")) {
left = left + theequation2.substring(0, theequation2.indexOf("/"));
right = right + theequation2.substring(theequation2.indexOf("/") + 1);
tecken = tecken + "/";
}
if (theequation2.contains("^")) {
left = left + theequation2.substring(0, theequation2.indexOf("^"));
right = right + theequation2.substring(theequation2.indexOf("^") + 1);
tecken = tecken + "^";
}
left = left.trim();
right = right.trim();
double left1 = Double.parseDouble(left);
double right1 = Double.parseDouble(right);
double svar = 0;
if (tecken.equals("+"))
svar = svar + left1 + right1;
if (tecken.equals("-"))
svar = svar + (left1 - right1);
if (tecken.equals("*"))
svar = svar + left1 * right1;
if (tecken.equals("/"))
svar = svar + left1 * right1;
if (tecken.equals("%"))
svar = svar + left1 % right1;
if (tecken.equals("^")) {
svar = Math.pow(left1, right1);
}
String answer1 = Double.toString(svar);
// JOptionPane.showMessageDialog(null, answer1);
JLabel answer = new JLabel();
answer.setBounds(180, 300, 150, 40);
answer.setText(answer1);
frame2.add(answer);
frame2.revalidate();
frame2.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
ActionEvent ActionEvent = null;
actionPerformed1(ActionEvent);
}
});
}
});
}
public static void main(String[] args) {
frame();
}
}
JLabel answer = new JPanel();
// answer.setBounds(180, 300, 150, 40);
答案 0 :(得分:2)
您应该使用相同的标签,而不是每次都使用新标签。
JLabel answer = new JLabel();
answer.setBounds(180, 300, 150, 40);
answer.setText(answer1);