所以,大家好,我一直在尝试通过Java和Net-Beans设计钢琴,这是我在uni的最终项目。到目前为止,每件事都运作良好,但我在这里遇到了一些问题。例如,我正在尝试通过键盘弹奏琴键,我希望钢琴琴键可以专注于指定的琴键,而该琴键分配了有关如何执行操作的想法?非常感激。这是我的第一个问题,所以我不确定在这里问的对吗。

我将提供我正在使用的样本。

   package javaapplication5;

   import sun.audio.*;

   import java.io.*;

   import javax.swing.JOptionPane;

   public class NewJFrame1 extends javax.swing.JFrame {

public NewJFrame1() {

    initComponents();

}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("A");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    jButton1.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            jButton1FocusGained(evt);
        }
    });
    jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton1KeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            jButton1KeyReleased(evt);
        }
    });

    jButton2.setText("B");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton2KeyPressed(evt);
        }
    });

    jButton3.setText("C");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });
    jButton3.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton3KeyPressed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(89, 89, 89)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(123, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(91, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(110, 110, 110))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(45, 45, 45)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(190, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(51, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\A.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\B.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
   InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\C.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {

    if(evt.getKeyChar()=='z'){
    InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\A.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
   }

}

private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {

    if(evt.getKeyChar()=='x'){
    InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\B.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
   }
}

private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {

    if(evt.getKeyChar()=='c'){
    InputStream iAudio;

    try{
        iAudio = new FileInputStream(new File("C:\\Users\\MohammedRishad\\Desktop\\Audios\\C.wav"));
        AudioStream iMusic = new AudioStream(iAudio);
        AudioPlayer.player.start(iMusic);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
   }
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new NewJFrame1().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
// End of variables declaration


}

因为这是一个示例,所以我在这里仅使用3个按钮。我想获得所有我可以提前感谢的帮助

最佳答案

使用addKeyListener(...)为所有三个键向jframe注册keylister。因此,当在框架中按下任何按键时,按键事件都会起作用。按键之前,请检查相框是否处于活动状态。如果未单击框架内,则按键。也可以使用requestFocus()方法更改按钮的焦点

10-06 13:37