Applet code..

Discussion in 'Java' started by Robert Mark Bram, Aug 28, 2003.

  1. Howdy all!

    Can anyone tell me what is wrong with this code?

    When I view it in the applet viewer, I can only see the button when I click
    on it. When I view the web page in a browser, all I see is the grey applet
    box..

    Any help would be most appreciated!

    Rob
    :)


    import java.applet.Applet;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.event.*;


    public class HelloWorld extends Applet implements ActionListener
    {
    private String myString;
    String username, password;
    private TextField usernameField=null;
    private JPasswordField passwordField=null;
    private TextField responseField=null;

    public void init()
    {

    username=getParameter("user");
    password=getParameter("passwd");

    JPanel controlPanel=new JPanel();
    controlPanel.setLayout(new GridLayout(3,2));


    JLabel usernameLabel=new JLabel("User Name:");
    usernameLabel.setFont(new Font("Courier", Font.BOLD,23));
    controlPanel.add(usernameLabel);

    usernameField=new TextField(20);
    usernameField.setText("Enter user name here");
    controlPanel.add(usernameField);

    JLabel passwordLabel=new JLabel("Password:");
    passwordLabel.setFont(new Font("Courier", Font.BOLD,23));
    controlPanel.add(passwordLabel);

    passwordField= new JPasswordField();
    passwordField.setText("Password");
    controlPanel.add(passwordField);

    JButton validateInput=new JButton();
    validateInput.setText("Press me");
    validateInput.addActionListener(this);
    controlPanel.add(validateInput);

    responseField=new TextField();
    responseField.setText("result will here");
    responseField.setEditable(false);
    controlPanel.add(responseField);

    add(controlPanel);

    myString="that's cool man!";
    }

    public void actionPerformed(ActionEvent event)
    { String usernameString = usernameField.getText();
    String passwordString = passwordField.getText();
    if(usernameString.equals(username)&& passwordString.equals(password))
    {
    responseField.setText("Authorisation succeedlafjdl");
    }
    else
    {
    responseField.setText("AUthorisation failed .");
    }
    }



    public String getAppletInfo()
    {
    return "Man Kwong Chan";
    }
    public void paint (Graphics g)
    {
    paint();
    // g.drawString(getAppletInfo(), 100,70);
    // g.drawString(username,100,90);
    // g.drawString(password,100,100);
    //
    }

    }
     
    Robert Mark Bram, Aug 28, 2003
    #1
    1. Advertising

  2. Robert Mark Bram

    Kevin Hooke Guest

    Remove the paint() method - you don't need it in this case. After that,
    works fine for me.
    Tested it in IE6 on XP with the Sun Java Plugin 1.4.2.

    Other than that, a general comment: I think it is considered bad practice to
    mix Swing Components (those prefixed with 'J') with AWT - you have a blend
    of both. Either use one or the other. I think you may run into problems if
    you have 'heavy' AWT components and 'light' Swing components on the same
    Container - the Heavyweight components (using platform native code) will
    always be displayed ontop of or overlapping the light (no native code /
    platform independent) Swing components.

    Hope that helps,

    --
    Kevin Hooke

    MindBeans Software Consulting
    http://www.mindbeans.net

    "Robert Mark Bram" <> wrote in message
    news:3f4e076a$0$4191$...
    > Howdy all!
    >
    > Can anyone tell me what is wrong with this code?
    >
    > When I view it in the applet viewer, I can only see the button when I

    click
    > on it. When I view the web page in a browser, all I see is the grey applet
    > box..
    >
    > Any help would be most appreciated!
    >
    > Rob
    > :)
    >
    >
    > import java.applet.Applet;
    > import java.awt.Font;
    > import java.awt.Graphics;
    > import java.awt.*;
    > import javax.swing.*;
    > import java.net.*;
    > import java.awt.event.*;
    >
    >
    > public class HelloWorld extends Applet implements ActionListener
    > {
    > private String myString;
    > String username, password;
    > private TextField usernameField=null;
    > private JPasswordField passwordField=null;
    > private TextField responseField=null;
    >
    > public void init()
    > {
    >
    > username=getParameter("user");
    > password=getParameter("passwd");
    >
    > JPanel controlPanel=new JPanel();
    > controlPanel.setLayout(new GridLayout(3,2));
    >
    >
    > JLabel usernameLabel=new JLabel("User Name:");
    > usernameLabel.setFont(new Font("Courier", Font.BOLD,23));
    > controlPanel.add(usernameLabel);
    >
    > usernameField=new TextField(20);
    > usernameField.setText("Enter user name here");
    > controlPanel.add(usernameField);
    >
    > JLabel passwordLabel=new JLabel("Password:");
    > passwordLabel.setFont(new Font("Courier", Font.BOLD,23));
    > controlPanel.add(passwordLabel);
    >
    > passwordField= new JPasswordField();
    > passwordField.setText("Password");
    > controlPanel.add(passwordField);
    >
    > JButton validateInput=new JButton();
    > validateInput.setText("Press me");
    > validateInput.addActionListener(this);
    > controlPanel.add(validateInput);
    >
    > responseField=new TextField();
    > responseField.setText("result will here");
    > responseField.setEditable(false);
    > controlPanel.add(responseField);
    >
    > add(controlPanel);
    >
    > myString="that's cool man!";
    > }
    >
    > public void actionPerformed(ActionEvent event)
    > { String usernameString = usernameField.getText();
    > String passwordString = passwordField.getText();
    > if(usernameString.equals(username)&& passwordString.equals(password))
    > {
    > responseField.setText("Authorisation succeedlafjdl");
    > }
    > else
    > {
    > responseField.setText("AUthorisation failed .");
    > }
    > }
    >
    >
    >
    > public String getAppletInfo()
    > {
    > return "Man Kwong Chan";
    > }
    > public void paint (Graphics g)
    > {
    > paint();
    > // g.drawString(getAppletInfo(), 100,70);
    > // g.drawString(username,100,90);
    > // g.drawString(password,100,100);
    > //
    > }
    >
    > }
    >
    >
    >
     
    Kevin Hooke, Aug 28, 2003
    #2
    1. Advertising

  3. Howdy Kevin!

    Thanks for the reply!

    > Remove the paint() method - you don't need it in this case. After that,
    > works fine for me.


    Does this mean that paint will always paint over gui components?

    Rob
    :)
     
    Robert Mark Bram, Aug 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    642
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    877
    webster
    Jul 20, 2003
  3. Andrew Thompson
    Replies:
    0
    Views:
    430
    Andrew Thompson
    Aug 5, 2003
  4. Blah Blah
    Replies:
    0
    Views:
    1,446
    Blah Blah
    Aug 13, 2003
  5. Rune Andresen
    Replies:
    2
    Views:
    1,962
    Phil Powell
    Sep 17, 2003
Loading...

Share This Page