Jak zdublować działanie przycisku "Przycisk1", klawiszem ENTER na klawiaturze?
Czy wystarczy dodanie:
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode( ) == e.VK_ENTER)
{
System.out.println("Przycisk1");
}
}
Kod programu:
public class NewJFrame extends javax.swing.JFrame
{
public NewJFrame()
{
initComponents();
}
private void initComponents()
{
przycisk1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
przycisk1.setText("Przycisk1");
przycisk1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
przycisk1ActionPerformed(evt);
}
});
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(164, 164, 164)
.addComponent(przycisk1)
.addContainerGap(159, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(140, Short.MAX_VALUE)
.addComponent(przycisk1)
.addGap(137, 137, 137))
);
pack();
}
private void przycisk1ActionPerformed(java.awt.event.ActionEvent evt)
{
System.out.println("Przycisk1");
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton przycisk1;
}