简体   繁体   English

按下键盘键时如何改变焦点在不同的Jbutton上?

[英]How to change focus different Jbuttons when a keyboard key is pressed?

So hey guys, I've been trying to design a piano through java, net-beans for my final project at uni. 所以,大家好,我一直在尝试通过Java和Net-Beans设计钢琴,这是我在uni的最终项目。 thing here is every thing works well so far but I've got issues with focus here. 到目前为止,一切都运转良好,但我在这里遇到了一些问题。 for example I'm trying to play the keys through the keyboard, i want the piano keys to get focused to the specified key its assigned any idea on how to do it? 例如,我正在尝试通过键盘弹奏琴键,我希望钢琴琴键可以专注于指定的琴键,而该琴键分配了有关如何执行操作的想法? much appreciated. 非常感激。 this is my 1st question here, so I'm not sure if I'm asking the right things here. 这是我的第一个问题,所以我不确定在这里问的对吗。

I'm gonna provide the sample I'm working with. 我将提供我正在使用的样本。

   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

} }

As this is a sample I'm working with just 3 buttons here. 因为这是一个示例,所以我在这里仅使用3个按钮。 I'd like to get all the help i can thanks in advance 我想获得所有我可以提前感谢的帮助

Register keylister with jframe using addKeyListener(...) for all three keys. 使用addKeyListener(...)为所有三个键向jframe注册keylister。 so when any keypressed within frame, key event works. 因此,当在框架中按下任何按键时,按键事件都会起作用。 Before key press, check your frame is active. 按键之前,请检查相框是否处于活动状态。 if not click within frame then press key. 如果未单击框架内,则按键。 Can also change focus on button using requestFocus() method 也可以使用requestFocus()方法更改按钮的焦点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM