java - 为什么打开网页的 JButton 仅在第一次单击时起作用,然后被停用?

标签 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();
}

最佳答案

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

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,它们提供了重要且有用的信息,可以帮助解决问题

关于java - 为什么打开网页的 JButton 仅在第一次单击时起作用,然后被停用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728360/

相关文章:

html - URL 框架打破了响应式网站

javascript - 替换 URL 中 id 的值

java - 不幸的是,MyApp 已停止。我该如何解决这个问题?

java - UDP 组播是正确的选择吗?

java - 在 Java 中使用已发布的 MouseEvent 解决问题

具有多个操作的 Java 键绑定(bind)

Java 垃圾收集器 A 类 -> B 类 -> C 类 -> B 类和循环引用

java - 计算斐波那契数列中终止条件的执行情况

java - 在 JButton 上绘制图形

java - Java 中的 URLDecoder 在 "São Paulo"处阻塞