有关JComboBox

behappy9479 2004-07-04 12:06:44
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Boxtest implements ActionListener{

public Boxtest() {
static int n;
static String[] items ={"one","two","three","four","five"};
JFrame f = new JFrame();
JComboBox combo=new JComboBox(items));
JButton buttonok=new JButton("Button ok"));

Container contentPane = f.getContentPane();
Box baseBox = Box.createHorizontalBox();
contentPane.add(baseBox);
baseBox.add(combo);
baseBox.add(buttonok);

f.setTitle("Boxtest");
f.setSize(new Dimension(200,200));
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonOK){
n = combo.getSelectedIndex();
f.setVisible(false);
}
}

//返回选中的项目的值
public static String getItems(){
....??如何实现?
}

}


我想通过内部函数public static String getItems()取得选择的项目值,该如何实现呢?
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qkaiwei 2004-07-06
  • 打赏
  • 举报
回复
JList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
Object value = list.getSelectedItem();
}
});
behappy9479 2004-07-05
  • 打赏
  • 举报
回复
问题是,
public static String getItems(){
n = combo.getSelectedIndex();
return items[n];
}
这样得到的只是第一次没有选择过时的项目值
如果想在按下确定按钮后,返回当前选定的项目,该怎么办呢?
会Java的蝈蝈 2004-07-04
  • 打赏
  • 举报
回复
JComboBox中的Object getSelectedItem()可以直接得到被选择的对象!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