为什么打开网页的JButton只在第一次点击一个然后被取消激活时才能工作?

时间:2014-05-19 00:55:21

标签: java swing url jbutton

我有一套JButton,每个JButton都会打开一个单独的YouTube视频网页。首次运行程序时,我可以单击任何一个按钮并进入视频页面。当我尝试通过点击按钮获得另一个视频页面时,它不起作用 - 事实上,所有按钮都被停用。无论我是否关闭视频网页都是如此。

如何激活所有按钮?提前致谢。

这是代码供参考。按钮链接和标签从文本文件中填充。

//import statements

public class VideoRecord extends JFrame {

private File videoRecordFile;

public VideoRecord() throws FileNotFoundException {
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new GridLayout(2,2));
    setSize(new Dimension(500, 500));
    videoRecordFile = new File("videorecord.txt");      
    getButtons();
    pack();
}

public void getButtons() throws FileNotFoundException {
    Scanner input = new Scanner(videoRecordFile);
    while (input.hasNextLine()) {
        Scanner lineInput = new Scanner(input.nextLine());
        while (lineInput.hasNext()) {               
            final String urlString = lineInput.next();
            String buttonText = lineInput.next();
            JButton btn = new JButton(buttonText);
            add(btn);
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                    
                    try {
                        URL videoURL = new URL(urlString);
                        URLConnection videoConnection = videoURL.openConnection();
                        videoConnection.connect();
                        openWebpage(videoURL);
                    } 
                    catch (MalformedURLException mue) {} 
                    catch (IOException ioe) {}
                    setEnabled(false);
                }
            });
        }
    }
}

public static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void openWebpage(URL url) {
    try {
        openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws FileNotFoundException {
    VideoRecord vr = new VideoRecord();
}

}

1 个答案:

答案 0 :(得分:4)

花一点时间查看你的代码...

btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {                    
                try {
                    URL videoURL = new URL(urlString);
                    URLConnection videoConnection = videoURL.openConnection();
                    videoConnection.connect();
                    openWebpage(videoURL);
                } 
                catch (MalformedURLException mue) {} 
                catch (IOException ioe) {}
                setEnabled(false);
            }
        });

当您点击按钮时,请拨打setEnabled(false); ...

这实际上禁用了框架,而不是单击的按钮......

  1. 尝试使用((JButton)e.getSource()).setEnabled(false)代替
  2. 不要盲目地抛弃你Exception,他们提供有助于解决问题的重要且有用的信息