如何在Java中运行程序后保持GUI运行
我是一名高中生,正在做一个项目,将YouTube链接的视频转换成MP3并下载。这个程序打开一个GUI,有一个按钮,可以将链接中的视频转换成MP3并下载。但是,在链接转换后,我希望它打开另一个GUI,但程序刚刚结束。有没有办法让程序在下载过程后继续运行如何在Java中运行程序后保持GUI运行,java,user-interface,mp3,converters,youtube-dl,Java,User Interface,Mp3,Converters,Youtube Dl,我是一名高中生,正在做一个项目,将YouTube链接的视频转换成MP3并下载。这个程序打开一个GUI,有一个按钮,可以将链接中的视频转换成MP3并下载。但是,在链接转换后,我希望它打开另一个GUI,但程序刚刚结束。有没有办法让程序在下载过程后继续运行 package com.company; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.Acti
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);
}
}
GUI应该一直在运行—您不应该关闭它或尝试创建新的JFrame 相反,转换视频的任务应该在单独的线程中运行,这样就不会阻止GUI响应事件 一个简单的方法是使用
SwingWorker
。阅读Swing tutorail上的部分,了解更多信息和示例
其他意见: