Hallo !!
Ich brauche wieder mal euren Rat. Ich will mir nen Taschenrechner schreiben und bin auch schon mit der Oberfläche fertig. Mein nächster Schritt soll sein, dass, wenn ich auf einen Button klicke, die Zahl im Textfeld erscheint, die auf dem geklickten Button steht.
Compiler meckert:
1. non-static variable this cannot be referenced from a static context
2. cannot resolve symbol tf
Ich weiß schon:
er erkennt die variable tf nicht...macht sinn. aber wo soll ich die deklarieren ??
und mit static raff ich echt net. was soll static sein ??
vielen dank...wünsche frohe ostern
Ich brauche wieder mal euren Rat. Ich will mir nen Taschenrechner schreiben und bin auch schon mit der Oberfläche fertig. Mein nächster Schritt soll sein, dass, wenn ich auf einen Button klicke, die Zahl im Textfeld erscheint, die auf dem geklickten Button steht.
Code:
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener
{
public static void main(String[] args)
{
createWindow();
}
//---------------------Creates Window within Properties-------------------------
static void createWindow()
{
Calculator window = new Calculator();
window.setSize(200,250);
window.setLayout(null);
window.setBackground(java.awt.SystemColor.black);
Panel pnorth = new Panel();
Panel psouth = new Panel();
TextField tf = new TextField();
Button b = null;
pnorth.setLocation(10,35);
pnorth.setSize(180,20);
psouth.setLocation(10,60);
psouth.setSize(180,180);
psouth.setLayout(new GridLayout(4,4));
pnorth.setLayout(new BorderLayout());
String[] buttonSigns = {"9","8","7","/","4","5","6","*","1","2","3","-","c",
"0","=","+"};
for(int i=0; i<buttonSigns.length; i++)
{
psouth.add(b = new Button(buttonSigns[i]));
b.addActionListener(this);
}
pnorth.add(tf,BorderLayout.CENTER);
window.add(pnorth);
window.add(psouth);
window.setVisible(true);
}
//-----------------cause a WindowClosing----------------------------------------
Calculator()
{
super(" CalcIt 1.0 ");
enableEvents(WindowEvent.WINDOW_CLOSING);
}
protected void processWindowEvent(WindowEvent cls)
{
if(cls.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
}
//----------------Event Handler-------------------------------------------------
public void actionPerformed(ActionEvent e)
{
String str = ((Button) e.getSource()).getLabel();
char arg0 = str.charAt(0);
switch(arg0)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
tf.setText(str);
}
}
}
Compiler meckert:
1. non-static variable this cannot be referenced from a static context
2. cannot resolve symbol tf
Ich weiß schon:
er erkennt die variable tf nicht...macht sinn. aber wo soll ich die deklarieren ??
und mit static raff ich echt net. was soll static sein ??
vielen dank...wünsche frohe ostern