如何在Choice上创建一个函数来改变其他Choice的内容

时间:2012-03-15 12:18:39

标签: java swing awt

我已经尝试过ActionListener和ItemListener,但它不会工作,我也尝试过搜索可能适用的代码和方法,但我发现它主要可能是因为JCombobox更受欢迎,因此。

这是Java程序的形式。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class HarderCode extends Frame {

    Choice Cb1;
    Choice Cb2;
    Choice Cb3;

    Label lbl1;
    Label lbl2;
    Label lbl3;
    Label lbl4;
    Label lbl5;

    TextField txt1;
//  TextField txt2;

    Button btn1;


    public HarderCode(){
        btn1 = new Button(" Click Me ! To Convert ");

        Cb1 = new Choice();
        Cb2 = new Choice();
        Cb3 = new Choice();

        Cb1.add("Select A Category");
        Cb1.add("Teperature");
        Cb1.add("Volume");
        Cb1.add("Area");
        Cb1.add("Length");
        Cb1.add("Weigth");

        Cb2.add("Select Unit");
        Cb2.add("Celsius");
        Cb2.add("Fahrenheit");
        Cb2.add("Kelvin");

        Cb3.add("Select Unit");
        Cb3.add("Celsius");
        Cb3.add("Fahrenheit");
        Cb3.add("Kelvin");

        lbl1 = new Label(" 10000.00 ");
        lbl2 = new Label(" From ");
        lbl3 = new Label(" To ");
        lbl4 = new Label(" Result    :");
        lbl5 = new Label(" Value to be Converted ");

        txt1 = new TextField(7);
//      txt2 = new TextField(7);

        add(lbl1);
        add(lbl2);
        add(lbl3);
        add(lbl4);
        add(lbl5);
        add(txt1);
//      add(txt2);
        add(Cb1);
        add(Cb2);
        add(Cb3);
        add(btn1);

        @Override
        Cb1.addItemListener(new ItemListener(){

            public void actionPerformed(ItemEvent e){

                if(Cb1.getSelectedItem().equals("Teperature"));

                    System.out.print("lallalala");
        }
        }   
            );
    }

    //30

    public void paint(Graphics HarderCode){

        this.Cb1.setSize(270,20);//
        this.Cb1.setLocation(10,40);//

        this.Cb2.setSize(130,2);//
        this.Cb2.setLocation(10,95);//

        this.Cb3.setSize(130,2);//
        this.Cb3.setLocation(150,95);//

        this.lbl1.setSize(100,20);//
        this.lbl1.setLocation(150,230);//

        this.lbl2.setSize(50,20);//
        this.lbl2.setLocation(10,70);//

        this.lbl3.setSize(50,20);//
        this.lbl3.setLocation(150,70);//

        this.lbl5.setSize(130,20);//
        this.lbl5.setLocation(10,130);//

        this.txt1.setSize(130,20);//
        this.txt1.setLocation(150,130);//

        this.btn1.setSize(270,60);//
        this.btn1.setLocation(10,160);//

        this.lbl4.setSize(80,20);//
        this.lbl4.setLocation(40,230);//

//      this.txt2.setSize(100,30);
//      this.txt2.setLocation(350,200);


    }

    public static void main (String[] args) {


        HarderCode HC = new HarderCode();

        HC.setTitle("MultiConverter Ver1.0");
        HC.setSize(290,300);
        HC.setResizable(false);
        HC.setVisible(true);

        /*

        HC.setBackground(Color.YELLOW);
        HC.lbl2.setBackground(Color.YELLOW);
        HC.lbl3.setBackground(Color.YELLOW);
        HC.lbl1.setBackground(Color.YELLOW);
        HC.lbl4.setBackground(Color.YELLOW);
        HC.lbl5.setBackground(Color.YELLOW);

        HC.Cb1.setBackground(Color.BLUE);
        HC.Cb2.setBackground(Color.BLUE);
        HC.Cb3.setBackground(Color.BLUE);

        HC.btn1.setBackground(Color.PINK);

        HC.txt1.setBackground(Color.BLUE);

        */
 }

}

1 个答案:

答案 0 :(得分:2)

班级java.awt.Choice没有单独的数据模型,因此您必须根据需要使用removeAll()add()。相比之下,javax.swing.JComboBox separable model可以动态更改,如here所示。