转换现有的applet介绍可执行jar文件

时间:2014-01-25 14:02:54

标签: java applet

我在学校学习java所以我做了一些任务,但我想能够制作一个可执行文件,所以我可以把它发送给一些朋友来展示我做的。我在这个网站上发现你必须使用JFrames和类似的东西,但我不知道它是如何工作的。做了一些研究,但我找不到一个简单的方法来转换它,而无需重做几乎所有的代码。我的代码如下,有人可以帮助我吗?

AEnterprise

    package jpr_keuze;
    import java.applet.*;
    import java.awt.*;

    public class Keuze extends Applet {
    Choice kleur, vorm;
    Label titel, lblKleur, lblVorm, parameters, lblParameter1, lblParameter2, lblParameter3, lblParameter4, lblParameter5, lblParameter6;
    TextField parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, tekst;
    Button teken, vergrendel1, vergrendel2;
    Integer intParameter1, intParameter2, intParameter3, intParameter4, intParameter5, intParameter6, x, y, lengte, grootsteX, grootsteY, kleinsteX, kleinsteY, intParameter7, intParameter8, intParameter9, intParameter10, intParameter11, intParameter12, intVorm, intKleur, intParameter13, intParameter14, intParameter15, intParameter16, intParameter17, intParameter18, intKleur2, intVorm2;
    Boolean draw, draw2, draw3;
    Polygon driehoek, driehoek2, driehoek3;
    String strTekst, strTekst2, strTekst3;

    public void init(){

        draw = false;
        draw2 = false;
        draw3 = false;

        kleur = new Choice();
        vorm = new Choice();
        titel = new Label("Objecten tekenen");
        lblKleur = new Label("Kies een kleur: ");
        lblVorm = new Label("Kies een vorm: ");
        parameters = new Label("Parameters");
        parameter1 = new TextField("1");
        parameter2 = new TextField("1");
        parameter3 = new TextField("1");
        parameter4 = new TextField("1");
        parameter5 = new TextField("1");
        parameter6 = new TextField("1");
        teken = new Button("Teken de gekleurde vorm");
        driehoek = new Polygon();
        tekst = new TextField();
        lblParameter1 = new Label();
        lblParameter2 = new Label();
        lblParameter3 = new Label();
        lblParameter4 = new Label();
        lblParameter5 = new Label();
        lblParameter6 = new Label();
        vergrendel1 = new Button("Vergrendel 1");
        vergrendel2 = new Button("Vergrendel 2");
        driehoek2 = new Polygon();
        driehoek3 = new Polygon();

        add(kleur);
        add(vorm);
        add(titel);
        add(lblKleur);
        add(lblVorm);
        add(parameters);
        add(parameter1);
        add(parameter2);
        add(parameter3);
        add(parameter4);
        add(parameter5);
        add(parameter6);
        add(teken);
        add(tekst);
        add(lblParameter1);
        add(lblParameter2);
        add(lblParameter3);
        add(lblParameter4);
        add(lblParameter5);
        add(lblParameter6);
        add(vergrendel1);
        add(vergrendel2);


        kleur.add("Rood");//0
        kleur.add("Groen");//1
        kleur.add("Blauw");//2
        kleur.add("Oranje");//3
        kleur.add("Lichtblauw");//4
        kleur.add("Geel");//5
        kleur.add("Zwart");//6
        kleur.add("Wit");//7
        kleur.add("Roze");//8
        kleur.add("Magenta");//9

        vorm.add("Lijnstuk");//0
        vorm.add("Rechthoek");//1
        vorm.add("Ovaal");//2
        vorm.add("Driehoek");//3
        vorm.add("Rechthoek met afgeronde hoeken");//4

        vergrendel1.setBackground(Color.green);
        vergrendel2.setBackground(Color.green);

    }
    public void paint(Graphics g){
        setSize(750,750);

        intParameter1 = Integer.parseInt(parameter1.getText());
        intParameter2 = Integer.parseInt(parameter2.getText());
        intParameter3 = Integer.parseInt(parameter3.getText());
        intParameter4 = Integer.parseInt(parameter4.getText());
        intParameter5 = Integer.parseInt(parameter5.getText());
        intParameter6 = Integer.parseInt(parameter6.getText());
        strTekst = tekst.getText();

        grootsteX = 0;
        grootsteY = 0;
        kleinsteX = 0;
        kleinsteY = 0;


        setBackground(Color.lightGray);
        lblKleur.setBackground(Color.lightGray);
        lblVorm.setBackground(Color.lightGray);
        parameters.setBackground(Color.lightGray);
        teken.setBackground(Color.cyan);
        titel.setBackground(Color.red);
        titel.setBounds(0, 0, 750, 25);
        titel.setAlignment(Label.CENTER);
        lblParameter1.setBackground(Color.lightGray);
        lblParameter2.setBackground(Color.lightGray);
        lblParameter3.setBackground(Color.lightGray);
        lblParameter4.setBackground(Color.lightGray);
        lblParameter5.setBackground(Color.lightGray);
        lblParameter6.setBackground(Color.lightGray);

        lblKleur.setBounds(150, 50, 100, 25);
        lblVorm.setBounds(150, 75, 100, 25);
        parameters.setBounds(150, 105, 100, 25);
        kleur.setBounds(250, 50, 100, 25);
        vorm.setBounds(250, 75, 250, 25);
        parameter1.setBounds(370, 105, 50, 20);
        parameter2.setBounds(550, 105, 50 ,20);
        parameter3.setBounds(370, 130, 50, 20);
        parameter4.setBounds(550, 130, 50, 20);
        parameter5.setBounds(370, 155, 50, 20);
        parameter6.setBounds(550, 155, 50, 20);
        teken.setBounds(250, 230, 200, 25);
        tekst.setBounds(250, 190, 200, 20);
        lblParameter1.setBounds(250, 105, 120, 20);
        lblParameter2.setBounds(430, 105, 120, 20);
        lblParameter3.setBounds(250, 130, 120, 20);
        lblParameter4.setBounds(430, 130, 120, 20);
        lblParameter5.setBounds(250, 155, 120, 20);
        lblParameter6.setBounds(430, 155, 120, 20);
        vergrendel1.setBounds(0, 230, 200, 25);
        vergrendel2.setBounds(500, 230, 200, 25);

        lblParameter1.setText("X1: ");
        lblParameter2.setText("Y1: ");

        lblParameter1.setAlignment(Label.RIGHT);
        lblParameter2.setAlignment(Label.RIGHT);
        lblParameter3.setAlignment(Label.RIGHT);
        lblParameter4.setAlignment(Label.RIGHT);
        lblParameter5.setAlignment(Label.RIGHT);
        lblParameter6.setAlignment(Label.RIGHT);

        driehoek.reset();
        driehoek2.reset();
        driehoek3.reset();

        if (draw == true){
            switch (kleur.getSelectedIndex()){
            case 0: g.setColor(Color.red);
                    break;
            case 1: g.setColor(Color.green);
                    break;
            case 2: g.setColor(Color.blue);
                    break;
            case 3: g.setColor(Color.orange);
                    break;
            case 4: g.setColor(Color.cyan);
                    break;
            case 5: g.setColor(Color.yellow);
                    break;
            case 6: g.setColor(Color.black);
                    break;
            case 7: g.setColor(Color.white);
                    break;
            case 8: g.setColor(Color.pink);
                    break;
            case 9: g.setColor(Color.magenta);
                    break;
        }
            switch (vorm.getSelectedIndex()){
            case 0: g.drawLine(intParameter1, intParameter2, intParameter3, intParameter4);
                    g.setColor(Color.darkGray);
                    g.drawString(intParameter1 + " , " + intParameter2, intParameter1 - 5, intParameter2 - 5);
                    g.drawString(intParameter3 + " , " + intParameter4, intParameter3 + 5, intParameter4 - 5);
                    grootsteX = intParameter1;
                    kleinsteX = intParameter3;
                    if (intParameter3>grootsteX){
                        grootsteX = intParameter3;
                        kleinsteX = intParameter1;
                    }

                    grootsteY = intParameter2;
                    kleinsteY = intParameter4;
                    if (intParameter4>grootsteY){
                        grootsteY = intParameter4;
                        kleinsteY = intParameter2;
                    }
                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    break;

            case 1: g.fillRect(intParameter1, intParameter2, intParameter3, intParameter4);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter1 + " , " +  intParameter2, intParameter1 - 5, intParameter2 - 5);
                    g.drawString(intParameter3.toString(), intParameter1 + intParameter3 /2, intParameter2 + intParameter4 + 25);
                    g.drawString(intParameter4.toString(), intParameter1 + intParameter3 + 5, intParameter2 + intParameter4 /2);
                    x = intParameter1 + (intParameter3 / 2);
                    y = intParameter2 + (intParameter4 / 2);
                    break;

            case 2: g.fillOval(intParameter1, intParameter2, intParameter3, intParameter4);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter1 + " , " + intParameter2, intParameter1-25, intParameter2 - 25);
                    g.drawString(intParameter3.toString(), intParameter1 + intParameter3/2, intParameter2 + intParameter4 + 25);
                    g.drawString(intParameter4.toString(), intParameter1 + intParameter3 + 5, intParameter2 + intParameter4/2);

                    x = intParameter1 + (intParameter3 / 2);
                    y = intParameter2 + (intParameter4 / 2);
                    break;

            case 3: driehoek.addPoint(intParameter1, intParameter2);
                    driehoek.addPoint(intParameter3, intParameter4);
                    driehoek.addPoint(intParameter5, intParameter6);
                    g.fillPolygon(driehoek);

                    grootsteX = intParameter1;
                    if (intParameter3>grootsteX)
                        grootsteX = intParameter3;
                    if (intParameter5>grootsteX)
                        grootsteX = intParameter5;

                    kleinsteX = intParameter1;
                    if (intParameter3<kleinsteX)
                        kleinsteX = intParameter3;
                    if (intParameter5<kleinsteX)
                        kleinsteX = intParameter5;

                    grootsteY = intParameter2;
                    if (intParameter4>grootsteY)
                        grootsteY = intParameter4;
                    if (intParameter6>grootsteY)
                        grootsteY = intParameter6;

                    kleinsteY = intParameter2;
                    if(intParameter4<kleinsteY)
                        kleinsteY = intParameter4;
                    if(intParameter6<kleinsteY)
                        kleinsteY = intParameter6;  

                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter1 + " , "  + intParameter2, intParameter1 - 25, intParameter2 - 25);
                    g.drawString(intParameter3 + " , " + intParameter4, intParameter3 - 25, intParameter4 - 25);
                    g.drawString(intParameter5 + " , " + intParameter6, intParameter5 - 10, intParameter6 - 25);

                    break;
            case 4: g.fillRoundRect(intParameter1, intParameter2, intParameter3, intParameter4, intParameter5, intParameter6);

                    x = intParameter1 + (intParameter3 / 2);
                    y = intParameter2 + (intParameter4 / 2);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter1 + " , " + intParameter2, intParameter1 - 5, intParameter2 - 10);
                    g.drawString(intParameter3.toString(), intParameter1 + intParameter3 /2, intParameter2 + intParameter4 + 25);
                    g.drawString(intParameter4.toString(), intParameter1 + intParameter3 + 5, intParameter2 + intParameter4 /2);
                    g.drawString(intParameter5.toString(), intParameter1 + 30, intParameter2 + 15);
                    g.drawString(intParameter6.toString(), intParameter1 + 15, intParameter2 + 30);

                    break;
            }
            g.setColor(Color.darkGray);
            lengte = (strTekst.length() * 5)/2;
            g.drawString(strTekst, x - lengte, y);
        }
            switch(vorm.getSelectedIndex()){
            case 0: lblParameter3.setText("X2: ");
                    lblParameter4.setText("Y2: ");
                    lblParameter5.setText("");
                    lblParameter6.setText("");
                    parameter5.setVisible(false);
                    parameter6.setVisible(false);                   
                    break;
            case 1: lblParameter3.setText("Breedte: ");
                    lblParameter4.setText("Hoogte: ");
                    lblParameter5.setText("");
                    lblParameter6.setText("");
                    parameter5.setVisible(false);
                    parameter6.setVisible(false);
                    break;
            case 2: lblParameter3.setText("Breedte: ");
                    lblParameter4.setText("Hoogte: ");
                    lblParameter5.setText("");
                    lblParameter6.setText("");
                    parameter5.setVisible(false);
                    parameter6.setVisible(false);
                    break;
            case 3: lblParameter3.setText("X2: ");
                    lblParameter4.setText("Y2: ");
                    lblParameter5.setText("X3: ");
                    lblParameter6.setText("Y3: ");
                    parameter5.setVisible(true);
                    parameter6.setVisible(true);
            case 4: lblParameter3.setText("Breedte: ");
                    lblParameter4.setText("Hoogte: ");
                    lblParameter5.setText("Breedte ronde hoek: ");
                    lblParameter6.setText("Hoogte ronde hoek: ");
                    parameter5.setVisible(true);
                    parameter6.setVisible(true);
                    break;
            }

        draw = false;

        if (draw2 == true){
            switch (intKleur){
            case 0: g.setColor(Color.red);
                    break;
            case 1: g.setColor(Color.green);
                    break;
            case 2: g.setColor(Color.blue);
                    break;
            case 3: g.setColor(Color.orange);
                    break;
            case 4: g.setColor(Color.cyan);
                    break;
            case 5: g.setColor(Color.yellow);
                    break;
            case 6: g.setColor(Color.black);
                    break;
            case 7: g.setColor(Color.white);
                    break;
            case 8: g.setColor(Color.pink);
                    break;
            case 9: g.setColor(Color.magenta);
                    break;
        }
            switch (intVorm){
            case 0: g.drawLine(intParameter7, intParameter8, intParameter9, intParameter10);
                    g.setColor(Color.darkGray);
                    g.drawString(intParameter7 + " , " + intParameter8, intParameter7 - 5, intParameter8 - 5);
                    g.drawString(intParameter9 + " , " + intParameter10, intParameter9 + 5, intParameter10 - 5);
                    grootsteX = intParameter7;
                    kleinsteX = intParameter9;
                    if (intParameter9>grootsteX){
                        grootsteX = intParameter9;
                        kleinsteX = intParameter7;
                    }

                    grootsteY = intParameter8;
                    kleinsteY = intParameter10;
                    if (intParameter10>grootsteY){
                        grootsteY = intParameter10;
                        kleinsteY = intParameter8;
                    }
                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    break;

            case 1: g.fillRect(intParameter7, intParameter8, intParameter9, intParameter10);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter7 + " , " +  intParameter8, intParameter7 - 5, intParameter8 - 5);
                    g.drawString(intParameter9.toString(), intParameter7 + intParameter9 /2, intParameter7 + intParameter10 + 25);
                    g.drawString(intParameter10.toString(), intParameter7 + intParameter9 + 5, intParameter8 + intParameter10 /2);
                    x = intParameter7 + (intParameter9 / 2);
                    y = intParameter8 + (intParameter10 / 2);
                    break;

            case 2: g.fillOval(intParameter7, intParameter8, intParameter9, intParameter10);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter7 + " , " + intParameter8, intParameter7-25, intParameter8 - 25);
                    g.drawString(intParameter9.toString(), intParameter7 + intParameter9/2, intParameter8 + intParameter10 + 25);
                    g.drawString(intParameter10.toString(), intParameter7 + intParameter9 + 5, intParameter8 + intParameter10/2);

                    x = intParameter7 + (intParameter9 / 2);
                    y = intParameter8 + (intParameter10 / 2);
                    break;

            case 3: driehoek2.addPoint(intParameter7, intParameter8);
                    driehoek2.addPoint(intParameter9, intParameter10);
                    driehoek2.addPoint(intParameter11, intParameter12);
                    g.fillPolygon(driehoek2);

                    grootsteX = intParameter7;
                    if (intParameter9>grootsteX)
                        grootsteX = intParameter9;
                    if (intParameter11>grootsteX)
                        grootsteX = intParameter11;

                    kleinsteX = intParameter7;
                    if (intParameter9<kleinsteX)
                        kleinsteX = intParameter9;
                    if (intParameter11<kleinsteX)
                        kleinsteX = intParameter11;

                    grootsteY = intParameter8;
                    if (intParameter10>grootsteY)
                        grootsteY = intParameter10;
                    if (intParameter12>grootsteY)
                        grootsteY = intParameter12;

                    kleinsteY = intParameter8;
                    if(intParameter10<kleinsteY)
                        kleinsteY = intParameter10;
                    if(intParameter12<kleinsteY)
                        kleinsteY = intParameter12; 

                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter7 + " , "  + intParameter8, intParameter7 - 25, intParameter8 - 25);
                    g.drawString(intParameter9 + " , " + intParameter10, intParameter9 - 25, intParameter10 - 25);
                    g.drawString(intParameter11 + " , " + intParameter12, intParameter11 - 10, intParameter12 - 25);

                    break;
            case 4: g.fillRoundRect(intParameter7, intParameter8, intParameter9, intParameter10, intParameter11, intParameter12);

                    x = intParameter7 + (intParameter9 / 2);
                    y = intParameter8 + (intParameter10 / 2);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter7 + " , " + intParameter8, intParameter7 - 5, intParameter8 - 10);
                    g.drawString(intParameter9.toString(), intParameter7 + intParameter9 /2, intParameter8 + intParameter10 + 25);
                    g.drawString(intParameter10.toString(), intParameter7 + intParameter9 + 5, intParameter8 + intParameter10 /2);
                    g.drawString(intParameter11.toString(), intParameter7 + 30, intParameter8 + 15);
                    g.drawString(intParameter12.toString(), intParameter7 + 15, intParameter8 + 30);

                    break;
            }
            g.setColor(Color.darkGray);
            lengte = (strTekst2.length() * 5)/2;
            g.drawString(strTekst2, x - lengte, y);
        }

        if (draw3 == true){
            switch (intKleur2){
            case 0: g.setColor(Color.red);
                    break;
            case 1: g.setColor(Color.green);
                    break;
            case 2: g.setColor(Color.blue);
                    break;
            case 3: g.setColor(Color.orange);
                    break;
            case 4: g.setColor(Color.cyan);
                    break;
            case 5: g.setColor(Color.yellow);
                    break;
            case 6: g.setColor(Color.black);
                    break;
            case 7: g.setColor(Color.white);
                    break;
            case 8: g.setColor(Color.pink);
                    break;
            case 9: g.setColor(Color.magenta);
                    break;
        }
            switch (intVorm2){
            case 0: g.drawLine(intParameter13, intParameter14, intParameter15, intParameter16);
                    g.setColor(Color.darkGray);
                    g.drawString(intParameter13 + " , " + intParameter14, intParameter13 - 5, intParameter14 - 5);
                    g.drawString(intParameter15 + " , " + intParameter16, intParameter15 + 5, intParameter16 - 5);
                    grootsteX = intParameter13;
                    kleinsteX = intParameter15;
                    if (intParameter15>grootsteX){
                        grootsteX = intParameter15;
                        kleinsteX = intParameter13;
                    }
                    grootsteY = intParameter14;
                    kleinsteY = intParameter16;
                    if (intParameter16>grootsteY){
                        grootsteY = intParameter16;
                        kleinsteY = intParameter14;
                    }
                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    break;

            case 1: g.fillRect(intParameter13, intParameter14, intParameter15, intParameter16);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter13 + " , " +  intParameter14, intParameter13 - 5, intParameter14 - 5);
                    g.drawString(intParameter15.toString(), intParameter13 + intParameter15 /2, intParameter13 + intParameter16 + 25);
                    g.drawString(intParameter16.toString(), intParameter13 + intParameter15 + 5, intParameter14 + intParameter16 /2);
                    x = intParameter7 + (intParameter15 / 2);
                    y = intParameter8 + (intParameter16 / 2);
                    break;

            case 2: g.fillOval(intParameter13, intParameter14, intParameter15, intParameter16);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter13 + " , " + intParameter14, intParameter13-25, intParameter14 - 25);
                    g.drawString(intParameter15.toString(), intParameter13 + intParameter15/2, intParameter14 + intParameter16 + 25);
                    g.drawString(intParameter16.toString(), intParameter13 + intParameter15 + 5, intParameter14 + intParameter16/2);

                    x = intParameter13 + (intParameter15 / 2);
                    y = intParameter14 + (intParameter16 / 2);
                    break;

            case 3: driehoek3.addPoint(intParameter13, intParameter14);
                    driehoek3.addPoint(intParameter15, intParameter16);
                    driehoek3.addPoint(intParameter17, intParameter18);
                    g.fillPolygon(driehoek3);

                    grootsteX = intParameter13;
                    if (intParameter15>grootsteX)
                        grootsteX = intParameter15;
                    if (intParameter17>grootsteX)
                        grootsteX = intParameter17;

                    kleinsteX = intParameter13;
                    if (intParameter15<kleinsteX)
                        kleinsteX = intParameter15;
                    if (intParameter17<kleinsteX)
                        kleinsteX = intParameter17;

                    grootsteY = intParameter14;
                    if (intParameter16>grootsteY)
                        grootsteY = intParameter16;
                    if (intParameter18>grootsteY)
                        grootsteY = intParameter18;

                    kleinsteY = intParameter14;
                    if(intParameter16<kleinsteY)
                        kleinsteY = intParameter16;
                    if(intParameter18<kleinsteY)
                        kleinsteY = intParameter18; 

                    x = kleinsteX + (grootsteX - kleinsteX)/2;
                    y = kleinsteY + (grootsteY - kleinsteY)/2;

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter13 + " , "  + intParameter14, intParameter13 - 25, intParameter14 - 25);
                    g.drawString(intParameter15 + " , " + intParameter16, intParameter15 - 25, intParameter16 - 25);
                    g.drawString(intParameter17 + " , " + intParameter18, intParameter17 - 10, intParameter18 - 25);

                    break;
            case 4: g.fillRoundRect(intParameter13, intParameter14, intParameter5, intParameter16, intParameter17, intParameter18);

                    x = intParameter13 + (intParameter15 / 2);
                    y = intParameter14 + (intParameter16 / 2);

                    g.setColor(Color.darkGray);
                    g.drawString(intParameter13 + " , " + intParameter14, intParameter13 - 5, intParameter14 - 10);
                    g.drawString(intParameter15.toString(), intParameter13 + intParameter15 /2, intParameter14 + intParameter16 + 25);
                    g.drawString(intParameter16.toString(), intParameter13 + intParameter15 + 5, intParameter14 + intParameter16 /2);
                    g.drawString(intParameter17.toString(), intParameter13 + 30, intParameter14 + 15);
                    g.drawString(intParameter18.toString(), intParameter13 + 15, intParameter14 + 30);

                    break;
            }
            g.setColor(Color.darkGray);
            lengte = (strTekst3.length() * 5)/2;
            g.drawString(strTekst3, x - lengte, y);
        }

    }

    public boolean action (Event e, Object o){

        if (e.target == teken){
            draw = true;
            repaint();
            return true;
        }
        if (e.target == vorm){
            repaint();
            return true;
        }
        if(e.target == vergrendel1){
            if (draw2 == false){
            draw2 = true;
            intParameter7 = Integer.parseInt(parameter1.getText());
            intParameter8 = Integer.parseInt(parameter2.getText());
            intParameter9 = Integer.parseInt(parameter3.getText());
            intParameter10 = Integer.parseInt(parameter4.getText());
            intParameter11 = Integer.parseInt(parameter5.getText());
            intParameter12 = Integer.parseInt(parameter6.getText());
            strTekst2 = tekst.getText();
            intVorm = vorm.getSelectedIndex();
            intKleur = kleur.getSelectedIndex();
            vergrendel1.setLabel("Ontgrendel 1");
            vergrendel1.setBackground(Color.red);
            repaint();
            } else {
                draw2 = false;
                vergrendel1.setLabel("Vergrendel 1");
                vergrendel1.setBackground(Color.green);
            }
            return true;
        }
        if(e.target == vergrendel2){
            if(draw3 == false){
                draw3 = true;
                intParameter13 = Integer.parseInt(parameter1.getText());
                intParameter14 = Integer.parseInt(parameter2.getText());
                intParameter15 = Integer.parseInt(parameter3.getText());
                intParameter16 = Integer.parseInt(parameter4.getText());
                intParameter17 = Integer.parseInt(parameter5.getText());
                intParameter18 = Integer.parseInt(parameter6.getText());
                strTekst3 = tekst.getText();
                intVorm2 = vorm.getSelectedIndex();
                intKleur2 = kleur.getSelectedIndex();
                vergrendel2.setLabel("Ontgrendel 2");
                vergrendel2.setBackground(Color.red);
                repaint();
            } else {
                draw3 = false;
                vergrendel2.setLabel("Vergrendel 2");
                vergrendel2.setBackground(Color.green);
                repaint();
            }
            return true;
        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:6)

  1. 让您的课程延伸JFrame而不是JApplet
  2. 有一个构造函数并从那里调用init()

    public Kueze(){
        init();
    }
    
  3. 使用main方法实例化Kueze int

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new Keuze();
            }
        });
    }
    
  4. 在您的init()中,您还需要执行正常的JFrame例程

    public void init(){
       ....
    
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       pack();
       setLocationRelativeTo(null);
       setVisible(true);
    }
    

    请参阅JFrame docs了解这些方法的用途


  5. 侧面说明

    • 您的计划中有很多AWT正在进行中。你应该真正研究Creating a GUI with Swing
    • 不要setSize(...)。我已经向您展示了如何pack()
    • 为什么世界上的paint方法中有这么多?在paint方法中你唯一想要的就是实际进行绘画的东西。
    • 在方法super.paint(g);
    • 正下方拨打public void paint

    更新

    您的代码太长,无法在此处发布,因此我将为您提供布局

    import java.awt.Button;
    import java.awt.Choice;
    import java.awt.Color;
    import java.awt.Event;
    import java.awt.Graphics;
    import java.awt.Label;
    import java.awt.Polygon;
    import java.awt.TextField;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Keuze extends JFrame {
        ....
        public Keuze(){
            init();
        }
    
        public void init(){
            ....
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
        ....
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new Keuze();
                }
            });
        }
    }
    

