Hallo, ich habe das Problem, dass ich eine ButtonGroup mit RadioButtons habe und wenn man einen anderen Button auswählt soll eine Funktion ausgeführt werden. Habe dazu in nem Tutorial was gelesen und mir ein Beispiel angeguckt aber wenn ich es so mache wie dort bekomme ich ein Problem. Hier erstmal der betroffene Code:
Das Problem ist, dass wenn ich einen Actionlistener hinzufügen will mittels:
(so war es im Beispiel) dann bekomme ich diesen Fehler:
Habe aber sonst auch keine Ahnung wie ich den Listener "anschließen" soll.
Code:
public class GUI implements ActionListener{
static ButtonGroup g = new ButtonGroup();
public static void main(String[] args) {
JFrame window = new JFrame( "Test" );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setSize( 800, 600 );
window.add(drawRadioButtons();
window.setVisible( true );
}
private static JPanel drawRadioButtons (){
JPanel rabattgruppenPanel = new JPanel();
rabattgruppenPanel.add(new JLabel("Händlerrabattgruppe:"));
JRadioButton rb1 = new JRadioButton( "I [1-9St/a] 40%" );
rb1.setActionCommand("1");
JRadioButton rb2 = new JRadioButton( "II [ab 10St/a] 45%" );
rb2.setActionCommand("2");
JRadioButton rb3 = new JRadioButton( "III [ab 15St/a] 48%" );
rb3.setActionCommand("3");
JRadioButton rb4 = new JRadioButton( "IV [ab 25St/a] 50%" );
rb4.setActionCommand("4");
g.add( rb1 ); g.add( rb2 ); g.add( rb3 ); g.add( rb4 );
rb1.setSelected(true);
rabattgruppenPanel.add(rb1); rabattgruppenPanel.add(rb2); rabattgruppenPanel.add(rb3); rabattgruppenPanel.add(rb4);
return rabattgruppenPanel;
}
Das Problem ist, dass wenn ich einen Actionlistener hinzufügen will mittels:
Code:
JRadioButton rb1 = new JRadioButton( "I [1-9St/a] 40%" );
rb1.setActionCommand("1");
rb1.addActionListener(this);
Cannot use this in a static context
Habe aber sonst auch keine Ahnung wie ich den Listener "anschließen" soll.