你只要用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();
}
}
运行结果:
循环下就知道了
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();
}
}
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");
}
}
}
}
以上就是关于java如何求JcomboBox中的元素item在对应数组中的下标全部的内容,包括:java如何求JcomboBox中的元素item在对应数组中的下标、JAVA怎么获取点击的BUTTON的数组下标、Java:一个对象数组中某个元素实现了某个接口,如何用instanceof找到该元素的下标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)