答案 1 :(得分:2)

如果您希望能够将代码同时用作applet和应用程序,则可以将所有内容包装在JPanel中,然后将该JPanel设置为applet和应用程序的内容窗格。

import java.awt.*;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Keuze extends JApplet {

    public static void main(String[] args) {
        JFrame window = new JFrame("Application Name");
        MyPanel content = new MyPanel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();
        window.setVisible(true);
    }

    public void init() {
        MyPanel content = new MyPanel();
        setContentPane(content);
    }
}

class MyPanel extends JPanel {

    Choice kleur, vorm;
    Label titel, lblKleur, lblVorm, parameters, lblParameter1, lblParameter2, lblParameter3, lblParameter4, lblParameter5, lblParameter6;
    TextField parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, tekst;
    Button teken, vergrendel1, vergrendel2;
    Integer intParameter1, intParameter2, intParameter3, intParameter4, intParameter5, intParameter6, x, y, lengte, grootsteX, grootsteY, kleinsteX, kleinsteY, intParameter7, intParameter8, intParameter9, intParameter10, intParameter11, intParameter12, intVorm, intKleur, intParameter13, intParameter14, intParameter15, intParameter16, intParameter17, intParameter18, intKleur2, intVorm2;
    Boolean draw, draw2, draw3;
    Polygon driehoek, driehoek2, driehoek3;
    String strTekst, strTekst2, strTekst3;

    MyPanel() {

        draw = false;
        draw2 = false;
        draw3 = false;

        /* The rest of your original init() */
    }

    public void paint(Graphics g) {
        super.paint(g);
        setSize(750, 750);
        intParameter1 = Integer.parseInt(parameter1.getText());
        intParameter2 = Integer.parseInt(parameter2.getText());
        intParameter3 = Integer.parseInt(parameter3.getText());
        intParameter4 = Integer.parseInt(parameter4.getText());
        intParameter5 = Integer.parseInt(parameter5.getText());
        intParameter6 = Integer.parseInt(parameter6.getText());

        /* The rest of your original paint(Graphics g) */

    }

    public boolean action(Event e, Object o) {  
        /* The rest of your action method */
    }

}