Applet code..

R

Robert Mark Bram

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

}
 
K

Kevin Hooke

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,
 
R

Robert Mark Bram

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top