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 去选具体触发了哪个
谢谢~~ 展开
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 去选具体触发了哪个
谢谢~~ 展开
2个回答
展开全部
下面的代码请参考: 记得给点奖赏哦:
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()这个的值来做为你的条件进行处理.
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()这个的值来做为你的条件进行处理.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
独立的 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.
你想触发哪个事件,看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.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询