jRadioButton的isSelected方法不起作用

时间:2012-12-04 19:39:50

标签: user-interface jradiobutton

我的isSelected方法的单选按钮不起作用,即使我在运行程序时选择它们,我是java gui编码的新手,所以plz解释说,我发布了整个gui的类代码。我是我正在使用eclipse swing设计师

 public class gui {

private JFrame frame;
private final ButtonGroup buttonGroup = new ButtonGroup();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                gui window = new gui();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @throws FileNotFoundException 
 */
public gui() throws FileNotFoundException {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @throws FileNotFoundException 
 */
private void initialize() throws FileNotFoundException {
    FileReader reader=new FileReader("C:\\Users\\kkj\\workspace\\javaproject\\src\\Database.txt");
    Scanner in=new Scanner(reader);
    BList Restaurant=new BList();
    BList ATM=new BList();
    BList Hospital=new BList();
    BList Hotels=new BList();
    BList Petrol=new BList();
    final Llist locations=new Llist();
    System.out.println("Loading");

    while(in.hasNextLine())

    {
        BNode bnode=new BNode();
        bnode.name=in.nextLine();
        //System.out.println(bnode.name);
        String type=in.nextLine();
        bnode.loc=in.nextLine();
        if(type.equals("Restaurant"))
        {
            Restaurant.insert(bnode);

        }
        if(type.equals("ATM"))
        {
            ATM.insert(bnode);

        }
        if(type.equals("Hospital"))
        {
            Hospital.insert(bnode);

        }
        if(type.equals("Hotels"))
        {
            Hotels.insert(bnode);

        }
        if(type.equals("Petrol"))
        {
            Petrol.insert(bnode);

        }



    }
    FileReader reader2=new FileReader("C:\\Users\\kkj\\workspace\\javaproject\\src\\locations.txt");
    Scanner inL=new Scanner(reader2);
    int s=0;

    while(inL.hasNextLine())
    {
        LNode loc=new LNode();
        loc.Name=inL.nextLine();
        loc.dist=s++;
        BNode temp;
        temp=Restaurant.head;
        while(temp.next!=null)
            {   if(temp.loc.equals(loc.Name))
                {loc.rest=temp;break;}
                temp=temp.next;
            }



        temp=Hospital.head;
        while(temp.next!=null)
        {   if(temp.loc.equals(loc.Name))
            {loc.Hospital=temp;break;}
            temp=temp.next;
        }

        temp=Hotels.head;
        while(temp.next!=null)
        {   if(temp.loc.equals(loc.Name))
            {loc.Hotels=temp;break;}
            temp=temp.next;
        }
        //loc.hotels=temp;
        temp=ATM.head;
        while(temp.next!=null)
        {   if(temp.loc.equals(loc.Name))
            {loc.ATM=temp;break;}
            temp=temp.next;
        }

        locations.insert(loc);
    }




    System.out.println("Loaded");
    System.out.println(">>>>>>>>>>>Restaurants<<<<<<<<<<<<");
    Restaurant.disp();
    System.out.println(">>>>>>>>>>>Hotels<<<<<<<<<<<<");
    Hotels.disp();
    System.out.println(">>>>>>>>>>>Hospital<<<<<<<<<<<<");
    Hospital.disp();
    System.out.println(">>>>>>>>>>>ATM's<<<<<<<<<<<<");
    ATM.disp();
    System.out.println(">>>>>>>>>>>Locations<<<<<<<<<<<<");
    locations.disp();

    final String curr="Bsk";
    String locin;
    final String typein="Hospital";
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);







    JRadioButton rdbtnBsk = new JRadioButton("Bsk");
    buttonGroup.add(rdbtnBsk);
    rdbtnBsk.setBounds(26, 35, 109, 23);
    frame.getContentPane().add(rdbtnBsk);

    JRadioButton rdbtnKoramangala = new JRadioButton("Koramangala");
    buttonGroup.add(rdbtnKoramangala);
    rdbtnKoramangala.setBounds(26, 72, 109, 23);
    frame.getContentPane().add(rdbtnKoramangala);

    JRadioButton rdbtnMgRoad = new JRadioButton("MG Road");
    buttonGroup.add(rdbtnMgRoad);
    rdbtnMgRoad.setBounds(26, 125, 109, 23);
    frame.getContentPane().add(rdbtnMgRoad);
    if(rdbtnBsk.isSelected())
        {
            locin="Bsk";
        }
    if(rdbtnKoramangala.isSelected())
    {
        locin="Koramangala";
    }
    if(rdbtnMgRoad.isSelected())
    {
        locin="MG Road";
    }
    else System.exit(2);
    final String locinn=locin;

    JButton btnOutput = new JButton("output");
    btnOutput.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            Output out1=new Output();
            System.out.println(">>>>>>>>>>>"+typein+" in "+locinn+"<<<<<<<<<<<<");
            out1.display(locations, locinn, typein,curr);
        }
    });
    btnOutput.setBounds(182, 193, 89, 23);
    frame.getContentPane().add(btnOutput);

        }
private class SwingAction extends AbstractAction {
    public SwingAction() {
        putValue(NAME, "SwingAction");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
    }
}

}

1 个答案:

答案 0 :(得分:0)

我也是Java编程的新手,但这可能有用,对我有用。

将整个if / else块放在“output”按钮的actionPerformed()方法中,如下所示:

JButton btnOutput = new JButton("output");
btnOutput.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) 
    {  if(rdbtnBsk.isSelected())
       {
         locin="Bsk";
       }
       else if(rdbtnKoramangala.isSelected())
       {
         locin="Koramangala";
       }
       else if(rdbtnMgRoad.isSelected())
       {
         locin="MG Road";
       }

       final String locinn=locin;
       Output out1=new Output();
       System.out.println(">>>>>>>>>>>"+typein+" in "+locinn+"<<<<<<<<<<<<");
       out1.display(locations, locinn, typein,curr);
    }
});
btnOutput.setBounds(182, 193, 89, 23);
frame.getContentPane().add(btnOutput);

    }