Hey, ich wollte bei meinem Taschenrechner ein paar Funktionen in eine Combobox packen, allerdings funktioniert das bei mir nicht so richtig.
Bis hier bin ich gekommen:
Jetzt sagt man mir allerdings, dass die Methode .getSelectedIndex nicht existiert.
Ich weiß zwar, dass ich den Index holen muss, aber ich habe keine Ahnung, wie ich das hier einbauen muss ..
Hoffe, mir kann jemand helfen
Bis hier bin ich gekommen:
Java:
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.Label.*;
import java.lang.Object.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class Taschenrechner extends JFrame implements ActionListener, FocusListener {
private double x;
private double y;
private int counter=0, j=0, index=-1;
private double ergebnis;
JTextField tEingabe1;
JTextField tEingabe2;
JTextField tAusgabe;
JTextArea tRechnung;
JLabel lCopyright;
JLabel lx;
JLabel ly;
JComboBox cBCopy;
JButton [] btn = new JButton[18];
String [] buttonName = {"plus" , "minus" , "mal" , "geteilt" , "Wurzel" , "x^2" , "x^y" , "e^x" , "10^x" , "ln(x)" , "log(x)" , "sin(x)" , "cos(x)" , "tan(x)" , "ggT(x,y)" , "kgV(x,y)" , "PI" , "Ans" };
String [] cBox = {"x kopieren" , "y kopieren" , "Ergebnis kopieren" };
public Taschenrechner() {
JFrame jFr = new JFrame();
setSize(600,600);
setBounds(212,84,600,600);
setLayout(null);
setResizable(false);
setDefaultCloseOperation(3);
setTitle("Taschenrechner");
tEingabe1= new JTextField(j+"");
tEingabe1.setBounds(30,50,150,60);
add(tEingabe1);
tEingabe1.addFocusListener(this);
tEingabe2=new JTextField(j+"");
tEingabe2.setBounds(410,50,150,60);
add(tEingabe2);
tEingabe2.addFocusListener(this);
tAusgabe=new JTextField("Ergebnis");
tAusgabe.setBounds(30,110,530,80);
add(tAusgabe);
tAusgabe.addFocusListener(this);
tRechnung=new JTextArea("Rechnung");
tRechnung.setBounds(180,50,230,60);
add(tRechnung);
tRechnung.setLineWrap(true);
lCopyright=new JLabel("Calc(this) v.1.x Copyright © by Jats");
lCopyright.setBounds(185,535,300,20);
lCopyright.setFont(new Font ("Verdana", Font.PLAIN, 12));
add(lCopyright);
lx=new JLabel("x:");
lx.setBounds(30,10,150,60);
add(lx);
ly=new JLabel("y:");
ly.setBounds(410,10,150,60);
add(ly);
cBCopy = new JComboBox(cBox);
cBCopy.setBounds(230,250,120,30);
add(cBCopy);
cBCopy.addActionListener(this);
for (int i=0;i<18;i++) {
btn[i] = new JButton("");
}
for (int i=0;i<18;i++) { //THX @ Niklas Schultheiß
if(i%6==0 && i!=0) {
counter++; j++;
}
btn[i].setText(buttonName[i]);
btn[i].setBounds(100+130*counter,300+i*40-j*40*6,120,30);
add(btn[i]);
btn[i].addActionListener(this);
}
setVisible(true);
}
@Override
public void focusGained(FocusEvent e) {
if(e.getSource()==tEingabe1 && tEingabe1.getText().equals("0")){
tEingabe1.setText("");
}
if(e.getSource()==tEingabe2 && tEingabe2.getText().equals("0")){
tEingabe2.setText("");
}
else {
}
}
@Override
public void focusLost(FocusEvent e) {
}
public void addieren() {
ergebnis = x+y;
tAusgabe.setText(ergebnis+"");
tRechnung.setText("Rechnung: \n"+x+"+"+y+"="+ergebnis);
}
public void substrahieren() {
ergebnis = x-y;
tAusgabe.setText(ergebnis+"");
}
public void multiplizieren() {
ergebnis = x*y;
tAusgabe.setText(ergebnis+"");
}
public void dividieren() {
ergebnis = x/y;
tAusgabe.setText(ergebnis+"");
}
public void wurzel() {
ergebnis = Math.sqrt(x);
tAusgabe.setText(ergebnis+"");
}
public void quadrieren() {
ergebnis = Math.pow(x,2);
tAusgabe.setText(ergebnis+"");
}
public void potenzieren() {
ergebnis = Math.pow(x,y);
tAusgabe.setText(ergebnis+"");
}
public void ehochx() {
ergebnis = Math.pow(Math.E,y);
tAusgabe.setText(ergebnis+"");
}
public void ZehnHochx() {
ergebnis = Math.pow(10,x);
tAusgabe.setText(ergebnis+"");
}
public void ln() {
ergebnis = Math.log(x);
tAusgabe.setText(ergebnis+"");
}
public void log() {
ergebnis = Math.log(x)/Math.log(y);
tAusgabe.setText(ergebnis+"");
}
public void sin() {
ergebnis = Math.sin(x);
tAusgabe.setText(ergebnis+"");
}
public void cos() {
ergebnis = Math.cos(x);
tAusgabe.setText(ergebnis+"");
}
public void tan() {
ergebnis = Math.tan(x);
tAusgabe.setText(ergebnis+"");
}
public double ggT(double zahl1, double zahl2) {
double r;
do {
r = x%y;
x = y;
y = r;
}
while(y!=0);
ergebnis = x;
tAusgabe.setText(ergebnis+"");
return x;
}
public void kgV() {
double c = x;
double d = y;
tAusgabe.setText(x*y/ggT(c,d)+"");
}
public void answer() {
tEingabe1.setText(tAusgabe.getText()+"");
}
public void actionPerformed(ActionEvent e) {
x = new Double(tEingabe1.getText()).doubleValue();
y = new Double(tEingabe2.getText()).doubleValue();
for(int i=0;i<3;i++) {
counter++;
}
if (e.getSource()==btn[0]) {
addieren();
}
if (e.getSource()==btn[1]) {
substrahieren();
}
if (e.getSource()==btn[2]) {
multiplizieren();
}
if (e.getSource()==btn[3]) {
dividieren();
}
if (e.getSource()==btn[4]) {
wurzel();
}
if (e.getSource()==btn[5]) {
quadrieren();
}
if (e.getSource()==btn[6]) {
potenzieren();
}
if (e.getSource()==btn[7]) {
ehochx();
}
if (e.getSource()==btn[8]) {
ZehnHochx();
}
if (e.getSource()==btn[9]) {
ln();
}
if (e.getSource()==btn[10]) {
log();
}
if (e.getSource()==btn[11]) {
sin();
}
if (e.getSource()==btn[12]) {
cos();
}
if (e.getSource()==btn[13]) {
tan();
}
if (e.getSource()==btn[14]) {
ggT(x,y);
}
if (e.getSource()==btn[17]) {
answer();
}
counter=-1;
Object o = e.getSource();
if(o==cBox)
{
counter=cBox.getSelectedIndex();
}
index=count;
cBox.removeActionListener(cBox.getActionListeners()[0]);
cBox.setSelectedIndex(index);
if(o.getSource()==cBox[0]) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(tEingabe1.getText()), null);
}
if(o.getSource()==cBox[1]) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(tEingabe2.getText()), null);
}
if(o.getSource()==cBox[2]) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(tAusgabe.getText()), null);
}
}
}
Jetzt sagt man mir allerdings, dass die Methode .getSelectedIndex nicht existiert.
Ich weiß zwar, dass ich den Index holen muss, aber ich habe keine Ahnung, wie ich das hier einbauen muss ..
Hoffe, mir kann jemand helfen