gpt4 book ai didi

java - 当我在我的 GUI 上播放歌曲时,JButton 一直被按下,我无法点击任何东西

转载 作者:行者123 更新时间:2023-11-29 03:01:31 25 4
gpt4 key购买 nike

我写了一个程序,它基本上是一个图片幻灯片放映,也显示文本和播放音乐。一切正常,除非我按下名为“播放歌曲”的 JButton。当我按下此按钮时,歌曲开始播放,但按钮仍处于按下状态,我无法在 GUI 中单击任何内容。我加载歌曲的方式有一个数组,其中包含选定文件夹中文件的所有路径名,然后我使用 FileInputStream 使用数组中的路径名加载文件。一旦文件被加载,它就会被播放。我认为这是读取 .mp3 文件并播放它们的最佳方式,因为我采用了类似的方法来读取选定文件夹中的所有图像,并且效果很好。任何帮助将不胜感激!这是包含我的主要方法的文件,它包含创建 GUI 的类,并包含除播放音乐之外的所有方法:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Aubs extends JFrame
{
static File pics[] = null;
JLabel label;
JPanel panel;
JTextArea quoteDisplay, imageDisplay;
JButton newQuote, newBC, newText, song;
ArrayList<String> quotes = new ArrayList<String>();
public Aubs()
{
loadPics();
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Background());
add(panel);

label = new JLabel();
setPics();
changePic e = new changePic();
label.addMouseListener(e);
panel.addMouseListener(e);

song = new JButton("Play Song"); // The button to change song
song.setBounds(1000,115,185,30);
song.addActionListener(new Song()); // Add ActionListener
panel.add(song); // Adds the button to the screen

imageDisplay = new JTextArea("Click the image for a new one");
imageDisplay.setBounds(630,30,300,300);
imageDisplay.setFont(new Font("FatFrank",Font.BOLD,16));
imageDisplay.setForeground(Font()); // Sets the font color
imageDisplay.setOpaque(false);
imageDisplay.setEditable(false);
panel.add(imageDisplay);
panel.setBackground(Background());

quoteDisplay = new JTextArea();
setQuotes();

newQuote = new JButton("Quote");
newQuote.setBounds(1000,0,185,40);
newQuote.addActionListener(new changeQuote());
newQuote.setForeground(Color.BLACK);
panel.add(newQuote);

newBC = new JButton("New Background Color");
newBC.setBounds(1000,90,185,30);
newBC.addActionListener(new newBack());
newBC.setForeground(Color.BLACK);
panel.add(newBC);

newText = new JButton("New Font color");
newText.setBounds(1000,65,185,30);
newText.addActionListener(new newFont());
newText.setForeground(Color.BLACK);
panel.add(newText);


}
private void loadPics()
{
JFileChooser choose = new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int status = choose.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
File dir = choose.getSelectedFile();
if (dir.exists())
{
pics = dir.listFiles(new FileFilter()
{
@Override
public boolean accept(File pathname)
{
String name = pathname.getName().toLowerCase();
return name.endsWith(".png")
|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")
|| name.endsWith(".bmp")
|| name.endsWith(".gif");
}
});
}
}
}
private void setPics()
{
int i = (int)(Math.random()*pics.length); // Chooses a number for index
try
{
BufferedImage buff = ImageIO.read(pics[i]); // Reads in the image
ImageIcon icon = new ImageIcon(buff); // Converts it to an ImageIcon
icon = transform(icon); // Resizes
label.setIcon(icon);
label.setBounds(10,10,600,700);
panel.add(label);
panel.setBackground(Background());
}
catch (IOException e)
{
e.printStackTrace();
}
}

