按下 ActionListerner 时 Jpanel 没有反应
Jpanel doesn't react when pressing ActionListerner
所以我制作了一个非常简单的 GUI,看起来与此类似:
我简单做的地方
- 打开以浏览音乐文件
- 播放音乐
- 停止音乐。
非常简单,但在制作 JFileChooser 时遇到了一些问题。所以我所做的是使用一个简单的 GUI:
btnOpen = new JButton("Open");
btnOpen.setBounds(6, 71, 75, 23);
pnlSound.add(btnOpen);
fileChooser = new JFileChooser();
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOpen) {
SampleJFileChooser();
并且有一个执行 JFileChooser 的方法:
public void SampleJFileChooser(){
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File("C:/Users/Balling/Desktop"));
int result = jFileChooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = jFileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
但现在的问题是,每当我按下打开按钮时,什么都没有发生。并且 JFileChooser 没有弹出,我一直在尝试为 JFileChooser 提供其他示例代码,但没有。仍然是同样的问题。这意味着按下按钮时没有任何反应。
有什么问题吗?
我还为音频创建了这个线程,如果我现在可以让它工作的话。我如何让它读取歌曲并播放它?有没有特殊的方法?
编辑:我不小心删除了一个具有相同 post 并且无法取消删除的线程,所以我重新尝试使用该线程。
试试吧...!!!
解决方案:-
public class Solu implements ActionListener {
File filename;
JFrame wind = new JFrame("V");
JButton btnOpen = new JButton("open");
JPanel pnlSound=new JPanel();
public Solu() {
wind.setSize(400, 500);
wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wind.add(btnOpen);
btnOpen.setBounds(6, 71, 75, 23);
pnlSound.add(btnOpen);
wind.add(pnlSound);
btnOpen.addActionListener(this);
wind.setVisible(true); }
public static void main(String[] args){
new Solu();}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==btnOpen){
JFileChooser filechooser = new JFileChooser();
filechooser.setCurrentDirectory(new File("C://Users//V//Desktop"));
int result = filechooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = filechooser.getSelectedFile();
System.out.println("Selected file: " +selectedFile.getAbsolutePath());
}}}}
所以我制作了一个非常简单的 GUI,看起来与此类似:
我简单做的地方
- 打开以浏览音乐文件
- 播放音乐
- 停止音乐。
非常简单,但在制作 JFileChooser 时遇到了一些问题。所以我所做的是使用一个简单的 GUI:
btnOpen = new JButton("Open");
btnOpen.setBounds(6, 71, 75, 23);
pnlSound.add(btnOpen);
fileChooser = new JFileChooser();
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOpen) {
SampleJFileChooser();
并且有一个执行 JFileChooser 的方法:
public void SampleJFileChooser(){
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File("C:/Users/Balling/Desktop"));
int result = jFileChooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = jFileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
但现在的问题是,每当我按下打开按钮时,什么都没有发生。并且 JFileChooser 没有弹出,我一直在尝试为 JFileChooser 提供其他示例代码,但没有。仍然是同样的问题。这意味着按下按钮时没有任何反应。
有什么问题吗?
我还为音频创建了这个线程,如果我现在可以让它工作的话。我如何让它读取歌曲并播放它?有没有特殊的方法?
编辑:我不小心删除了一个具有相同 post 并且无法取消删除的线程,所以我重新尝试使用该线程。
试试吧...!!! 解决方案:-
public class Solu implements ActionListener {
File filename;
JFrame wind = new JFrame("V");
JButton btnOpen = new JButton("open");
JPanel pnlSound=new JPanel();
public Solu() {
wind.setSize(400, 500);
wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wind.add(btnOpen);
btnOpen.setBounds(6, 71, 75, 23);
pnlSound.add(btnOpen);
wind.add(pnlSound);
btnOpen.addActionListener(this);
wind.setVisible(true); }
public static void main(String[] args){
new Solu();}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==btnOpen){
JFileChooser filechooser = new JFileChooser();
filechooser.setCurrentDirectory(new File("C://Users//V//Desktop"));
int result = filechooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = filechooser.getSelectedFile();
System.out.println("Selected file: " +selectedFile.getAbsolutePath());
}}}}