
循环下就知道了
public class TestFrame extends JFrame implements ActionListener{
JButton bt[][]=new JButton[3][3];
public TestFrame(){
init();
}
private void init(){
thissetLayout(new GridLayout(3,3));
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
bt[i][j]= new JButton(i+"行"+j+"列");
bt[i][j]addActionListener(this);
thisadd(bt[i][j]);
}
}
thissetSize(400, 300);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetLocationRelativeTo(null); //居中显示
thissetResizable(false);
thissetVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(egetSource()==bt[i][j]){
JOptionPaneshowMessageDialog(this, "点击了"+i+"行"+j+"列按钮");
}
}
}
}
public static void main(String args[]){
new TestFrame();
}
}
你只要用cb1getSelectedIndex();就可以取出元素item在对应数组中的下标了。
完整的Java程序如下:
import javaawtFlowLayout;import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJComboBox;
import javaxswingJFrame;
import javaxswingJTextField;
public class Test001 extends JFrame{
String[] b1=new String[]{"请选择站点","双港站","蛟桥站","长江路站","珠江路站","庐山南大道站","绿茵路站","会展路站","地铁大厦站","秋水广场站",
"滕王阁站","万寿宫站","八一馆站","八一广场站","丁公路北站","文教路南站","彭家桥站","谢家村站","青山湖大道站",
"高新大道站","艾溪湖西站","艾溪湖东站","太子殿站","奥体中心站","瑶湖西站",};
JComboBox cb1=new JComboBox(b1);
JTextField tf=new JTextField(10);
Test001(){
add(cb1);add(tf);
cb1addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String item1=(String)cb1getSelectedItem();
tfsetText(cb1getSelectedIndex()+"-->"+item1);
}
});
setLayout(new FlowLayout());
setSize(300, 200);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Test001();
}
}
运行结果:
楼上的方法会产生数组越界。应该用这样的方式分别得到一维和二维的,先定义一个随即对象,并填入种子:Random rand=new Random();而后开始产生维数:一维:randnextInt(alength),二维:randnextInt(a[0]length),这样就能从数组中取得随机元素了。int c=a[randnextInt(alength)][randnextInt(a[0]length)]
其实很简单,完全不用用集合,只要一找好就print或组String,多馀的内存也可以省下
public class Run {
private final static int [] temp = {1,2,5,8,6,2,11,15,2,6,2,11,15,5,1,8,6};
public static void main(String[] args) {
Run r = new Run();
rlocateAllAndPrint();
}
private void locateAllAndPrint () {
for (int i = 0; i < templength; i++) {
int h = temp[i];
boolean repeated = false;
for (int j = 0; j < i; j++) {
if (temp[j] == h) {
repeated = true;
break;
}
}
if (!repeated) {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < templength; j++) {
if (h == temp[j]) {
sbappend(", ")append(j);
}
}
sbdelete(0, 2)insert(0, " --> [")insert(0, h)append(']');
Systemoutprintln(sbtoString());
}
}
}
}
解释一下,第一个 j 的 loop是用来防止print重覆的数字
第二个 j 的 loop是用来组一串string,串好了才print,这样可以减少print的次数,delay也可以减少,当然,如果你喜欢的话,你可以直接print,不需要用stringbuffer
import javaio;
public class ShowDemo{
public static void main(String[] args){
File f[] = {new File("d:\\"),new File("E:\\")};
for(int i = 0;i<flength;i++){
if(f[i] instanceof Object){
Systemoutprintln("数组f的第"+i+"个对象实现了Object");
}
}
}
}
下标其实就是一个标示意思,让你通过下标来获得你想获得的数组中的一个值。
例如:
int
a[]
=
{10,20,30,40,50};
下标
从0开始
,也就是
0,1,2,3,4
所以如果你要取第一个值就是
a[0],第三个值就是a[2]
因为下标从0开始,所以你取的值是第几个减去1
就是下标的数
以上就是关于JAVA怎么获取点击的BUTTON的数组下标全部的内容,包括:JAVA怎么获取点击的BUTTON的数组下标、java如何求JcomboBox中的元素item在对应数组中的下标、JAVA 怎么得到数组的随机下标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)