Guten Tag liebe Nutzer,
ich habe ein Problem mit einem Array den ich über Buttons ansteuere und ausfülle.
Ich will danach mit dem Array arbeiten, dabei habe ich ein Problem wenn der Array nicht weit genug ausgefüllt ist.
Zum Beispiel:
Der Befehl
Zahlen1.get(3) funktioniert nicht wenn mein Array erst 3 Stellen hat, wobei er bei längeren Rechnungen mehr als 3 Stellen hat.
Hier mein Code:
Ich hoffe ihr könnt mir weiterhelfen.
Mit freundlichen Grüßen
MaGuS11
ich habe ein Problem mit einem Array den ich über Buttons ansteuere und ausfülle.
Ich will danach mit dem Array arbeiten, dabei habe ich ein Problem wenn der Array nicht weit genug ausgefüllt ist.
Zum Beispiel:
Der Befehl
Zahlen1.get(3) funktioniert nicht wenn mein Array erst 3 Stellen hat, wobei er bei längeren Rechnungen mehr als 3 Stellen hat.
Hier mein Code:
Java:
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class TR2 implements ActionListener
{
JFrame f;
JTextField t;
JTextArea txtProtokoll;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
static double a=0,b=0,result=0;
static int operator=0;
String REinheit;
ArrayList <String> Zahlen1 = new ArrayList();
TR2()
{
f=new JFrame("Calculator");
t=new JTextField();
txtProtokoll = new JTextArea(4,10);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bdel=new JButton("Delete");
bclr=new JButton("Clear");
txtProtokoll.setCaretPosition(txtProtokoll.getDocument().getLength());
txtProtokoll.setLineWrap(true);
txtProtokoll.setWrapStyleWord(true);
t.setBounds(10, 11, 230, 38);
txtProtokoll.setBounds(260,10,100,330);
b7.setBounds(10, 60, 50, 50);
b8.setBounds(68, 60, 50, 50);
b9.setBounds(128, 60, 50, 50);
bdiv.setBounds(188, 242, 50, 50);
b4.setBounds(10, 121, 50, 50);
b5.setBounds(68, 121, 50, 50);
b6.setBounds(128, 121, 50, 50);
bmul.setBounds(188, 182, 50, 50);
b1.setBounds(10, 182, 50, 50);
b2.setBounds(68, 182, 50, 50);
b3.setBounds(128, 182, 50, 50);
bsub.setBounds(188, 121, 50, 50);
bdec.setBounds(10,243,50,50);
b0.setBounds(68, 243, 50, 50);
beq.setBounds(128,243,50,50);
badd.setBounds(188, 60, 50, 50);
bdel.setBounds(10,300 ,108,50);
bclr.setBounds(128,300,110,50);
f.add(t);
f.add(txtProtokoll);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(bdiv);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(bmul);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(bsub);
f.add(bdec);
f.add(b0);
f.add(beq);
f.add(badd);
f.add(bdel);
f.add(bclr);
f.setLayout(null);
f.setVisible(true);
f.setSize(380,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bdel.addActionListener(this);
bclr.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText(t.getText().concat("1"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b2)
t.setText(t.getText().concat("2"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b3)
t.setText(t.getText().concat("3"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b4)
t.setText(t.getText().concat("4"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b5)
t.setText(t.getText().concat("5"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b6)
t.setText(t.getText().concat("6"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b7)
t.setText(t.getText().concat("7"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b8)
t.setText(t.getText().concat("8"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b9)
t.setText(t.getText().concat("9"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==b0)
t.setText(t.getText().concat("0"));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==bdec)
t.setText(t.getText().concat("."));
txtProtokoll.setText( txtProtokoll.getText());
if(e.getSource()==badd)
{
a=Double.parseDouble(t.getText());
operator=1;
t.setText("");
REinheit = "+";
txtProtokoll.setText(a+" "+ REinheit);
String Z1 = String.valueOf(a);
Zahlen1.add(Z1);
}
if(e.getSource()==bsub)
{
a=Double.parseDouble(t.getText());
operator= 2;
t.setText("");
REinheit = "-";
txtProtokoll.setText(a+" "+ REinheit);
String Z1 = String.valueOf(a);
Zahlen1.add(Z1);
}
if(e.getSource()==bmul)
{
a=Double.parseDouble(t.getText());
operator= 3;
t.setText("");
REinheit = "*";
txtProtokoll.setText(a+" "+ REinheit);
String Z1 = String.valueOf(a);
Zahlen1.add(Z1);
}
if(e.getSource()==bdiv)
{
a=Double.parseDouble(t.getText());
operator = 4;
t.setText("");
REinheit = "/";
txtProtokoll.setText(a+" "+ REinheit);
String Z1 = String.valueOf(a);
Zahlen1.add(Z1);
}
if(e.getSource()==beq)
{
b=Double.parseDouble(t.getText());
switch(operator)
{
case 1: result=a+b;
REinheit = "+";
break;
case 2: result=a-b;
REinheit = "-";
break;
case 3: result=a*b;
REinheit = "*";
break;
case 4: result=a/b;
REinheit = "/";
break;
default: result=0;
}
t.setText(""+result);
String Z2 = String.valueOf(b);
Zahlen1.add(Z2);
System.out.println(Zahlen1.toString());
txtProtokoll.setText( a +" "+ REinheit+" "+ b + " = "
+ result );
}
if(e.getSource()==bclr) {
t.setText("");
txtProtokoll.setText("");
Zahlen1.clear();
}
if(e.getSource()==bdel)
{
String s=t.getText();
t.setText("");
for(int i=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));
}
}
public static void main(String...s)
{
new TR2();
}
}
Ich hoffe ihr könnt mir weiterhelfen.
Mit freundlichen Grüßen
MaGuS11
Zuletzt bearbeitet von einem Moderator: