Hey Leute, ich mache einen grafischen taschenrechner als Jahresprojekt. Morgen soll ich ein Teil des Programms vorzeigen können. Hier ist es:
Bis morgen soll der taschenrechner die grundrechenarten draufhaben (+, -, *, / ). Aber es will einfach nicht klappen.
Es ist vlt nicht ganz richtig von mir, aber könnt ihr mir sagen was ich tun muss, damit es funktioniert?? Weil ich hab wirklich schon viel probiert aber, nun, Programmieren ist jetzt nich unbedingt meine große stärke...
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* description
*
* @version 1.0 from 03.12.2011
* @Cyrus Zeller
*/
public class GTR extends JFrame
{
// Anfang Attribute
private JPanel jPanel1 = new JPanel();
private JLabel display1 = new JLabel();
private JButton Null = new JButton();
private JButton eins = new JButton();
private JButton zwei = new JButton();
private JButton drei = new JButton();
private JButton vier = new JButton();
private JButton fuenf = new JButton();
private JButton sechs = new JButton();
private JButton sieben = new JButton();
private JButton acht = new JButton();
private JButton neun = new JButton();
private JButton punkt = new JButton();
private JButton minus = new JButton();
private JButton plus = new JButton();
private JButton mal = new JButton();
private JButton geteilt = new JButton();
private JButton enter = new JButton();
private JButton pi = new JButton();
private JButton hoch2 = new JButton();
private JButton wurzel = new JButton();
private JButton variable = new JButton();
boolean start = true;
private String lastCommand;
private double result;
// Ende Attribute
public GTR (String title)
{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 750;
int frameHeight = 600;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jPanel1.setBounds(16, 416, 686, 144);
jPanel1.setBorder(new javax.swing.border.LineBorder(Color.BLACK));
cp.add(jPanel1);
Null.setBounds(160, 288, 60, 40);
Null.setText("0");
Null.setMargin(new Insets(2, 2, 2, 2));
Null.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Null_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(Null);
eins.setBounds(160, 240, 60, 40);
eins.setText("1");
eins.setMargin(new Insets(2, 2, 2, 2));
eins.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
eins_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(eins);
zwei.setBounds(232, 240, 60, 40);
zwei.setText("2");
zwei.setMargin(new Insets(2, 2, 2, 2));
zwei.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
zwei_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(zwei);
drei.setBounds(304, 240, 60, 40);
drei.setText("3");
drei.setMargin(new Insets(2, 2, 2, 2));
drei.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
drei_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(drei);
vier.setBounds(160, 192, 60, 40);
vier.setText("4");
vier.setMargin(new Insets(2, 2, 2, 2));
vier.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
vier_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(vier);
fuenf.setBounds(232, 192, 60, 40);
fuenf.setText("5");
fuenf.setMargin(new Insets(2, 2, 2, 2));
fuenf.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
fuenf_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(fuenf);
sechs.setBounds(304, 192, 60, 40);
sechs.setText("6");
sechs.setMargin(new Insets(2, 2, 2, 2));
sechs.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
sechs_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(sechs);
sieben.setBounds(160, 144, 60, 40);
sieben.setText("7");
sieben.setMargin(new Insets(2, 2, 2, 2));
sieben.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
sieben_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(sieben);
acht.setBounds(232, 144, 60, 40);
acht.setText("8");
acht.setMargin(new Insets(2, 2, 2, 2));
acht.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
acht_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(acht);
neun.setBounds(304, 144, 60, 40);
neun.setText("9");
neun.setMargin(new Insets(2, 2, 2, 2));
neun.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
neun_ActionPerformed(evt);
String input = evt.getActionCommand();
if (start)
{
start = false;
}
}
});
cp.add(neun);
punkt.setBounds(232, 288, 60, 40);
punkt.setText(".");
punkt.setMargin(new Insets(2, 2, 2, 2));
punkt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
punkt_ActionPerformed(evt);
}
});
cp.add(punkt);
minus.setBounds(376, 192, 60, 40);
minus.setText("-");
minus.setMargin(new Insets(2, 2, 2, 2));
minus.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
minus_ActionPerformed(evt);
String command = evt.getActionCommand();
// Füge den Präfix "-" an den String an wenn
// es sich um den ersten Befehl handelt (negative Zahl)
if (start)
{
if (command.equals("-"))
{
start = false;
}
else
lastCommand = command;
}
else
{
// Berechnung ausführen
lastCommand = command;
start = true;
}
}
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
}
});
cp.add(minus);
plus.setBounds(376, 144, 60, 40);
plus.setText("+");
plus.setMargin(new Insets(2, 2, 2, 2));
plus.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
plus_ActionPerformed(evt);
}
});
cp.add(plus);
mal.setBounds(376, 240, 60, 40);
mal.setText("*");
mal.setMargin(new Insets(2, 2, 2, 2));
mal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
mal_ActionPerformed(evt);
}
});
cp.add(mal);
geteilt.setBounds(376, 288, 60, 40);
geteilt.setText("÷");
geteilt.setMargin(new Insets(2, 2, 2, 2));
geteilt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
geteilt_ActionPerformed(evt);
}
});
cp.add(geteilt);
enter.setBounds(304, 288, 60, 40);
enter.setText("Enter");
enter.setMargin(new Insets(2, 2, 2, 2));
enter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
enter_ActionPerformed(evt);
}
});
cp.add(enter);
pi.setBounds(448, 192, 60, 40);
pi.setText("?");
pi.setMargin(new Insets(2, 2, 2, 2));
pi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
pi_ActionPerformed(evt);
}
});
cp.add(pi);
hoch2.setBounds(448, 240, 60, 40);
hoch2.setText("x²");
hoch2.setMargin(new Insets(2, 2, 2, 2));
hoch2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
hoch2_ActionPerformed(evt);
}
});
cp.add(hoch2);
wurzel.setBounds(448, 288, 60, 40);
wurzel.setText("v");
wurzel.setMargin(new Insets(2, 2, 2, 2));
wurzel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
wurzel_ActionPerformed(evt);
}
});
cp.add(wurzel);
variable.setBounds(448, 144, 60, 40);
variable.setText("x");
variable.setMargin(new Insets(2, 2, 2, 2));
variable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
variable_ActionPerformed(evt);
}
});
cp.add(variable);
display1.setBounds(160, 32, 355, 81);
display1.setText("");
display1.setOpaque(true);
cp.add(display1);
cp.setBackground(new Color(0xB8CFE5));
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Methoden
public void Null_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void eins_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void zwei_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void drei_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void vier_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void fuenf_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void sechs_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void sieben_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void acht_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void neun_ActionPerformed(ActionEvent evt)
{
// TODO hier Quelltext einfügen
}
public void punkt_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void minus_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void plus_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void mal_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void geteilt_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void enter_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void pi_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void hoch2_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void wurzel_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void variable_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
// Ende Methoden
public static void main(String[] args)
{
new GTR("GTR");
}
}
Bis morgen soll der taschenrechner die grundrechenarten draufhaben (+, -, *, / ). Aber es will einfach nicht klappen.
Es ist vlt nicht ganz richtig von mir, aber könnt ihr mir sagen was ich tun muss, damit es funktioniert?? Weil ich hab wirklich schon viel probiert aber, nun, Programmieren ist jetzt nich unbedingt meine große stärke...