Cześć. Mam problem z kalkulatorem, mój kalkulator rozpoznaje pojedyncze działanie, jak napisze 2+2 i zatwierdzę znakiem = to pokaże oczywiście dobry wynik ale jak napisze 2+2+2 to pokaże znowu 4. Nie wiem jak dodać funkcje, która pozwoli mi dodać kilka cyfr bez konieczności wstawiania znaku = po każdej operacji. Jestem początkującym i napisałem ten kod jak umiałem i zdaję sobie sprawe z tego, że mozna to napisać w bardziej optymalny sposób. Z góry dziekuję za pomoc i prosze o porady odnosie ogólnego wyglądu kodu.
package dodwaniewizualne;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class dodawanie {
int a=0;
int b=0;
int wynikobliczenia=0;
boolean plus=false;
boolean odej=false;
boolean mon=false;
boolean dziel=false;
String ope;
private JFrame frame;
private JTextField ekran;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dodawanie window = new dodawanie();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public dodawanie() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 399, 422);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
ekran = new JTextField();
ekran.setBounds(23, 11, 339, 58);
frame.getContentPane().add(ekran);
ekran.setColumns(10);
JButton jeden = new JButton("1");
jeden.setBounds(23, 80, 89, 62);
jeden.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + jeden.getText();
ekran.setText(cyfra);
}
});
frame.getContentPane().add(jeden);
JButton dwa = new JButton("2");
dwa.setBounds(122, 80, 89, 62);
dwa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + dwa.getText();
ekran.setText(cyfra);
}
});
frame.getContentPane().add(dwa);
JButton trzy = new JButton("3");
trzy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + trzy.getText();
ekran.setText(cyfra);
}
});
trzy.setBounds(222, 80, 89, 62);
frame.getContentPane().add(trzy);
JButton cztery = new JButton("4");
cztery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + cztery.getText();
ekran.setText(cyfra);
}
});
cztery.setBounds(23, 153, 89, 62);
frame.getContentPane().add(cztery);
JButton piec = new JButton("5");
piec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + piec.getText();
ekran.setText(cyfra);
}
});
piec.setBounds(122, 153, 89, 62);
frame.getContentPane().add(piec);
JButton szesc = new JButton("6");
szesc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + szesc.getText();
ekran.setText(cyfra);
}
});
szesc.setBounds(222, 153, 89, 62);
frame.getContentPane().add(szesc);
JButton siedem = new JButton("7");
siedem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + siedem.getText();
ekran.setText(cyfra);
}
});
siedem.setBounds(23, 226, 89, 62);
frame.getContentPane().add(siedem);
JButton osiem = new JButton("8");
osiem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + osiem.getText();
ekran.setText(cyfra);
}
});
osiem.setBounds(122, 226, 89, 62);
frame.getContentPane().add(osiem);
JButton dziewiec = new JButton("9");
dziewiec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + dziewiec.getText();
ekran.setText(cyfra);
}
});
dziewiec.setBounds(222, 226, 89, 62);
frame.getContentPane().add(dziewiec);
JButton zero = new JButton("0");
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cyfra = ekran.getText() + zero.getText();
ekran.setText(cyfra);
}
});
zero.setBounds(122, 299, 89, 62);
frame.getContentPane().add(zero);
JButton dodawanie = new JButton("+");
dodawanie.setBounds(321, 80, 41, 62);
dodawanie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
plus=true;
String wartosc = ekran.getText();
a = Integer.valueOf(wartosc);
ekran.setText(null);
}
});
frame.getContentPane().add(dodawanie);
JButton minus = new JButton("-");
minus.setBounds(321, 153, 41, 62);
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
odej=true;
String wartosc = ekran.getText();
a = Integer.valueOf(wartosc);
ekran.setText(null);
}
});
frame.getContentPane().add(minus);
JButton mnozenie = new JButton("*");
mnozenie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mon=true;
String wartosc = ekran.getText();
a = Integer.valueOf(wartosc);
ekran.setText(null);
}
});
mnozenie.setBounds(321, 226, 41, 62);
frame.getContentPane().add(mnozenie);
JButton dzielenie = new JButton("/");
dzielenie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dziel=true;
String wartosc = ekran.getText();
a = Integer.valueOf(wartosc);
ekran.setText(null);
}
});
dzielenie.setBounds(321, 299, 41, 62);
frame.getContentPane().add(dzielenie);
JButton czyszczenie = new JButton("C");
czyszczenie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a=0;
b=0;
ekran.setText(null);
}
});
czyszczenie.setBounds(23, 299, 89, 62);
frame.getContentPane().add(czyszczenie);
JButton wynik = new JButton("wynik");
wynik.setBounds(222, 299, 89, 62);
wynik.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(plus==true) {
String wartosc = ekran.getText();
b = Integer.valueOf(wartosc);
wynikobliczenia=a+b;
String wynikobliczenia2 = Integer.toString(wynikobliczenia);
ekran.setText(null);
ekran.setText(wynikobliczenia2);
plus=false;
}
if(odej==true) {
String wartosc = ekran.getText();
b = Integer.valueOf(wartosc);
wynikobliczenia=a-b;
String wynikobliczenia2 = Integer.toString(wynikobliczenia);
ekran.setText(null);
ekran.setText(wynikobliczenia2);
odej=false;
}
if(mon==true) {
String wartosc = ekran.getText();
b = Integer.valueOf(wartosc);
wynikobliczenia=a*b;
String wynikobliczenia2 = Integer.toString(wynikobliczenia);
ekran.setText(null);
ekran.setText(wynikobliczenia2);
mon=false;
}
if(dziel==true) {
String wartosc = ekran.getText();
b = Integer.valueOf(wartosc);
if(a==0) {
ekran.setText("Nie mozna dzieli przez zero!!");
}else {
wynikobliczenia=a/b;
String wynikobliczenia2 = Integer.toString(wynikobliczenia);
ekran.setText(null);
ekran.setText(wynikobliczenia2);
}
dziel=false;
}
}
});
frame.getContentPane().add(wynik);
}
}