R
Ruby
Gast
hallo leute hab da ne frage, hab da einen Quellcode aber wenn ich auf Run gehe sind meine Buttons weg obwohl die doch auf Visible true stehen? Hoffe ihr könnt mir helfen. Vielen Danke im voraus.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Spielfeld extends JFrame {
private JButton button1, button2;
Spielfeld() {
super();
this.getContentPane().setLayout(null);
button1 = new JButton("Klick mich!");
button1.setSize(130, 40);
button1.setLocation(10, 10);
button1.setVisible(true);
button2 = new JButton("oder mich");
button2.setSize(130, 40);
button2.setLocation(150, 10);
button2.setVisible(true);
//
this.getContentPane().add(button1);
this.getContentPane().add(button2);
// Default-Knopf
this.getRootPane().setDefaultButton(button2);
// Events
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new KnopfAktionen());
button2.addActionListener(new KnopfAktionen());
button1.getLocation( );
}
class KnopfAktionen implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == button1.getText()) {
button1.setText("Hallo");
}
if (e.getActionCommand() == button2.getText()) {
button2.setText("Danke!");
}
}
}
public static void main(String[] args) {
Spielfeld fenster = new Spielfeld();
fenster.setSize(525, 300);
fenster.setLocation(200, 100);
fenster.setVisible(true);
fenster.getContentPane().setBackground(Color.yellow);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(100,100,100,100);
}
}