package student;import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;public class NewJFrame extends javax.swing.JFrame { public NewJFrame() { initComponents();
} @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jDesktopPane1 = new javax.swing.JDesktopPane();
jButton1 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jDesktopPane1.setBackground(new java.awt.Color(240, 240, 240)); jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jButton1KeyPressed(evt);
}
});
jButton1.setBounds(20, 30, 81, 23);
jDesktopPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER); jComboBox1.setEditable(true);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jComboBox1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jComboBox1KeyPressed(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
jComboBox1KeyTyped(evt);
}
});
jComboBox1.setBounds(70, 110, 200, 21);
jDesktopPane1.add(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER); 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(26, 26, 26)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(49, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(29, Short.MAX_VALUE))
); pack();
}// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewJInternalFrame aa = new NewJInternalFrame();
this.jDesktopPane1.add(aa);
aa.setVisible(true);
} private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
} private void jComboBox1KeyPressed(java.awt.event.KeyEvent evt) {
} private void jComboBox1KeyTyped(java.awt.event.KeyEvent evt) {
} private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
//// evt.getActionCommand() 这里判断是否EDIT的触发///////////////////////////////////////////////////////////////////
final String value;
final int f = 0;
value = (String) this.jComboBox1.getSelectedItem();
JTextField editorField = (JTextField) jComboBox1.getEditor().getEditorComponent();
editorField.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (f == 0) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
JOptionPane.showMessageDialog(null, "你按了回车" + value);
}
}
}
});
////////////////////////////////////////////////////////////////////////////
//////////////////判断完后这段还是触发了多次,而且第按一次回车都会比上次多触发一次
////这个怎么解决啊???? }
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JDesktopPane jDesktopPane1;
// End of variables declaration
}
//// evt.getActionCommand() 这里判断是否EDIT的触发///////////////////////////////////////////////////////////////////
final String value;
final int f = 0;
value = (String) this.jComboBox1.getSelectedItem();
JTextField editorField = (JTextField) jComboBox1.getEditor().getEditorComponent();
editorField.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (f == 0) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
JOptionPane.showMessageDialog(null, "你按了回车" + value);
}
}
}
});
////////////////////////////////////////////////////////////////////////////
//////////////////判断完后这段还是触发了多次,而且第按一次回车都会比上次多触发一次
////这个怎么解决啊???? }
主要是这段,应该怎么写呢???