0
我想创建一个钢琴程序,你点击这个键并使用一个actionlistener从jfugue库中播放音符。出于某种原因,约18次点击后没有改变任何东西,按钮停止工作。我减少了代码来分析为什么会发生这种情况,因此只有两个注释。JButton停止工作几次点击后没有其他变化
在此先感谢您的任何建议!
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Color;
import javax.swing.JLayeredPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.*;
import org.jfugue.*;
public class ChordPlayer2 extends JComponent{
public ChordPlayer2(){
final Player player = new Player();
JFrame frame = new JFrame();
JButton cButton, csharpButton;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(0, 0);
buttonPanel.setSize(1700, 1000);
csharpButton = new JButton("");
csharpButton.setLocation(100, 150);
csharpButton.setSize(100,520);
buttonPanel.add(csharpButton);
cButton = new JButton("");
cButton.setLocation(0, 150);
cButton.setSize(160, 800);
buttonPanel.add(cButton);
class cClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C");
}
}
class csClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C#");
}
}
ActionListener c = new cClicker();
cButton.addActionListener(c);
ActionListener cs = new csClicker();
csharpButton.addActionListener(cs);
buttonPanel.setOpaque(true);
//return buttonPanel;
frame.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1700, 1000);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//JFrame.setDefaultLookAndFeelDecorated(true);
ChordPlayer2 demo = new ChordPlayer2();
}
}
此代码正常工作对我来说,按钮从未停止工作。 – syb0rg
裁减版本是否以相同的方式停止?你有任何输出看,例如,一个堆栈跟踪出现的控制台;如果没有,那么安排好,如果是的话,它说什么? – arcy