简体   繁体   中英

Java actionListener in another class won't open the window

I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time. I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.

Here is the class where I created the window:

import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;

public class window extends JFrame{
    public JRadioButton radio1= new JRadioButton("Salam1");
    public JRadioButton radio2= new JRadioButton("Salam2");
    public JRadioButton radio3= new JRadioButton("Salam3");

    public window(){
        super("Title");
        setLayout(new FlowLayout());

        add(radio1);
        add(radio2);
        add(radio3);

        action acc =  new action();
        radio1.addActionListener(acc);
        radio2.addActionListener(acc);
        radio3.addActionListener(acc); 
    }
}

And this is my ActionListener Class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class action implements ActionListener{
    window sarma =  new window();

    public void actionPerformed(ActionEvent event){
        if(sarma.radio1.isSelected()){
            sarma.radio2.setSelected(false);
            sarma.radio3.setSelected(false);
        }
        if(sarma.radio2.isSelected()){
            sarma.radio1.setSelected(false);
            sarma.radio3.setSelected(false);
        }
        if(sarma.radio3.isSelected()){
            sarma.radio2.setSelected(false);
            sarma.radio1.setSelected(false);
        }
    }
}

The main class

    import javax.swing.JFrame;
    public class first{
    public static void main(String args[]) {
        window salam = new window();
        salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        salam.setSize(500,150);
        salam.setResizable(false);
        salam.setVisible(true);
    }
}

After I created the window object (named sarma) in the action class, the window won't open when I try to run the program. So, how could I make this program work?

The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by DG).

But the action listener is not needed. The effect can be achieved using a ButtonGroup , like this:

import javax.swing.*;
import java.awt.*;

public class RadioButtonWindow extends JFrame{
    public JRadioButton radio1= new JRadioButton("Salam1");
    public JRadioButton radio2= new JRadioButton("Salam2");
    public JRadioButton radio3= new JRadioButton("Salam3");

    public RadioButtonWindow(){
        super("Title");
        setLayout(new FlowLayout());

        add(radio1);
        add(radio2);
        add(radio3);

        // Only one button in this group can be selected at a time!
        ButtonGroup bg = new ButtonGroup();
        bg.add(radio1);
        bg.add(radio2);
        bg.add(radio3);
    }

   public static void main(String args[]) {
        RadioButtonWindow  salam = new RadioButtonWindow ();
        salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Wrong way to size a GUI!
        //salam.setSize(500,150);
        salam.setResizable(false);
        // Correct way to size a GUI
        salam.pack();
        salam.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM