佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1081|回复: 9

我要如何判断是哪个button被按

[复制链接]
发表于 6-12-2013 03:59 PM | 显示全部楼层 |阅读模式
我了一个用array来制造的button,A~Z,我要如何判断是哪个判断那个button被按然后要display在textfield
  1. String [] secretWord = {"S","W","I","N","G"};
  2.     String [] abcd={"A","B","C","D","E","F","G","H","I","J","K","L","N","M","O","P","Q","R"
  3.                                      +"","S","T","U","V","W","X","Y","Z"};
  4.     JButton [] efgh = new JButton[abcd.length];
  5.     JTextField [] input= new JTextField[secretWord.length];
复制代码
  1. for(int i=0;i<secretWord.length;i++)
  2.         {
  3.         input[i] = new JTextField(6);
  4.         test2.add(input[i]);
  5.         }

  6. for(int j=0;j<abcd.length;j++)
  7.         {
  8.         efgh[j] = new JButton(abcd[j]);
  9.         efgh[j].addActionListener(this);
  10.         test3.add(efgh[j]);
  11.         }
复制代码

这个就是我的interface

这个就是我的interface
回复

使用道具 举报


ADVERTISEMENT

发表于 6-12-2013 04:15 PM | 显示全部楼层
我的java都忘清光了。

你的eventlistner 不是会有argument吗?argument里面会装着你要的东西(button's id)
在那个delegate catch就能了.我只会asp.net + c#
回复

使用道具 举报

 楼主| 发表于 6-12-2013 04:19 PM | 显示全部楼层
johnkia 发表于 6-12-2013 04:15 PM
我的java都忘清光了。

你的eventlistner 不是会有argument吗?argument里面会装着你要的东西(button's  ...

谢谢你的回复,你可以给我一个example吗?我不是很懂如果拿回button里面的argument
回复

使用道具 举报

发表于 6-12-2013 05:41 PM | 显示全部楼层
我assume你的String [] abcd里面的都是unique 字母
  1. for(int j=0;j<abcd.length;j++)
  2.         {
  3.         efgh[j] = new JButton(abcd[j]);
  4.           efgh[j].setActionCommand(efgh[j]); //set你要在textfield放的字母

  5.         efgh[j].addActionListener(this);
  6.         test3.add(efgh[j]);
  7.         }
复制代码
然后你的class应该是implements 了ActionListener. so 你的 actionPerformed()是
  1. public void actionPerformed(ActionEvent evt){
  2.        String btnCommand  = evt.getActionCommand(); //这就是你要在textfield放的字母
  3.        for(int z=0;z<input.length;z++){
  4.           if(input[z].getText().equals(""){
  5.                  input[z].setText(btnCommand);
  6.                  break;
  7.            }
  8.        }
  9. }
复制代码
syntax 是google回来的。有错的话,别见怪

本帖最后由 johnkia 于 6-12-2013 05:43 PM 编辑

回复

使用道具 举报

 楼主| 发表于 6-12-2013 06:19 PM | 显示全部楼层
johnkia 发表于 6-12-2013 05:41 PM
我assume你的String [] abcd里面的都是unique 字母然后你的class应该是implements 了ActionListener. so 你 ...

还没看到你回复之前,我自己试做了如下,结果成功了,谢谢你还google帮我找 我还看不懂到底做么会做到的,哈哈
  1. for(int a=0;a<abcd.length;a++)
  2.                 {
  3.                         if(e.getSource()==efgh[a])
  4.                         {
  5.                                 if(i<input.length)
  6.                                 {
  7.                                 input[i].setText(abcd[a]);
  8.                                 i++;
  9. }
  10.                                 }
复制代码
回复

使用道具 举报

 楼主| 发表于 7-12-2013 12:44 PM | 显示全部楼层
johnkia 发表于 6-12-2013 05:41 PM
我assume你的String [] abcd里面的都是unique 字母然后你的class应该是implements 了ActionListener. so 你 ...

想请教你最后一个问题,我做了很久,看不到错在哪里,就是我会拿user input的value来compare跟我一个已经initialization了的array,代码如下,每次compare都出错的display就算我进对的input
  1. for(int a=0;a<abcd.length;a++)
  2.                 {
  3.                         if(e.getSource()==efgh[a])
  4.                         {
  5.                                 efgh[a].setEnabled(false);
  6.                                 if(i<input.length)
  7.                                 {
  8.                                         input[i].setText(abcd[a]);
  9.                                         i++;
  10.                                 }
  11.                                
  12.                                 if(i==input.length)
  13.                                 {       
  14.                                         for(int c=0;c<input.length;c++)
  15.                                         {
  16.                                                 assign=input[c].getText();
  17.                                                 content[c]=assign;
  18.                                                 System.out.println(content[c]);
  19.                                         }
  20.                                 }
  21.                                
  22.                                 if(i==secretWord.length)
  23.                                 {
  24.                                                 if(content[0]==secretWord[0] || content[1]==secretWord[1] || content[2]==secretWord[2] || content[3]==secretWord[3] || content[4]==secretWord[4])
  25.                                                 {
  26.                                                         output1.setText("You are right!!!!");
  27.                                                         result=true;
  28.                                                 }
  29.                                                 else
  30.                                                 {
  31.                                                         output1.setText("Try again");
  32.                                                         result=false;
  33.                                                 }
  34.                                         //}
  35.                                                 /*if(result==true)
  36.                                                 {                                       
  37.                                                         output1.setText("Pandainya!!!!");
  38.                                                 }
  39.                                                 else if(result==false)
  40.                                                 {
  41.                                                         output1.setText("Try again");
  42.                                                 }*/
  43.                                 }
  44.                                
  45.                         }
  46.                  }
  47.                
  48.     }
复制代码
回复

使用道具 举报

Follow Us
发表于 9-12-2013 09:15 AM | 显示全部楼层
你最好post完全部code来看看。。一part一part,看到头痛
回复

使用道具 举报

发表于 9-12-2013 01:55 PM | 显示全部楼层
shern91 发表于 7-12-2013 12:44 PM
想请教你最后一个问题,我做了很久,看不到错在哪里,就是我会拿user input的value来compare跟我一个已经 ...

试试把 content[0..5]==secretWord[0..5] 改成 content[0..5].equals(secretWord[0..5])

== 是identity comparison. equals 比较正确。

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 9-12-2013 02:07 PM | 显示全部楼层
huionn 发表于 9-12-2013 01:55 PM
试试把 content[0..5]==secretWord[0..5] 改成 content[0..5].equals(secretWord[0..5])

== 是identit ...

谢谢你,做到了
回复

使用道具 举报

 楼主| 发表于 9-12-2013 02:08 PM | 显示全部楼层
johnkia 发表于 9-12-2013 09:15 AM
你最好post完全部code来看看。。一part一part,看到头痛

我的code太长了,300多行,所以一part一part问,不过没关系,解决了,谢谢你
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 8-11-2025 08:47 PM , Processed in 0.255471 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表