Hi leute
ich hab ein JFrame mit ActionListener im GridBagLayout mit drei Buttons.
Nachher möchte ich in der Methode actionPerformed() herausfinden welcher Button gedrückt wurde, wie mach ich das?
Also damit ich weiß, welcher der drei Buttons gedrückt wurde
Ich geb euch auch gleich den Code
ich hab ein JFrame mit ActionListener im GridBagLayout mit drei Buttons.
Nachher möchte ich in der Methode actionPerformed() herausfinden welcher Button gedrückt wurde, wie mach ich das?
Also damit ich weiß, welcher der drei Buttons gedrückt wurde
Ich geb euch auch gleich den Code
Code:
class Options extends JFrame implements ActionListener{
JLabel spieler1 = TicTacToe.getSpieler1();
JLabel spieler2 = TicTacToe.getSpieler2();
Options(){
super("Optionen");
setSize(290, 110);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setLayout(gbl);
buildConstraints(gbc, 0, 0, 1, 1, 80, 100);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(spieler1, gbc);
panel.add(spieler1);
buildConstraints(gbc, 1, 0, 1, 1, 20, 100);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
JButton button1 = new JButton("Ändern?");
gbl.setConstraints(button1, gbc);
panel.add(button1);
buildConstraints(gbc, 0, 1, 1, 1, 0, 100);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(spieler2, gbc);
panel.add(spieler2);
buildConstraints(gbc, 1, 1, 1, 1, 0, 100);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
JButton button2 = new JButton("Ändern?");
gbl.setConstraints(button2, gbc);
panel.add(button2);
buildConstraints(gbc, 0, 2, 2, 1, 0, 100);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
JButton okbutton = new JButton("OK");
gbl.setConstraints(okbutton, gbc);
panel.add(okbutton);
button1.addActionListener(this);
button2.addActionListener(this);
okbutton.addActionListener(this);
setContentPane(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
}