我有一个将视频转换为音频的应用程序,它正确转换文件但它有两个错误,第一个错误是:一旦它完成转换文件并浏览新文件,所以它在这样的线程中给出错误:< / p>
线程中的异常 “AWT-EventQueue-0”java.lang.IllegalThreadStateException at java.lang.Thread.start(未知来源)
另一个错误是:进度条无法正常工作 这是代码:
import it.sauronsoftware.jave.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class MultiThreadign {
static String x=null;
static File Audio;
public static void main(String[] aa)
{
final JFrame mainframe=new JFrame("Video To Mp3 Converter");
mainframe.setResizable(true);
final JPanel panel=new JPanel();
panel.setLayout(null);
panel.setBackground(Color.white);
mainframe.add(panel);
/* ********************** MAIN BODY ************************* */
JLabel browsefiles=new JLabel("Files");
browsefiles.setBounds(300, 50, 100, 25);
panel.add(browsefiles);
JLabel progressl=new JLabel("Status");
progressl.setBounds(600, 50, 100, 25);
panel.add(progressl);
// first task buttons start
final JTextField field1=new JTextField (300);
field1.setBounds(160, 80, 300, 25);
panel.add(field1);
final JProgressBar progress = new JProgressBar();
progress.setBounds(475, 80, 280, 25);
progress.setStringPainted(true);
progress.setMinimum(0);
progress.setMaximum(99);
panel.add(progress);
JButton browse=new JButton("Browse File");
browse.setBounds(10, 80, 130, 25);
panel.add(browse);
browse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{ final JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.mp4, *.avi,*.3gp, *2gp", new String[] {"mp4","avi","3gp"});
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(mainframe) == JFileChooser.APPROVE_OPTION)
{
field1.setText(chooser.getSelectedFile().getName());
x=chooser.getSelectedFile().getAbsolutePath();
}
}
}); // browse files action ends here
final Thread t1=new Thread (new Runnable()
{
public void run()
{ mainframe.getContentPane().repaint();
mainframe.getContentPane().validate();
try
{
File Video=new File(x);
String z="Audio.mp3";
Audio=new File (z);
AudioAttributes audio=new AudioAttributes();
audio.setCodec("libmp3lame"); //mp3 format "libmp3lame"
int abc=128000;
audio.setBitRate(abc);
audio.setChannels (new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attr=new EncodingAttributes();
attr.setFormat("mp3");
attr.setAudioAttributes(audio);
Encoder encode=new Encoder();
long totalLength = Audio.length();
try
{
encode.encode(Video, Audio, attr);
FileReader fr=new FileReader(Audio);
@SuppressWarnings("resource")
BufferedReader br=new BufferedReader(fr);
long readLength = 0;
String s="";
while ((s = br.readLine()) != null) {
readLength += s.length();
progress.setValue((int) totalLength);
}
}
catch (Exception e)
{
System.out.print(e);
}
JOptionPane.showMessageDialog(null, "first Completed");
Audio=null;
}
catch (Exception e)
{
System.out.print(e);
}
//mainframe.repaint();
}
});
// first task button ends
final JButton start=new JButton("Start Converting");
start.setBounds(180, 260, 250, 25);
panel.add(start);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (x !=null)
{
t1.start();
JOptionPane.showMessageDialog(null, "process one started.");
x=null;
}
}});
/* ********************* MAIN BODY ENDS HERE ************* */
/* **************************** MENU BAR ACTIONS HERE ******************* */
mainframe.setSize(800, 400);
mainframe.setLocationRelativeTo(null);
mainframe.setVisible(true);
}
}
答案 0 :(得分:1)
首先,您的代码尝试使用除EDT之外的线程的Swing组件。这将导致未定义的行为。这就是你获得该例外的原因。
您应始终在AWT Event Dispatch Thread (EDT)上创建,修改和访问所有Swing组件。阅读Concurrency in Swing。
以下是在EDT上调用代码的简短示例:
//Submits this Runnable to the queue to be run by on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add stuff to frame
frame.pack();
frame.setVisible(true);
}
});
其次,对JProgressBar
使用SwingWorker
会更好。阅读How to Use Progress Bars。如果你真的想要,你可以使用Thread
,但你仍然可以管理一种只在EDT上更新Swing组件的方法。这就是SwingWorker
的用途:以不会阻止EDT的方式轻松执行长时间运行操作,并可选择定期更新Swing组件,例如{{ 1}},正确地在EDT上。
第三,您应该avoid setting absolute sizes and positions for components并使用Layout Managers代替。