JComboBox combo1;
String[] s = {"选1","选2"};

    combo1 = new JComboBox(s);
        combo1.addItem("");
        combo1.setBorder(BorderFactory.createTitledBorder("型材"));
请问这个JComboBox combo1;监视怎么写
因为是这样,{"选1","选2"};中调用的不同的方法 还要按下JButton
怎么实现,

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【BAIYMT】截止到2008-07-21 17:11:49的历史汇总数据(不包括此帖):
    发帖的总数量:2                        发帖的总分数:20                       每贴平均分数:10                       
    回帖的总数量:0                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:1                        结贴的总分数:0                        
    无满意结贴数:1                        无满意结贴分:20                       
    未结的帖子数:1                        未结的总分数:20                       
    结贴的百分比:50.00 %               结分的百分比:0.00  %                  
    无满意结贴率:100.00%               无满意结分率:---------------------
    楼主加油
      

  2.   

    添加一个ActionListener阿
    combo1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
    int index = combo1.getSelectedIndex();
    if(0 == index){
    System.out.println("do 选1");

    }else{
    System.out.println("do 选2");
    }
    }

    });
      

  3.   

    在Action里面做判断吧,获得你选择的值
      

  4.   

    首先:class 类名 implements ActionListener,ItemListener
    其次:注册监听器
    JComboBox应是注册ItemListener 即combo1.addItemListener(this);
    JButton 应是ActionListener    即button1.addActionListener(this);
    最后,定义事件处理方法
    public void itemStateChanged(ItemEvent e)//针对JComboBox
    {
     if(combo1.getSelectedIndex()==0) 方法1;
     if(combo1.getSelectedIndex()==1) 方法2;
    }
    public void actionPerformed(ActionEvent e)//针对JButton
    {
     if(e.getSource()==button1) 方法;
    }
     
      

  5.   

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.math.BigDecimal;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    public class Cm  implements ActionListener,ItemListener
    {
    JFrame  f;
    Timer timer; JButton b1;
    JTextArea in;
    JTextField out;
    double n;
    double mj;
    JLabel top,top1,top2;
    JComboBox combo1;


    public void draw()
    {  f=new JFrame("米重计算");
     
     b1=new JButton("确定");
     in=new JTextArea();
    out=new JTextField();


    top1=new JLabel("面积");
    top2=new JLabel("米重");

     String[] s = {"铝材","钢材"};

        combo1 = new JComboBox(s);
            combo1.addItem("");
            combo1.setBorder(BorderFactory.createTitledBorder("型材"));
            
            
            
            
      //按键
    b1.setSize(80,20);
    b1.setLocation(60,130);
    //文本框
    in.setSize(100,20);
    in.setLocation(55,70);

    out.setSize(100,20);
    out.setLocation(55,100);

    top1.setSize(100,30);
    top1.setLocation(10,67);

    top2.setSize(100,30);
    top2.setLocation(10,97);
    //

    combo1.setSize(150,50);
    combo1.setLocation(25,10);

       


    f.setLayout(null);
    f.add(b1);
    f.add(in);
    f.add(out);


    f.add(top1);
    f.add(top2);

    f.add(combo1);
    b1.addActionListener(this);
    b1.setBorder(BorderFactory.createLineBorder(Color.blue,3));
    combo1.addActionListener(new ActionListener(this));
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200,200);
    f.setLocation(150,150);
    f.show();
     
    }



     /*public void actionPerformed(ActionEvent e)
       {  
          if(e.getSource()==b1)
          
          

    n = Long.parseLong(in.getText());

       铝材面积();
    }*/public void itemStateChanged(ItemEvent e)//针对JComboBox 

    if(combo1.getSelectedIndex()==0) 铝材面积(); 
    if(combo1.getSelectedIndex()==1) 钢材面积(); 

    public void actionPerformed(ActionEvent e)//针对JButton 

    if(e.getSource()==button1) n = Long.parseLong(in.getText()); //方法没写我想通过combo1通给方法参数,通过button1获取JTextArea in的值 并进行计算
    } /*
     *hwh2zyq  我编译通不过*/
      //下面是方法  public void 铝材面积(){

          
    double x=2.75,y=1000;


    mj=x/y*n;
    BigDecimal bd = new BigDecimal(mj);
    BigDecimal returnValue = bd.divide(new BigDecimal(1.0), 2, BigDecimal.ROUND_HALF_UP);
    out.setText(returnValue +"  kg/m");
    }
    public void 钢材面积(){

          
    double x=7.85,y=1000;


    mj=x/y*n;
    out.setText(String.valueOf(mj)+"  kg/m(钢材)");
    }




    public static void main(String[] args)
    {

    Cm s= new Cm();
    s.draw();
    }

    }//方法没写我想通过combo1通给方法参数,通过button1获取JTextArea in的值 并进行计算
    要编译通的过