2012-12-04 36 views
0

我isSelected方法的单选按钮不工作,即使我选择它们当我运行该程序,我是新的Java gui编码所以PLZ解释根据,我张贴整个GUI的类code.And我使用Eclipse的摆动设计师isRadioButton isSelected方法不工作

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) { 
    } 
} 

}

回答

0

我也是新来的Java编程,但是这可能会奏效,它为我工作。

把整个如果 “输出” 按钮的actionPerformed()方法内/ else块,像这样:

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); 

    }