java - 如何在 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);
}
}
解决方案
推荐阅读
- python - 验证用户输入在两个数字之间
- python - 数组列表在 Python 中不断返回相同的值
- ruby-on-rails - Rails、ActiveAdmin、Cancancan:如何防止访问/admin 中的资源?
- gpu - Vulkan计算着色器的最大线程数
- python - @property 和 @propertyName.settter 之间有区别吗
- python - 为什么我没有自动使用 Python 在 Datalake gen2 中创建文件系统
- php - 数组中的默认选项
- java - 我在运行 java 中的 if else 程序时遇到问题,如果 if 语句为 false,则不会打印 else 语句
- makefile - 合并极其相似的 Make 规则
- python - 使用匀称几何扩展旋转的矩形