Hi,
wie kann ich den "Fehler" beheben?
Und falls euch zufällig noch schlechte programmierter Code auffällt könnt ihr das gerne sagen. Einfach mal ausführen und auf den Button klicken. Dann wisst ihr was ich meine
Danke schonmal im Vorraus
wie kann ich den "Fehler" beheben?
Und falls euch zufällig noch schlechte programmierter Code auffällt könnt ihr das gerne sagen. Einfach mal ausführen und auf den Button klicken. Dann wisst ihr was ich meine
Danke schonmal im Vorraus
Java:
package teste;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainFrame extends JFrame {
private InputPanel input;
private OutputPanel output;
MainFrame(){
super("ButtonTest");
this.setSize(400, 400);
this.setLocationByPlatform(true);
this.setLayout(new GridLayout(3,1));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
input = new InputPanel();
output = new OutputPanel();
this.add(output);
this.add(input);
this.add(new Label("Hallo"));
input.addActionListener(new SoneAktion());
}
private class SoneAktion implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
output.farbe(Color.blue);
output.repaint();
}
}
public static void main(String[]args){
new MainFrame();
}
}
Java:
package teste;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class InputPanel extends JPanel {
private JButton button;
InputPanel(){
this.setLayout(new FlowLayout());
button = new JButton("Change colour");
button.setSize(100, 100);
this.add(button);
}
public void addActionListener(ActionListener al){
button.addActionListener(al);
}
}
Java:
package teste;
import java.awt.*;
import javax.swing.*;
public class OutputPanel extends JPanel {
Color c = Color.white;
OutputPanel(){
this.setLayout(new FlowLayout());
}
public void paintComponent(Graphics g) {
g.setColor(this.c);
g.drawOval(getWidth()/2-10, getHeight()/4, 20, 20);
g.fillOval(getWidth()/2-10, getHeight()/4, 20, 20);
}
public Color farbe(Color c){
this.c = c;
return c;
}
}
Zuletzt bearbeitet: