所以我有2個類,一個主類和一個繼承JButton並實現ActionListener的輔助類。我的主類擴展了JFrame並實現了ActionListener。當我點擊任何自定義按鈕時,輔助程序中的ActionListener被調用並執行其功能(打開文件選取器)。在主類中沒有調用ActionListener?
但是,當我按下任何與主類相關的按鈕(不是我自定義按鈕的按鈕)時,ActionListener不會被調用。
下面是從主類的ActionListener代碼:
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent MM");
String ac = e.getActionCommand();
if(ac.equalsIgnoreCase("play")){
pl.unPause();
}
if(ac.equalsIgnoreCase("stop")){
pl.pause();
}
}
這裏是從自定義的JButton類的ActionListener代碼:
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent MB");
int code = fc.showOpenDialog(this);
if (code == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile().getName();
if (file.substring(file.length() - 3).equalsIgnoreCase("mp3")
|| file.substring(file.length() - 3)
.equalsIgnoreCase("wav")) {
super.setText(file);
musicfile = new File(fc.getSelectedFile().getPath());
}
}
}
的系統輸出僅用於調試目的。
編輯:MusicMaker(主)類的全部代碼:
public class MusicMaker extends JFrame implements ActionListener{
public static Random gen = new Random();
public static Scanner kbr = new Scanner(System.in);
private JButton play = new JButton(new ImageIcon("play.png"));
private JButton stop = new JButton(new ImageIcon("stop.png"));
private JLabel BPML = new JLabel("BPM: ");
private SpinnerModel BPMsm = new SpinnerNumberModel(150, // initial value
1, // minimum
300, // max
1); // step
final private JSpinner BPMs = new JSpinner(BPMsm);
private ArrayList<MusicButton> mbtn = new ArrayList<MusicButton>();
public final CopyOnWriteArrayList<ArrayList<JCheckBox>> chbxsal = new CopyOnWriteArrayList<ArrayList<JCheckBox>>();
private final Object lock = new Object();
private Player pl = new Player();
public MusicMaker() {
super("Music Maker Beta v0.1");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
for (int i = 0; i < 12; i++) {
mbtn.add(new MusicButton("choose" + i));
}
for (int i = 0; i < 16; i++) {
chbxsal.add(new ArrayList<JCheckBox>());
for (int e = 0; e < 12; e++) {
chbxsal.get(i).add(new JCheckBox());
}
}
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
this.add(play, gbc);
gbc.gridx = 1;
gbc.gridwidth = 1;
this.add(stop, gbc);
gbc.gridx = 4;
gbc.gridwidth = 3;
this.add(BPMs, gbc);
gbc.gridx = 2;
gbc.gridwidth = 3;
this.add(BPML, gbc);
gbc.gridwidth = 2;
for (int i = 0; i < 12; i++) {
gbc.gridx = 0;
gbc.gridy = i + 1;
this.add(mbtn.get(i), gbc);
}
gbc.gridwidth = 1;
for (int i = 0; i < 16; i++) {
for (int e = 0; e < 12; e++) {
gbc.gridx = i + 2;
gbc.gridy = e + 1;
this.add(chbxsal.get(i).get(e), gbc);
}
}
this.pack();
Thread thread = new Thread(pl);
thread.setName("Music player thread");
thread.setDaemon(true);
thread.start();
}
public static void main(String[] args) {
MusicMaker mm = new MusicMaker();
mm.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent MM");
String ac = e.getActionCommand();
if(ac.equalsIgnoreCase("play")){
pl.unPause();
}
if(ac.equalsIgnoreCase("stop")){
pl.pause();
}
}
private class Player implements Runnable {
private volatile boolean isPaused = true;
@Override
public void run() {
while (true) {
try {
while (isPaused) {
synchronized (lock) {
lock.wait();
}
}
System.out.println("test");
} catch (Exception e) {
// handle exceptions
}
}
}
public void pause() {
isPaused = true;
}
public void unPause() {
isPaused = false;
synchronized (lock) {
lock.notifyAll();
}
}
}
}
MusicButton(自定義按鈕)類的
全部代碼:
public class MusicButton extends JButton implements ActionListener {
public static Random gen = new Random();
public static Scanner kbr = new Scanner(System.in);
// Create a file chooser
final JFileChooser fc = new JFileChooser();
public String file = "";
public File musicfile;
public MusicButton(String s) {
super(s);
super.addActionListener(this);
super.setText("Choose");
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent MB");
int code = fc.showOpenDialog(this);
if (code == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile().getName();
if (file.substring(file.length() - 3).equalsIgnoreCase("mp3")
|| file.substring(file.length() - 3)
.equalsIgnoreCase("wav")) {
super.setText(file);
musicfile = new File(fc.getSelectedFile().getPath());
}
}
}
}
你可以在附加這些ActionListeners的地方顯示你的代碼嗎? – PeterK 2014-10-10 15:56:20
我剛添加完整的代碼。 – TheSabreSlicer 2014-10-10 15:59:19
此外,當你選擇文件時,你可以有一個過濾器,你只能選擇哪些文件(因爲我看到你做一些檢查與MP3文件和WAV文件)。我建議再閱讀一下。 – user2494817 2014-10-10 15:59:20