在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,30);
        titleTwo.setFont(new Font("Courier",30));
        panelOne.add(titleTwo);

        name = new JLabel("By Zeid Akel");
        name.setBounds(220,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,25);
        panelOne.add(info);

        frameOne.setVisible(true);

    }

    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)