java 怎么写键盘事件监听?
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ziti implements KeyListener
{JFrame jf;
JComboBox jc;
String[] kk={"隶书","楷体","黑体","宋体"};
String str="隶书"; int k1=Font.PLAIN; int k2=16;
ziti()
{jf=new JFrame("字体事件测试");
jc=new JComboBox(kk)
Container co=jf.getContentPane();
JPanel pp=new JPanel();
co.add(pp);
pp.setLayout(null);
pp.add(jc
jf.setSize(400,500);
jf.setLocation(50,50);
jf.setVisible (true);
jc.setBounds(10,100,100,30);
jc.addKeyListener(this);
}
public void keyTyped(KeyEvent e)
{ str=kk[jc.getSelectedIndex()];
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
public class cv1
{public static void main(String[] args)
{ new ziti();
}
}
基本上就是这么写的 里面加你要写的就好咯 有什么问题你可以追问
import javax.swing.*;
import java.awt.event.*;
class ziti implements KeyListener
{JFrame jf;
JComboBox jc;
String[] kk={"隶书","楷体","黑体","宋体"};
String str="隶书"; int k1=Font.PLAIN; int k2=16;
ziti()
{jf=new JFrame("字体事件测试");
jc=new JComboBox(kk)
Container co=jf.getContentPane();
JPanel pp=new JPanel();
co.add(pp);
pp.setLayout(null);
pp.add(jc
jf.setSize(400,500);
jf.setLocation(50,50);
jf.setVisible (true);
jc.setBounds(10,100,100,30);
jc.addKeyListener(this);
}
public void keyTyped(KeyEvent e)
{ str=kk[jc.getSelectedIndex()];
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
public class cv1
{public static void main(String[] args)
{ new ziti();
}
}
基本上就是这么写的 里面加你要写的就好咯 有什么问题你可以追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询