绑定无论焦点如何,都要将键击输入到jButton

时间:2014-07-20 00:53:09

标签: java

我理解这个问题的主题可能以前出现在这个网站上,但是当我看到这些例子时,我仍然不明白如何将这个功能特别应用到我的程序中,特别是因为放置了这些语句是不同的,也因为他们似乎都说不同的东西。在发布这个问题时,我希望有一种简单的方法可以用我能理解的术语来设置这个程序的能力。

这是代码 - 我正在使用Netbeans IDE,如果有帮助的话。 我特别需要的是Enter触发btnAddMarksActionPerformed。 提前谢谢。

import java.util.*;
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*; 

public class U3A6_Marks extends javax.swing.JFrame {

ArrayList <Integer> marks = new ArrayList();

public U3A6_Marks() {
    initComponents();        
}

@SuppressWarnings("unchecked")

private void txtInput_markActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // I can't get rid of this method
}                                             

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

    //Accept mark input and add to list
    String addedMarkInput = this.txtInput_mark.getText();
    int addedMark = Integer.parseInt(addedMarkInput);
    marks.add(addedMark);

    //Clear output box so getText doesn't get redundant text
    this.txtOutput_marksList.setText("");

    //Display marks with loop
    for (int i=0; i<marks.size(); i++ ){
    this.txtOutput_marksList.setText(txtOutput_marksList.getText()+ marks.get(i) +"\n");    
    }

    //Clear input box so user doesn't need to press backspace
    this.txtInput_mark.setText("");

}                                           

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

    //Sort in ascending order
    Collections.sort(marks);

    //Redisplay required
    //Clear output box so getText doesn't get redundant text
    this.txtOutput_marksList.setText("");

    //Display marks with loop
    for (int i=0; i<marks.size(); i++ ){
    this.txtOutput_marksList.setText(txtOutput_marksList.getText()+ marks.get(i) +"\n");    
    }


}                                            

//Following line is empty
private void txtInput_markKeyTyped(java.awt.event.KeyEvent evt) {                                      
}                                      




public static void main(String args[]) {
    /* Set the Nimbus look and feel */
   .....................................
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new U3A6_Marks().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnAddMarks;
private javax.swing.JButton btnProcessMarks;
private javax.swing.JButton btnSortMarks;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField txtInput_mark;
private javax.swing.JTextArea txtOutput_marksAnalysis;
private javax.swing.JTextArea txtOutput_marksList;
// End of variables declaration                   

}

这是initComponents。我想在那里添加getRootPane()。setDefaultButton(btnAddMarks),但它的生成让我不能。

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    txtInput_mark = new javax.swing.JTextField();
    btnAddMarks = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    txtOutput_marksList = new javax.swing.JTextArea();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jScrollPane2 = new javax.swing.JScrollPane();
    txtOutput_marksAnalysis = new javax.swing.JTextArea();
    btnSortMarks = new javax.swing.JButton();
    btnProcessMarks = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Marks Processor Program");

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
    jLabel2.setText("Input Student Mark Here:");

    txtInput_mark.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    txtInput_mark.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtInput_markActionPerformed(evt);
        }
    });
    txtInput_mark.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            txtInput_markKeyTyped(evt);
        }
    });

    btnAddMarks.setText("Add Mark");
    btnAddMarks.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnAddMarksActionPerformed(evt);
        }
    });
    btnAddMarks.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            btnAddMarksKeyPressed(evt);
        }
    });

    txtOutput_marksList.setEditable(false);
    txtOutput_marksList.setColumns(20);
    txtOutput_marksList.setLineWrap(true);
    txtOutput_marksList.setRows(5);
    txtOutput_marksList.setWrapStyleWord(true);
    jScrollPane1.setViewportView(txtOutput_marksList);

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel3.setText("Marks List");

    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel4.setText("Marks Analysis");

    txtOutput_marksAnalysis.setEditable(false);
    txtOutput_marksAnalysis.setColumns(20);
    txtOutput_marksAnalysis.setLineWrap(true);
    txtOutput_marksAnalysis.setRows(5);
    txtOutput_marksAnalysis.setWrapStyleWord(true);
    jScrollPane2.setViewportView(txtOutput_marksAnalysis);

    btnSortMarks.setText("Sort");
    btnSortMarks.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSortMarksActionPerformed(evt);
        }
    });

    btnProcessMarks.setText("Process and Analyze");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtInput_mark, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAddMarks, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2))))
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(btnSortMarks, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(btnProcessMarks, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel2)
                .addComponent(txtInput_mark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnAddMarks))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel3)
                .addComponent(jLabel4))
            .addGap(7, 7, 7)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSortMarks)
                .addComponent(btnProcessMarks))
            .addContainerGap())
    );

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

private void initComponents2(){
    getRootPane().setDefaultButton(btnAddMarks);
}

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

}                                             

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


    //Accept mark input and add to list
    String addedMarkInput = this.txtInput_mark.getText();
    int addedMark = Integer.parseInt(addedMarkInput);
    marks.add(addedMark);

    //Clear output box so getText doesn't get redundant text
    this.txtOutput_marksList.setText("");

    //Display marks with loop
    for (int i=0; i<marks.size(); i++ ){
    this.txtOutput_marksList.setText(txtOutput_marksList.getText()+ marks.get(i) +"\n");    
    }

    //Clear input box so user doesn't need to press backspace twice
    this.txtInput_mark.setText("");

}                                    

1 个答案:

答案 0 :(得分:3)

frame.getRootPane().setDefaultButton( btnAddMarks );

然后,当用户使用Enter键时,它的行为就像用户点击按钮一样。此方法将为您创建适当的Key Bindings。要了解有关Key Bindings的更多信息,请阅读How to Use Key Bindings上的Swing教程。

编辑:

  

问题是,当jTextField txtInput_mark未处于焦点时,ENTER按钮仅用于触发该按钮

始终将KeyEvent分派给具有焦点的组件。因此,这意味着您的文本字段正在拦截该事件,因为您在文本字段中添加了ActionListener,或者正在处理添加到文本字段的KeyListener中的Enter键。摆脱KeyListener和/或ActionListener。