首页 > 解决方案 > 如何在 Java 中运行程序后保持 GUI 运行

问题描述

我是一名高中生,正在从事一个项目,该项目会将视频从 YouTube 链接转换为 MP3 并下载。该程序打开一个 GUI,并有一个按钮,可将视频从链接转换为 MP3 并下载。但是,在链接转换后,我希望它打开另一个 GUI,但程序刚刚结束。有没有办法让程序在下载过程后继续运行?

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class GUI extends JFrame {

    private static JTextField userLink;
    private static JFrame frameOne;
    private static JPanel panelOne;
    private static JButton convertB;
    private static JLabel titleOne;
    private static JLabel name;
    public String youtubeLink;
    public static JLabel titleTwo;
    public static JLabel info;

    public GUI() {

        panelOne = new JPanel();
        frameOne = new JFrame();
        frameOne.setSize(500, 300);
        frameOne.setBackground(Color.CYAN);

        frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frameOne.setVisible(true);
        frameOne.add(panelOne);
        panelOne.setLayout(null);

        titleOne = new JLabel("Welcome to the Youtube");      //Title for first panel
        titleOne.setBounds(55, 10, 500, 30);
        titleOne.setFont(new Font("Courier", Font.BOLD, 30));
        panelOne.add(titleOne);

        titleTwo = new JLabel("to MP3 Converter!");      //Title for first panel
        titleTwo.setBounds(100, 40, 500, 30);
        titleTwo.setFont(new Font("Courier", Font.BOLD, 30));
        panelOne.add(titleTwo);

        name = new JLabel("By Zeid Akel");
        name.setBounds(220, 80, 80, 25);
        panelOne.add(name);

        convertB = new JButton("Convert and Download");                //Convert and 
        convertB.setBounds(170, 220, 175, 50);
        convertB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                youtubeLink = userLink.getText();
                System.out.println(youtubeLink);

                String s;
                String l = "youtube-dl -x --audio-format mp3 ";
                String rl = l + youtubeLink;

                File f = new File("/Users/zeidakel/YoutubeVideos");     //Location where 

                if (!f.exists()) {
                    System.out.println("Download Location does not exist");     //Check if 

                } else {

                    try {

                        Process p = Runtime.getRuntime().exec(rl); //Code put int terminal

                        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                        System.out.println("Output:\n");
                        while ((s = stdInput.readLine()) != null) {
                            System.out.println(s);
                        }

                        System.out.println("Errors:\n");
                        while ((s = stdError.readLine()) != null) {
                            System.out.println(s);
                        }

                        frameOne.setVisible(false);
                        new GUI2();

                        System.exit(0);
                    } catch (IOException e1) {

                    }
                }

            }
        });
        panelOne.add(convertB);

        userLink = new JTextField();                         //Input area for youtube link
        userLink.setBounds(135, 190, 250, 25);
        panelOne.add(userLink);

        info = new JLabel("Paste your link here:");
        info.setBounds(140, 170, 250, 25);
        panelOne.add(info);

        frameOne.setVisible(true);

    }

    }

标签: javauser-interfacemp3convertersyoutube-dl

解决方案


GUI 应该一直在运行,你永远不应该关闭它或尝试创建一个新的 JFrame。

相反,转换视频的任务应该在单独的线程中运行,因此您不会阻止 GUI 响应事件。

一个简单的方法是使用SwingWorker. 阅读 Swing 教程中关于并发的部分以获取更多信息和示例。

其他的建议:

  1. 不应将 Swing 组件定义为静态变量。这不是 static 关键字的用途。

  2. 不要使用空布局和 setBounds()。Swing 旨在与布局管理器一起使用。


推荐阅读