Witam!
Mój problem polega na tym, że...
Kalkulator jako tako działa - liczy pierwsze działanie. Problem pojawia się wtedy gdy do wyświetlonego wyniku zamierzam dodać kolejną liczbę. Wiem, że błąd polega na użyciu zmiennej typu final, ale nie mam pojęcia jak ją zastąpić. Proszę o pomoc.
Pracuję w środowisku eclipse z pluginem do tworzenia okienek (nazwy zapomniałem).
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
public class App extends JFrame {
private JPanel contentPane;
private JTextField PoleTekstowe1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
App frame = new App();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public App() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 365, 290);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
PoleTekstowe1 = new JTextField();
PoleTekstowe1.setHorizontalAlignment(SwingConstants.CENTER);
PoleTekstowe1.setFont(new Font("Tahoma", Font.PLAIN, 16));
PoleTekstowe1.setBounds(10, 10, 244, 50);
contentPane.add(PoleTekstowe1);
PoleTekstowe1.setColumns(10);
final JLabel znak = new JLabel("");
znak.setFont(new Font("Tahoma", Font.BOLD, 16));
znak.setHorizontalAlignment(SwingConstants.CENTER);
znak.setBounds(238, 10, 101, 50);
contentPane.add(znak);
final JLabel wynik = new JLabel("");
wynik.setFont(new Font("Tahoma", Font.BOLD, 15));
wynik.setForeground(Color.BLACK);
wynik.setHorizontalAlignment(SwingConstants.CENTER);
wynik.setBounds(94, 166, 101, 75);
contentPane.add(wynik);
final JButton equalsButton = new JButton("=");
equalsButton.setFont(new Font("Tahoma", Font.BOLD, 11));
equalsButton.setBounds(10, 166, 75, 75);
contentPane.add(equalsButton);
JButton plusButton = new JButton("+");
plusButton.setFont(new Font("Tahoma", Font.BOLD, 11));
plusButton.setBounds(10, 71, 75, 75);
contentPane.add(plusButton);
JButton minusButton = new JButton("-");
minusButton.setFont(new Font("Tahoma", Font.BOLD, 18));
minusButton.setBounds(94, 71, 75, 75);
contentPane.add(minusButton);
JButton timesButton = new JButton("*");
timesButton.setFont(new Font("Tahoma", Font.BOLD, 11));
timesButton.setBounds(179, 71, 75, 75);
contentPane.add(timesButton);
JButton divideButton = new JButton("/");
divideButton.setFont(new Font("Tahoma", Font.BOLD, 11));
divideButton.setBounds(264, 71, 75, 75);
contentPane.add(divideButton);
//dodawanie
plusButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final double x=Double.parseDouble(PoleTekstowe1.getText());
znak.setText("+");
equalsButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double y=Double.parseDouble(PoleTekstowe1.getText());
double z;
if(znak.getText()=="+")
{
z=x+y;
wynik.setText(""+z);
PoleTekstowe1.setText("" + z);
}
else
{
z=0;
}
}
}
);
}
}
);
//odejmowanie
minusButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
final double x=Double.parseDouble(PoleTekstowe1.getText());
znak.setText("-");
equalsButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double y=Double.parseDouble(PoleTekstowe1.getText());
double z;
if(znak.getText()=="-")
{
z=x+y;
}
else
{
z=0;
}
wynik.setText(""+z);
PoleTekstowe1.setText(""+z);
}
}
);
}
}
);
//mnożenie
timesButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final double x=Double.parseDouble(PoleTekstowe1.getText());
znak.setText("*");
equalsButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double y=Double.parseDouble(PoleTekstowe1.getText());
double z;
if(znak.getText()=="*")
{
z=x-y;
}
else
{
z=0;
}
wynik.setText(""+z);
PoleTekstowe1.setText("" + z);
}
}
);
}
}
);
//dzielenie
divideButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final double x=Double.parseDouble(PoleTekstowe1.getText());
znak.setText("/");
equalsButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double y=Double.parseDouble(PoleTekstowe1.getText());
double z;
if(znak.getText()=="/")
{
z=x/y;
}
else
{
z=0;
}
wynik.setText(""+z);
PoleTekstowe1.setText("" + z);
}
}
);
}
}
);
}
}
<code=java></code>