java JComboBox 项触发事件问题
发这个帖子是想知道触发项事件,监听接口里要怎么写代码呢?例如我选择3,则输出3看了下面的代码就知道我想问什么了importjava.awt.*;importjavax.s...
发这个帖子是想知道 触发项事件,监听接口里要怎么写代码呢?
例如我选择3,则输出3
看了下面的代码就知道我想问什么了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class heqian extends JFrame
{
public heqian()
{
Container c=getContentPane();
//c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.setLayout(new FlowLayout());
String[] s={"1","2","3","4","5","6"};
JComboBox JL=new JComboBox (s);
JL.setMaximumRowCount(5);
JL.addItemListener(new J_ItemListener());
c.add(JL);
}
class J_ItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
//这里该怎么写,达到我上述的目的呢?????
}
}
public static void main(String[] args)
{
heqian app=new heqian();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(360,150);
app.setVisible(true);
}
} 展开
例如我选择3,则输出3
看了下面的代码就知道我想问什么了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class heqian extends JFrame
{
public heqian()
{
Container c=getContentPane();
//c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.setLayout(new FlowLayout());
String[] s={"1","2","3","4","5","6"};
JComboBox JL=new JComboBox (s);
JL.setMaximumRowCount(5);
JL.addItemListener(new J_ItemListener());
c.add(JL);
}
class J_ItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
//这里该怎么写,达到我上述的目的呢?????
}
}
public static void main(String[] args)
{
heqian app=new heqian();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(360,150);
app.setVisible(true);
}
} 展开
2个回答
2009-12-07
展开全部
package df;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class heqian extends JFrame
{
//变量在其它地方用建议写这
String[] s={"1","2","3","4","5","6"};
JComboBox JL=new JComboBox (s);
public heqian()
{
Container c=getContentPane();
//c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.setLayout(new FlowLayout());
JL.setMaximumRowCount(5);
JL.addItemListener(new ItemListener() {
//处理部分如下
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange()==ItemEvent.SELECTED)
{
System.out.println(JL.getSelectedItem());
}
}
});
c.add(JL);
}
public static void main(String[] args)
{
heqian app=new heqian();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(360,150);
app.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class heqian extends JFrame
{
//变量在其它地方用建议写这
String[] s={"1","2","3","4","5","6"};
JComboBox JL=new JComboBox (s);
public heqian()
{
Container c=getContentPane();
//c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
c.setLayout(new FlowLayout());
JL.setMaximumRowCount(5);
JL.addItemListener(new ItemListener() {
//处理部分如下
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange()==ItemEvent.SELECTED)
{
System.out.println(JL.getSelectedItem());
}
}
});
c.add(JL);
}
public static void main(String[] args)
{
heqian app=new heqian();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(360,150);
app.setVisible(true);
}
}
参考资料: http://hi.csdn.net/zhao_999
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.out.println(JL.getSelectedItem().toString);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |