问下 KeyListener 在什么情况,添加事件监听,无响应。如果贴代码太麻烦了。
只求思路就行。各位大神,帮帮忙吧!
只求思路就行。各位大神,帮帮忙吧!
解决方案 »
- 【求教】JAVA 上传文件到FTP服务器报错,
- 关于imageio的java.lang.OutOfMemoryError问题
- java如何实现单例模式
- 两个JAVA小问题
- jbuilder2005在开发web应用程序时,当编译一个.java后如何自动放入web-info\classes下?
- 再次提问:不同frame间applet间通信
- 我的配置为什么老出现Exception in thread "main" java.lang.NoClassDefFoundError: test
- 请介绍一些能进行以JIT(Just In Time)方式编译的工具.
- 怎样用代码设置frame,让它运行开始就最大化显示。
- 做项目时的一个疑问
- 关于java运行时jre的疑问
- mysql 数据批量转移
像JcomboBox 这种组合的组件,使用时不能直接加载JcomboBox 实例上,没有响应的。
加了 但是 无响应。 我也做了个例子但是都是好使的, 所以很痛疼。
我的示例/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* OpenLab.java
*
* Created on 2010-3-25, 11:06:40
*/
package test;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Timer;/**
*
* @author Administrator
*/
public class OpenLab extends javax.swing.JFrame implements ActionListener {
/** Creates new form OpenLab */
Timer timer ;
public OpenLab() {
initComponents();
timer = new Timer(1000,this);
captureBt.setMnemonic(KeyEvent.VK_ADD);
captureBt.addActionListener(this);
captureBt.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) {
jbtExitKeyPressed(evt);
}
});
} private void jbtExitKeyPressed(java.awt.event.KeyEvent evt) {
// 用户按下组合键Ctrl + F4,调用exit()方法,退出窗体
// evt.getModifiers() == InputEvent.CTRL_MASK&&
if (evt.getKeyCode() == KeyEvent.VK_ADD) {
System.out.println("我是好人");
}
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { captureBt = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); captureBt.setText("jButton1");
captureBt.setName("captureBt"); // NOI18N
captureBt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
captureBtActionPerformed(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(113, 113, 113)
.addComponent(captureBt)
.addContainerGap(206, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(captureBt)
.addContainerGap(217, Short.MAX_VALUE))
); pack();
}// </editor-fold> private void captureBtActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new OpenLab().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton captureBt;
// End of variables declaration
int f = 10;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == captureBt){
f =10;
timer.start(); }else{
f--;
System.out.println(f);
if (f <= 0) {
timer.stop();
}
}
}
}