在public void actionPerformed(ActionEvent e) 方法中加入如下代碼:
創(chuàng)新互聯(lián)是一家專業(yè)提供中原企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為中原眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
if (e.getSource()==a) {
System.out.println("a");
} else if (e.getSource() == b) {
System.out.println("b");
} else if (e.getSource() == c) {
System.out.println("c") ;
}
另外:你似乎還有語(yǔ)法錯(cuò)誤:
for (int i = 0; i allButtons.length; i++) {
allButtons[i].addActionListener(this);}
這句話的位置沒(méi)有放對(duì),放這里肯定會(huì)報(bào)空指針,說(shuō)你的allButton為空
最好放在你的注釋:注冊(cè)監(jiān)聽程序
這個(gè)之后
1,為相應(yīng)的控件設(shè)置KeyListener()的監(jiān)聽
2,實(shí)現(xiàn)接口中的方法
3,主要重寫keyPressed()這個(gè)方法
4,書寫邏輯
5,見代碼
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_1e.getKeyCode()==KeyEvent.VK_2){
System.out.println("1,2鍵被點(diǎn)擊了。。。");
}
}ps,還可以自己寫一個(gè)數(shù)組記錄每個(gè)按鍵的狀態(tài),根據(jù)狀態(tài)值進(jìn)行邏輯處理。
你這個(gè)問(wèn)題是由于焦點(diǎn)(Focus)不正確引起的問(wèn)題.
在窗口打開之后,如果焦點(diǎn)落在Frame上面,按鍵被Frame捕獲,提示輸出,如果焦點(diǎn)落在Button上面,剛按鍵被Button捕獲,當(dāng)然就沒(méi)有輸出.
更改方法有如下建議:
1.this.setFocusable(true);加在構(gòu)造函數(shù)最后,可以保證Frame可以響應(yīng)鍵盤事件,但是按下按扭之后焦點(diǎn)轉(zhuǎn)移,鍵盤事件不再被Frame響應(yīng),因此需要按Tab鍵重新讓Frame得到焦點(diǎn).(不推薦此方法,不知道按Tab鍵的用戶就慘了...)
2. 將鍵盤響應(yīng)KeyListener分別注冊(cè)給所有的Button.
最后代碼如下:
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends Frame implements KeyListener,ActionListener{
private Button b1;
private Button b2;
public ButtonTest(int i,int j) {
addKeyListener(this);
setLayout(new FlowLayout(1));
b1=new Button("yellow");
b1.addActionListener(this);
b1.addKeyListener(this);
b2=new Button("blue");
b2.addActionListener(this);
b2.addKeyListener(this);
setSize(i, j);
add(b1);add(b2);
pack();
setVisible(true);
this.setFocusable(true);
}
//實(shí)現(xiàn)ActionListener接口方法
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand().equals("yellow")) {
b1.setBackground(Color.red);
b2.requestFocus();//點(diǎn)擊button1時(shí)把事件焦點(diǎn)給b2
} else if (a.getActionCommand().equals("blue")) {
b2.setBackground(Color.BLUE);
}
}
//實(shí)現(xiàn)keylistener的3個(gè)方法
public void keyTyped(KeyEvent e){
System.out.println("KeyTyped"+" "+e);
}
public void keyPressed(KeyEvent e){
System.out.println("KeyPressed"+" "+e);
}
public void keyReleased(KeyEvent e){
System.out.println("KeyReleased"+" "+e);
}
public static void main(String[] args) {
ButtonTest my=new ButtonTest(300,300);
my.setSize(200,200);
}
}
把下面兩個(gè)類去了,直接用匿名內(nèi)部類來(lái)實(shí)現(xiàn)...
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
// 根據(jù)文本框里輸入的數(shù)字,調(diào)用DoubleColorBall中的方法生成彩票
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("請(qǐng)輸入正整數(shù)數(shù)字");
}
}
});
網(wǎng)頁(yè)名稱:點(diǎn)擊監(jiān)聽事件代碼java java監(jiān)聽按鈕
文章URL:http://jinyejixie.com/article4/dddhcie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、App設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、微信小程序、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)