Zdravim programátoři, vyskytl se malý a problém a to: nefunguje mi zadávání dvojciferných čísel v kalkulačce, funguji jen jednociferná!
package kalkulačka;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HLclass {
private int MeziVypocet;
private double KonecneCislo=0;
private char operace='0';
private boolean prvniVstup=true;
private final JFrame frame=new JFrame();
private final JLabel display=new JLabel("prříklad: ");
private final JPanel panel1=new JPanel();
private final JPanel panel2=new JPanel();
private final JPanel panel3=new JPanel();
private JButton jedna=new JButton("1");
private JButton dva=new JButton("2");
private JButton tri=new JButton("3");
private JButton ctyri=new JButton("4");
private JButton pet=new JButton("5");
private JButton sest=new JButton("6");
private JButton sedum=new JButton("7");
private JButton osum=new JButton("8");
private JButton devet=new JButton("9");
private JButton nula=new JButton("0");
private JButton plus=new JButton("+");
private JButton minus=new JButton("-");
private JButton krat=new JButton("*");
private JButton deleno=new JButton("/");
private JButton rovnase=new JButton("=");
private JButton clear=new JButton("C");
public HLclass(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().setLayout(new BorderLayout());
frame.setLayout(new GridLayout(4, 2));
panel1.add(jedna);panel1.add(dva);panel1.add(tri);panel1.add(ctyri);panel1.add(pet);
panel2.add(sest);panel2.add(sedum);panel2.add(osum);panel2.add(devet);panel2.add(nula);
panel3.add(plus);panel3.add(minus);panel3.add(krat);panel3.add(deleno);panel3.add(rovnase);panel3.add(clear);
frame.add(display);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.pack();
frame.setVisible(true);
jedna.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(1);}});
dva.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(2);}});
tri.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(3);}});
ctyri.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(3);}});
pet.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(5);}});
sest.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(6);}});
sedum.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(7);}});
osum.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(8);}});
devet.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(9);}});
nula.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(0);}});
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operace='+';
display.setText(display.getText()+"+");
}});
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operace='-';
display.setText(display.getText()+"-");
}});
krat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operace='*';
display.setText(display.getText()+"*");
}});
deleno.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operace='/';
display.setText(display.getText()+"/");
}});
rovnase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operace='0';
display.setText(display.getText()+"="+KonecneCislo);
}});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
KonecneCislo=0;
MeziVypocet=0;
operace=0;
display.setText("příklad: ");
prvniVstup=true;
}});
}
public void vypocet(int Button){
MeziVypocet*=10;
MeziVypocet+=Button;
if(prvniVstup==true)KonecneCislo=MeziVypocet;
prvniVstup=false;
if(operace=='+')KonecneCislo+=MeziVypocet;
if(operace=='-')KonecneCislo-=MeziVypocet;
if(operace=='*')KonecneCislo*=MeziVypocet;
if(operace=='/')KonecneCislo/=MeziVypocet;
display.setText(display.getText()+MeziVypocet);
MeziVypocet=0;
}
}
Ještě jedna drobnost Jak to, že nemůžu nahradit toto:
if(prvniVstup==true)KonecneCislo=MeziVypocet;
prvniVstup=false;
tím to, v zásadě je to stejné ne?
if(operace==0)KonecneCislo=MeziVypocet;
Předem díky moc za radu :).