如何实现我扩展到Frame而不是JFrame的类?

时间:2012-03-15 20:26:50

标签: java awt implementation

我无法实现这个课程。计划是从两个子选择框中选择单位然后单击按钮,结果必须出现在cmd中。当我编译这个类时,它有一个错误,说

HarderCode不是抽象的,并且不会覆盖java.awt.event.ItemListener中的抽象方法itemStateChanged(java.awt.event.ItemEvent)

import java.awt.event.ActionListener;

import java.awt.event.*;

import java.awt.*;

public class HarderCode extends Frame implements ItemListener,ActionListener{


    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();

        int i;


        String [] Ctgy = {"Select A Category", "Volume", "Length", "Teperature", "Area", "Weigth"};




        for (i = 0; i < Ctgy.length; ++i) {
      Cb1.insert (Ctgy [i], i);
        }
                Cb2.add("Select Unit");
                Cb3.add("Select Unit");



        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(Cb1);
        add(Cb2);
        add(Cb3);
        add(btn1);

        Cb1.addItemListener (this);
        Cb2.addItemListener (this);
        Cb3.addItemListener (this);
        btn1.addActionListener(this);

    }
    @Override
    public void itemStateChanged(ItemEvent e){

            int Selection;
            int Selection2;
            int Selection3;
        Selection = Cb1.getSelectedIndex();
        Selection2 = Cb2.getSelectedIndex();
        Selection3 = Cb3.getSelectedIndex();



        if(Selection == 0){

            Cb2.removeAll();
            Cb3.removeAll();

            String [] def1 = {"Select Unit",};
            String [] def2 = {"Select Unit",};

            for (int i = 0; i < def1.length; ++i) {
      Cb2.insert (def1 [i], i);
        }

         for (int i = 0; i < def2.length; ++i) {
      Cb3.insert (def2 [i], i);
    }
        }else if(Selection == 1){

                Cb2.removeAll();
                Cb3.removeAll();

        String [] temp1 = {"Select Unit", "Celsius", "Fahrenheit", "Kelvin"};
        String [] temp2 = {"Select Unit", "Celsius", "Fahrenheit", "Kelvin"};


                for (int i = 0; i < temp1.length; ++i) {
             Cb2.insert (temp1 [i], i);
            }

                 for (int i = 0; i < temp2.length; ++i) {
               Cb3.insert (temp2 [i], i);
            }
        }
    }

    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);//
    }

    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);

}
*/  
}

2 个答案:

答案 0 :(得分:1)

它应该工作,代码没有任何问题。你正在实现ItemListener接口,如果你已经实现了任何接口,你必须实现该接口的所有方法。 itemStateChanged是ItemListener的方法之一。因此,您必须覆盖它并在HarderCode类中提供相同的实现。

答案 1 :(得分:1)

FrameJFrame无关。您声明您的类要实现ItemListener,但您没有实现其itemStateChanged方法。