我目前正在尝试编写一个简单的程序,它将在BPM中以给定的间隔显示音符。我已经编写了大部分代码,但是我无法弄清楚如何获取包含注释和sharp / flat的标签来更新给定的每个间隔。到目前为止这是我的代码。 NoteGenerator对象是我自己创建的,仅用于生成随机音符/ sharp / flat并跟踪速度。我已经测试了NoteGenerator类,它的工作方式与我的预期完全相同。提前谢谢!
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class NotePractice extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NotePractice frame = new NotePractice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NotePractice() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
NoteGenerator noteGen = new NoteGenerator();
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(new Integer(100), new Integer(20), null, new Integer(5)));
spinner.setBounds(211, 196, 60, 20);
contentPane.add(spinner);
JLabel lblBpm = new JLabel("BPM");
lblBpm.setFont(new Font("Arial", Font.BOLD, 12));
lblBpm.setBounds(175, 198, 26, 14);
contentPane.add(lblBpm);
JLabel lblNote = new JLabel(noteGen.getNote());
lblNote.setFont(new Font("Arial", Font.BOLD, 99));
lblNote.setBounds(175, 60, 82, 104);
contentPane.add(lblNote);
JLabel lblStep = new JLabel(noteGen.getStep());
lblStep.setFont(new Font("Arial", Font.BOLD, 70));
lblStep.setBounds(258, 93, 60, 58);
contentPane.add(lblStep);
JButton btnUpdateTempo = new JButton("Update Tempo");
btnUpdateTempo.setFont(new Font("Arial", Font.PLAIN, 11));
btnUpdateTempo.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
noteGen.setBPM(spinner.getComponentCount());
Timer t = new Timer(noteGen.getBPM(), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lblNote.setText(noteGen.getNote());
lblStep.setText(noteGen.getStep());
contentPane.updateUI();
}
});
}
});
btnUpdateTempo.setBounds(168, 227, 103, 23);
contentPane.add(btnUpdateTempo);
}
}