private ImageIcon transform(ImageIcon x) // Resizes the Image
{
ImageIcon temp = x;
Image image = temp.getImage();
Image tempImg = image.getScaledInstance(600,700,java.awt.Image.SCALE_SMOOTH );
temp = new ImageIcon(tempImg);
return temp;
}
private void loadQuotes()
{
URL url = getClass().getResource("Quotes/Quotes.txt");
try{
Scanner scan = new Scanner(new FileInputStream(url.getPath()));

while(scan.hasNextLine())
{
quotes.add(scan.nextLine());
}
scan.close();
}
catch(Exception ex)
{
System.out.println("File not found");
}
}
private void setQuotes()
{
loadQuotes();
int q = (int)(Math.random()*quotes.size());
quoteDisplay.setText(quotes.get(q));
quoteDisplay.setBounds(650,100,200,400);
quoteDisplay.setFont(new Font("FatFrank",Font.BOLD,16));
quoteDisplay.setForeground(Font());
quoteDisplay.setLineWrap(true);
quoteDisplay.setWrapStyleWord(true);
quoteDisplay.setOpaque(false);
quoteDisplay.setEditable(false);
panel.setBackground(Background());
panel.add(quoteDisplay);
imageDisplay.setBackground(Background());
}
private Color Font()
{
int r = (int)(Math.random() *256);
int g = (int)(Math.random() *256);
int b = (int)(Math.random() *256);
return (new Color(r, g, b).brighter());
}
private Color Background()
{
int r = (int)(Math.random() *256);
int g = (int)(Math.random() *256);
int b = (int)(Math.random() *256);
return (new Color(r, g, b).darker());
}
public void playSong()
{
MP3 play = new MP3(); // Creates and object of MP3
play.play(); // Calles the play method in MP3
}
private class changePic implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
setPics();
quoteDisplay.setForeground(Font());
imageDisplay.setForeground(Background());
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
}
private class changeQuote implements ActionListener
{
public void actionPerformed(ActionEvent q)
{
setQuotes();
}
}
private class newBack implements ActionListener
{
public void actionPerformed(ActionEvent bc)
{
panel.setBackground(Background());
}
}
private class newFont implements ActionListener
{
public void actionPerformed(ActionEvent fc)
{
quoteDisplay.setForeground(Font());
imageDisplay.setForeground(Font());
}
}
public class Song implements ActionListener // Supposes to change the song when a button is pressed
{
public void actionPerformed(ActionEvent s)
{
playSong();
}
}
public static void main(String[] args)
{
Aubs aubs = new Aubs();
aubs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aubs.setSize(new Dimension(1200, 1000));
aubs.setVisible(true);
}

}这是播放音乐的类:

import javazoom.jl.player.*;
import java.io.*;
import java.util.ArrayList;
public class MP3
{
static File list[] = null;
ArrayList<Player> songs = new ArrayList<Player>();
public void play()
{
MP3Files(); // Loads the pathnames to the array list[].
int i = 0; // Will be the count of the files loaded in
int x = (int)(Math.random()*i);
try
{
Player cur;
if(i >= list.length-1) // True when all files are loaded,
{
cur = songs.get(x); // Then it chooses a random song
cur.play(); // Play that song
}
else // True when not all files from list[] are loaded,
{
FileInputStream in = new FileInputStream(list[i]);
songs.add(new Player(in)); //add the song to theArrayList.
cur = songs.get(x); // Get random song
cur.play(); // Play random song
i++;
}
}
catch(Exception e)
{
System.out.println("Error");
}
}
public static void MP3Files() // Loads all the pathnames
{
File dir = new File("/Users/mine/Desktop/Music");
if(dir.isDirectory())
{
list = dir.listFiles(new FileFilter() // the pathnames.
{
@Override
public boolean accept(File pathname)
{
String name = pathname.getName().toLowerCase();
return name.endsWith(".mp3");

}
});
}
}

最佳答案

When I press this button the songs play but the button remains pressed and I can't click and anything in GUI.

从监听器调用的代码在 Event Dispatch Thread (EDT) 上执行,该线程负责绘制 GUI。

如果您随后调用一个长时间运行的任务,您将阻止 EDT,这意味着 GUI 无法响应任何更多事件或重新绘制自身,直到任务完成。

您需要启动一个单独的Thread 来播放您的歌曲。

阅读 Concurrency 上的 Swing 教程部分有关 EDT 的更多信息。您可以创建自己的 Thread,也可以使用教程中描述的 SwingWorker(它会为您创建 Thread),具体取决于您的具体要求。

关于java - 当我在我的 GUI 上播放歌曲时,JButton 一直被按下,我无法点击任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34551663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com