0

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

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

}

4

1 回答 1

0

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

将整个 if/else 块放在“输出”按钮的 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);

    }
于 2013-10-02T02:41:57.487 回答