社区
Java SE
帖子详情
关于addKeyListener
kingofvc
2003-10-19 04:49:43
使用addKeyListener的时候发现对于JLabel JPane都没有反应
只有JTextXXX才有结果 那么如何使得其他控件也接受键盘输入呢?
...全文
59
3
打赏
收藏
关于addKeyListener
使用addKeyListener的时候发现对于JLabel JPane都没有反应 只有JTextXXX才有结果 那么如何使得其他控件也接受键盘输入呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
tripofdream
2003-10-19
打赏
举报
回复
因为JLabel和JPane的isFocusTraversable()是false.
kingofvc
2003-10-19
打赏
举报
回复
再顶
兄弟们 来捧个人场也好
kingofvc
2003-10-19
打赏
举报
回复
up……
j2me打砖块游戏原代码
import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.
KeyListener
; import javax.swing.*; public class asd { public static void main(String[] args) { JFrame f = new JFrame(); f.setTitle("我爱JAVA"); f.setSize(400, 605); MyPanel mp = new MyPanel(); f.
add
(mp); Thread t = new Thread(mp); t.start(); f.
add
KeyListener
(mp); mp.
add
KeyListener
(mp); f.show(); } } 这是部分代码,绝对真实,骗子死全家!!!
金蝶EAS Dep开发资料
金蝶EAS Dep开发资料,DEP常用脚本集2013,DEP-监听查询调用,动态扩展平台常见问题,动态扩展平台常用脚本集
JAVA编写的计算器
package com; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.
KeyListener
; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.WindowConstants; public class Caculator extends JFrame implements ActionListener,
KeyListener
{ /** * */ private static final long serialVersionUID = 5204982079673572494L; private JTextField tf=new JTextField(); private float x=0; private float y=0; private int code=0; private boolean enable; private boolean first; private String str=""; public Caculator(){ Container ct=this.getContentPane(); this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); tf.setHorizontalAlignment(JTextField.RIGHT); //tf.setText("0"); enable=true; first=true; ct.
add
(tf,BorderLayout.NORTH); JPanel panel=new JPanel(); panel.setLayout(new GridLayout(4,4)); this.
add
WindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){ e.getWindow().setVisible(false); e.getWindow().dispose(); System.exit(0); } } }); Button btn=null; btn=new Button("1"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("2"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("3"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("+"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("4"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("5"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("6"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("-"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("7"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("8"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("9"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("*"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("0"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("."); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("/"); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); btn=new Button("="); panel.
add
(btn); btn.
add
ActionListener(this); btn.
add
KeyListener
(this); this.
add
(panel,BorderLayout.CENTER); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Caculator mainframe=new Caculator(); mainframe.setTitle("testing Caculator"); mainframe.setSize(400,400); mainframe.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand()=="+"){ x= Float.parseFloat(tf.getText()); code=0; this.tf.setText(""); } if(e.getActionCommand()=="-"){ x= Float.parseFloat(tf.getText()); code=1; this.tf.setText(""); } if(e.getActionCommand()=="*"){ x= Float.parseFloat(tf.getText()); code=2; this.tf.setText(""); } if(e.getActionCommand()=="/"){ x= Float.parseFloat(tf.getText()); code=3; this.tf.setText(""); } if(e.getActionCommand()!="+"&&e.getActionCommand()!="-"&&e.getActionCommand()!="*"&&e.getActionCommand()!="/"&&e.getActionCommand()!="="){ if(enable){ if(first){ System.out.println("haha"); tf.setText(e.getActionCommand()); first=false; } else { tf.setText(tf.getText()+e.getActionCommand()); } } else { tf.setText(e.getActionCommand()); enable=true; } } if(e.getActionCommand()=="="){ switch(code){ case 0: y=x+Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 1: y=x-Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 2: y=x*Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 3: y=x/Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; } } } public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyChar()=='+'){ x= Float.parseFloat(tf.getText()); code=0; this.tf.setText(""); } if(e.getKeyChar()=='-'){ x= Float.parseFloat(tf.getText()); code=1; this.tf.setText(""); } if(e.getKeyChar()=='*'){ x= Float.parseFloat(tf.getText()); code=2; this.tf.setText(""); } if(e.getKeyChar()=='/'){ x= Float.parseFloat(tf.getText()); code=3; this.tf.setText(""); } if(e.getKeyChar()=='1'||e.getKeyChar()=='2'||e.getKeyChar()=='3'||e.getKeyChar()=='0' ||e.getKeyChar()=='4'||e.getKeyChar()=='5'||e.getKeyChar()=='6'||e.getKeyChar()=='.' ||e.getKeyChar()=='7'||e.getKeyChar()=='8'||e.getKeyChar()=='9'){ System.out.println("hai"); if(enable){ if(first){ System.out.println("hehe"); str=Character.toString(e.getKeyChar()); tf.setText(str); first=false; } else { str=Character.toString(e.getKeyChar()); tf.setText(tf.getText()+str); } } else { str=Character.toString(e.getKeyChar()); tf.setText(str); enable=true; } } if(e.getKeyCode()==KeyEvent.VK_ENTER){ switch(code){ case 0: y=x+Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 1: y=x-Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 2: y=x*Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; case 3: y=x/Float.parseFloat(this.tf.getText()); tf.setText(Float.toString(y)); enable=false; break; } } } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }
简单的Java窗口源码
import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.
KeyListener
; public abstract class MyFrame extends JFrame implements
KeyListener
{ public MyFrame(){ this.setSize(800,600); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); this.
add
KeyListener
(this); this.setTitle("武当王也的超级玛丽"); } public static void main(String[] args) { MyFrame myFrame= new MyFrame() {
基于java的简易俄罗斯方块制作
public class Tetris extends JFrame { public Tetris() { Tetrisblok a = new Tetrisblok();
add
KeyListener
(a);
add
(a); } public static void main(String[] args) { Tetris frame = new Tetris(); JMenuBar menu = new JMenuBar(); frame.setJMenuBar(menu); JMenu game = new JMenu("游戏"); JMenuItem newgame = game.
add
("新游戏"); JMenuItem pause = game.
add
("暂停"); JMenuItem goon = game.
add
("继续"); JMenuItem exit = game.
add
("退出"); 内含标识
Java SE
62,614
社区成员
307,327
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章