Hallo,
ich schreibe ein klein GUI Programm mit AWT aber meine actionPerformed method wird nicht erkannt oder wird nicht überschrieben.
Ich weis es nicht woran es liegen kann. Können Sie mir bitte helfen?
Danke im Voraus.
Diese Klasse rufe ich so
ich schreibe ein klein GUI Programm mit AWT aber meine actionPerformed method wird nicht erkannt oder wird nicht überschrieben.
Ich weis es nicht woran es liegen kann. Können Sie mir bitte helfen?
Danke im Voraus.
Java:
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyGUI extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
GridBagLayout gridbag;
GridBagConstraints gbc;
TextField textfield;
Button addition; // = new Button();
Button b00; // = new Button("0");
Button b01; // = new Button("1");
Button b02; // = new Button("2");
public MyGUI() {
this.gridbag = new GridBagLayout();
this.gbc = new GridBagConstraints();
setLayout(gridbag);
this.textfield = new TextField();
textfield.addActionListener(this);
this.addition = new Button("add");
addition.addActionListener(this);
this.b00 = new Button("0");
b00.addActionListener(this);
this.b01 = new Button("1");
b01.addActionListener(this);
this.b02 = new Button("2");
b02.addActionListener(this);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 4;
gbc.gridheight = 1;
add(textfield, gbc);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
makebutton("0", gridbag, gbc);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
makebutton("1", gridbag, gbc);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
makebutton("2", gridbag, gbc);
gbc.insets = new Insets(1, 1, 1, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
makebutton("add", gridbag, gbc);
//Zuständig um das Fenster zumachen
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
//setSize(300,300);
// setVisible(true);
}
protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
//Object o = e.getSource();
textfield.setText("Default");
if(e.getSource() == addition){
textfield.setText("Adition Geklickt");
System.out.println("Hallo");
}else {
System.out.println("Hallo");
}
}
}
Diese Klasse rufe ich so
Java:
public class MyGuITest {
public static void main(String[] args) {
MyGUI rechner = new MyGUI();
rechner.setSize(300,300);
rechner.setVisible(true);
}
}