Hallo, ich versuche schon seit paar Tagen bei meinem Raster von Button ein Punkt erscheinen zu lassen, also das wenn man einen drückt ein Punkt auf ihm erscheint, ich selber bin neu in Java Gui und kenne mich da kaum bis gar nicht aus. Würde mcih freuen wenn mir jemand helfen könnte
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Üben extends JFrame{
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button10;
private JButton button11;
private JButton button12;
private JButton button13;
private JButton button14;
private JButton button15;
private JButton button16;
private JButton button17;
private JButton button18;
private JButton button19;
private JButton button20;
private JButton button21;
private JButton button22;
private JButton button23;
private JButton button24;
private JButton button25;
private JButton button26;
private JButton button27;
private JButton button28;
private JButton button29;
private JButton button30;
private JButton button31;
private JButton button32;
private JButton button33;
private JButton button34;
private JButton button35;
private JButton button36;
private JPanel panelButton;
private JLabel oben;
private JLabel anzeige;
public Üben(){
super("Raster");
setSize(1000,1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(10,10));
button1 = new JButton(" ");
button2 = new JButton(" ");
button3 = new JButton(" ");
button4 = new JButton(" ");
button5 = new JButton(" ");
button6 = new JButton(" ");
button7 = new JButton(" ");
button8 = new JButton(" ");
button9 = new JButton(" ");
button10 = new JButton(" ");
button11 = new JButton(" ");
button12 = new JButton(" ");
button13 = new JButton(" ");
button14 = new JButton(" ");
button15 = new JButton(" ");
button16 = new JButton(" ");
button17 = new JButton(" ");
button18 = new JButton(" ");
button19 = new JButton(" ");
button20 = new JButton(" ");
button21 = new JButton(" ");
button22 = new JButton(" ");
button23 = new JButton(" ");
button24 = new JButton(" ");
button25 = new JButton(" ");
button26 = new JButton(" ");
button27 = new JButton(" ");
button28 = new JButton(" ");
button29 = new JButton(" ");
button30 = new JButton(" ");
button31 = new JButton(" ");
button32 = new JButton(" ");
button33 = new JButton(" ");
button34 = new JButton(" ");
button35 = new JButton(" ");
button36 = new JButton(" ");
//Panels erzeugen auf einem GridLayout
panelButton = new JPanel(new GridLayout(0,6));
//Auf Panel Buttons packen
panelButton.add(button1);
panelButton.add(button2);
panelButton.add(button3);
panelButton.add(button4);
panelButton.add(button5);
panelButton.add(button6);
panelButton.add(button7);
panelButton.add(button8);
panelButton.add(button9);
panelButton.add(button10);
panelButton.add(button11);
panelButton.add(button12);
panelButton.add(button13);
panelButton.add(button14);
panelButton.add(button15);
panelButton.add(button16);
panelButton.add(button17);
panelButton.add(button18);
panelButton.add(button19);
panelButton.add(button20);
panelButton.add(button21);
panelButton.add(button22);
panelButton.add(button23);
panelButton.add(button24);
panelButton.add(button25);
panelButton.add(button26);
panelButton.add(button27);
panelButton.add(button28);
panelButton.add(button29);
panelButton.add(button30);
panelButton.add(button31);
panelButton.add(button32);
panelButton.add(button33);
panelButton.add(button34);
panelButton.add(button35);
panelButton.add(button36);
//Listener für Buttons
addButtonListener(button1);
addButtonListener(button2);
addButtonListener(button3);
addButtonListener(button4);
addButtonListener(button5);
addButtonListener(button6);
addButtonListener(button7);
addButtonListener(button8);
addButtonListener(button9);
addButtonListener(button10);
addButtonListener(button11);
addButtonListener(button12);
addButtonListener(button13);
addButtonListener(button14);
addButtonListener(button15);
addButtonListener(button16);
addButtonListener(button17);
addButtonListener(button18);
addButtonListener(button19);
addButtonListener(button20);
addButtonListener(button21);
addButtonListener(button22);
addButtonListener(button23);
addButtonListener(button24);
addButtonListener(button25);
addButtonListener(button26);
addButtonListener(button27);
addButtonListener(button28);
addButtonListener(button29);
addButtonListener(button30);
addButtonListener(button31);
addButtonListener(button32);
addButtonListener(button33);
addButtonListener(button34);
addButtonListener(button35);
addButtonListener(button36);
//Labels erzeugen
oben = new JLabel("Layout Test");
//Label zentrieren
oben.setHorizontalAlignment(JLabel.CENTER);
anzeige = new JLabel("Button klicken");
//Labels auf Frame packen (direkt auf das BorderLayout)
getContentPane().add(BorderLayout.NORTH, oben);
getContentPane().add(anzeige);
//Panels auf Frame packen (das panelButton hat ein GridLayout, dass jetzt in den WestBereich des BorderLayouts kommt)
getContentPane().add(BorderLayout.WEST, panelButton);
pack();
setVisible(true);
}
public static void main(String[] args){
Üben g = new Üben();
}
private void addButtonListener(JButton b){
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
eingabe(ae.getActionCommand());
}
});
}
private void eingabe(String a){
anzeige.setText(a);
}
}