線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1713
推到 Plurk!
推到 Facebook!

KeyEvent 跟 Buttton的 Actionevent不行共用. …

尚未結案
rz600000
一般會員


發表:3
回覆:1
積分:0
註冊:2005-02-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-06 21:24:19 IP:218.175.xxx.xxx 未訂閱
KeyEvent 跟 Buttton的 Actionevent不行共用. … import javax.swing.*; import java.awt.event.*; import java.awt.*; public class KeyDemo extends JFrame implements KeyListener,ActionListener { private String line1 = "", line2 = ""; private String line3 = ""; private JTextArea tt; public KeyDemo() { super( "Demonstrating Keystroke Events" ); getContentPane().setLayout(new GridLayout()); tt = new JTextArea( 10, 15 ); tt.setText( "Press any key on the keyboard..." ); tt.setEnabled( false ); JButton jb1= new JButton("jb1"); jb1.addActionListener(this); tt.addKeyListener( this ); getContentPane().add( tt ); getContentPane().add( jb1 ); setSize( 350, 100 ); show(); } public void keyPressed( KeyEvent e ) { line1 = "Key pressed: " e.getKeyText( e.getKeyCode() ); setLines2and3( e ); } public void keyReleased( KeyEvent e ) { line1 = "Key released: " e.getKeyText( e.getKeyCode() ); setLines2and3( e ); } public void keyTyped( KeyEvent e ) { line1 = "Key typed: " e.getKeyChar(); setLines2and3( e ); } private void setLines2and3( KeyEvent e ) { line2 = "This key is " ( e.isActionKey() ? "" : "not " ) "an action key"; String temp = e.getKeyModifiersText( e.getModifiers() ); line3 = "Modifier keys pressed: " ( temp.equals( "" ) ? "none" : temp ); tt.setText( line1 "\n" line2 "\n" line3 "\n" ); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("jb1")) { System.out.println("why..."); } } public static void main( String args[] ) { KeyDemo app = new KeyDemo(); app.addWindowListener ( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } 上面那個程式是說 我在textArea 中按下如 enter的鍵 他在textArea中就會出現 你按了 enter鍵 或是按下a textarea就會出現說你按下了a 而旁邊的按鈕 按下去的話會出現 why的文字.. 假若兩個功能分開兩個frame寫 可以達到兩種功能 但是我把兩個功能寫在一個 Frame中KeyEnent就不會跑了 只會跑button的actionlistener 該如何解決阿...
系統時間:2024-04-28 23:37:42
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!