单击按钮时给出链接参数

时间:2014-09-17 23:12:40

标签: java swing button rss actionlistener

我有10个不同的按钮,我希望每个按钮链接到不同的RSS风格的网页,获取RSS提要,并将其打印到我的控制台。

我有:

`public static void rssReader() throws IOException {

    URL url = new URL("http://www.npr.org/rss/rss.php?id=1001");

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}`

但是,根据我点击的按钮而不是代码中显示的静态URL,我希望URL不同。

这是我的buttonListener类:

public class MyButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {

    JButton source = (JButton) e.getSource();
    JOptionPane.showMessageDialog(source, source.getText() + " button has been pressed");
    if(source.getText().equals("News Headlines")) {
        try {
            rssReader.rssReader();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

  } 
}

0 个答案:

没有答案
相关问题