java gui JCombo 事件触发; 5

privatestaticString[]filename={"matrix2","matrix3","matrix4"}box=newJComboBox(filenam... private static String[] filename = {"matrix2","matrix3","matrix4"}
box = new JComboBox(filename);
add(box);

定义了3个box...
然后创建了个handler class

private class thehandler implements ActionListener {

public void actionPerformed(ActionEvent event){

if(event.getSource()==box);
calculate.calculation();

}
}
}

大概就这样,我想触发不同的box...这么做具体操作。。。用string 去选具体触发了哪个

谢谢~~
展开
 我来答
Leo_8609
2012-05-03 · TA获得超过142个赞
知道小有建树答主
回答量:152
采纳率:0%
帮助的人:126万
展开全部
下面的代码请参考:  记得给点奖赏哦:
class JComboboxTest extends JFrame implements ActionListener{
private static String[] filename = {"matrix1","matrix2","matrix3"};
DefaultComboBoxModel m=new DefaultComboBoxModel(filename);
JComboBox box = new JComboBox(m);

private JPanel p;

public JComboboxTest() {
box.addActionListener(this);
p = new JPanel();
p.add(box);
this.add(p);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == box) {
int index = box.getSelectedIndex();
switch (index) {
case 0:
System.out.println(box.getSelectedItem().toString());
break;
case 1:
System.out.println(box.getSelectedItem().toString());
break;
case 2:
System.out.println(box.getSelectedItem().toString());
break;
}
}
}

public static void main(String[] args) {
new JComboboxTest();
}
}

如果你想用字符来判断的话, 你可以根据box.getSelectedItem().toString()这个的值来做为你的条件进行处理.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
bd9006
2012-04-26 · TA获得超过2.5万个赞
知道大有可为答主
回答量:4.8万
采纳率:63%
帮助的人:1.6亿
展开全部
独立的 handler class,过程中不需要 if(event.getSource()==box);。

你想触发哪个事件,看API文档,除了下面的,还有继承而来的事件
java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by javax.swing.JComponent
extended by javax.swing.JComboBox

void actionPerformed(ActionEvent e)
This method is public as an implementation side effect.
protected void actionPropertyChanged(Action action, String propertyName)
Updates the combobox's state in response to property changes in associated action.
void addActionListener(ActionListener l)
Adds an ActionListener.
void addItem(Object anObject)
Adds an item to the item list.
void addItemListener(ItemListener aListener)
Adds an ItemListener.
void addPopupMenuListener(PopupMenuListener l)
Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box.
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式