#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Help with Applet


    Hello guys

    Sorry to bother you but i'm stuck for 3 days with this problem. I need to load a class into the initial frame after clicking a start button. The problem is that i never can get it done. I guess that the problem is in the actionListener method.... but i can solve it. This is for a school project, and the teacher doesn't let us use any Swing or any Jcomponent. Can anyone give me a little help?

    The code for the first applet is
    ----------------------------------------------
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Frame extends Applet {

    public Button v;
    public Button f;
    AppletB secondApplet;

    public void init() {

    setSize(400, 550);

    }

    public void start() {

    Font myFont = new Font("Arial", Font.BOLD, 25);
    v = new Button("INICIAR");
    v.setFont(myFont);
    v.setBounds((400 / 2) - 35, (550 / 2) - 10, 100, 30);
    setLayout(null);
    add(v);
    v.addActionListener(new Load());
    f = new Button("FECHAR");
    f.setVisible(true);
    f.setBounds(10, 500, 60, 30);
    add(f);
    }

    class Load implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    secondApplet = (AppletB) getAppletContext().getApplet("AppletB");
    if (secondApplet != null) {
    secondApplet.init();
    secondApplet.start();
    } else {
    System.out.println("Not Running\n");
    System.exit(0);
    }
    }
    }
    }

    ------------------------
    The applet that i want to load in the first applet above is

    import java.applet.Applet;
    import java.awt.Checkbox;
    import java.awt.Choice;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Label;
    import java.awt.TextField;

    public class AppletB extends Applet {

    public void init() {
    setSize(400, 550);
    }

    public void start() {

    setBackground(Color.RED);
    Label i, ii, iii;
    i = new Label("Indique o número de lâmpadas.");
    ii = new Label("Têm todas da mesma potência?");
    iii = new Label("Indique a Potência:");
    i.setBounds(10, 10, 200, 20);
    ii.setBounds(10, 50, 200, 20);
    iii.setBounds(10, 100, 200, 20);
    setLayout(null);
    add(i);
    add(ii);
    add(iii);

    Checkbox w15, w25, w40, w60, w75, w100, w150, wout;
    w15 = new Checkbox("15W");
    w25 = new Checkbox("25W");
    w40 = new Checkbox("40W");
    w60 = new Checkbox("60W");
    w75 = new Checkbox("75W");
    w100 = new Checkbox("100W");
    w150 = new Checkbox("150W");
    wout = new Checkbox("Outra?");
    w15.setBounds(20, 120, 50, 50);
    w25.setBounds(70, 120, 50, 50);
    w40.setBounds(120, 120, 50, 50);
    w60.setBounds(170, 120, 50, 50);
    w75.setBounds(220, 120, 50, 50);
    w100.setBounds(270, 120, 50, 50);
    w150.setBounds(320, 120, 50, 50);
    wout.setBounds(20, 155, 55, 50);
    add(w15);
    add(w25);
    add(w40);
    add(w60);
    add(w75);
    add(w100);
    add(w150);
    add(wout);

    TextField num;
    num = new TextField();

    num.setBounds(250, 10, 100, 20);
    add(num);

    Choice sn;
    sn = new Choice();
    sn.addItem("");
    sn.addItem("Sim");
    sn.addItem("Não");
    sn.setBounds(250, 50, 100, 20);
    add(sn);
    sn.getBackground();

    }

    public void paint(Graphics g) {
    setSize(400, 550);
    g.setColor(Color.RED);
    g.fillRect(0, 0, 400, 550);
    }

    }

    ----------------------------------------
    Thanks for your time
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap the code in code tags to make it readable.

IMN logo majestic logo threadwatch logo seochat tools logo