如何在按下键盘键时更改焦点不同的Jbuttons?

时间:2017-07-04 07:25:11

标签: java

嘿,伙计们,我一直试图通过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个按钮。我想提前得到所有帮助

2 个答案:

答案 0 :(得分:1)

使用addKeyListener(...)为所有三个键注册keyl与jframe。因此,当在帧内按键时,键事件有效。在按键之前,检查您的画面是否有效。如果没有在框架内单击,则按键。也可以使用requestFocus()方法

更改焦点按钮

答案 1 :(得分:0)

可以使用requestFocus()方法更改焦点按钮