jbuilder 100例 java源码学习,非常不错,有需要朋友下来
源代码在线查看: frame1.java~7~
package keyeventii; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); Label label1 = new Label(); /**Construct the frame*/ public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); label1.setAlignment(1); label1.setFont(new java.awt.Font("Dialog", 1, 30)); contentPane.setLayout(borderLayout1); this.setResizable(false); this.setSize(new Dimension(400, 126)); this.setTitle("KeyEventII演示程序"); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { this_keyPressed(e); } }); contentPane.add(label1, BorderLayout.CENTER); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void this_keyPressed(KeyEvent e) { String s = null , s1 = null , s2 = null; int i,j; i = e.getModifiers(); j = e.getKeyCode(); s1 = e.getKeyModifiersText(i); s2 = e.getKeyText(e.getKeyCode()); //s = (new Integer(e.getKeyCode())).toString(); if (j == e.VK_CONTROL || j == e.VK_SHIFT || j == e.VK_ALT || i == 0) s = s2; else s = s1 + " + " + s2; label1.setText(s); } }