Entry
Java: How to create an applet where clicking on a button launches an URL?
Sep 7th, 2009 04:49
Knud van Eeden, Joe Bloggs,
----------------------------------------------------------------------
--- Knud van Eeden --- 01 May 2002 - 05:48 pm ------------------------
Java: Applet: URL: How to create an applet where clicking on a button
launches an URL?
---
You use the getAppletContext and showDocument method to open a new
window with that URL
(after you determined which button was clicked).
---
Steps: Overview:
1. -Create the java file
2. -Compile the java file using javac.exe, to a .class applet file
3. -Create the HTML file which launches the applet
4. -Run the HTML file in your browser
---
---
Step: Worked out:
1. -Create the java file
2. -Compile the java file using javac.exe, to a .class applet file
--- cut here ---------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
//
public class ButtonList extends Applet implements ActionListener {
String s = "";
int buttonI = 0;
Button bList[] = new Button[3];
Color colorO[] = new Color[3];
URL urlO[] = new URL[3];
//
public void init() {
Font font = new Font( "Helvetica", Font.BOLD, 24 );
setFont( font );
//
// store buttons
Button url1 = new Button( "URL1" );
Button url2 = new Button( "URL2" );
Button url3 = new Button( "URL3" );
//
// store colors
colorO[0] = Color.red;
colorO[1] = Color.blue;
colorO[2] = Color.yellow;
//
// store URL addresses
//
try {
urlO[0] = new URL( "http://www.altavista.com" );
urlO[1] = new URL( "http://www.google.com" );
urlO[2] = new URL( "http://www.vivisimo.com" );
}
catch ( MalformedURLException e ) {
}
//
// store references to buttons as added
bList[0] = ( Button ) add( url1 );
bList[1] = ( Button ) add( url2 );
bList[2] = ( Button ) add( url3 );
//
// register to receive action events
int I;
for ( I = 1; I <= 3; I++ ) {
buttonI = I - 1;
bList[ buttonI ].addActionListener( this );
}
}
//
public void actionPerformed( ActionEvent ae ) {
int I;
for ( I = 1; I <= 3; I++ ) {
if( ae.getSource() == bList[ I - 1 ] ) {
buttonI = I - 1;
s = "You pressed button" + " " + bList[ I - 1 ].getLabel();
getAppletContext().showDocument( urlO[ I - 1 ] );
}
}
repaint();
}
//
public void paint( Graphics g ) {
g.setColor( colorO[ buttonI ] );
g.drawString( s, 6, 100 );
}
//
}
--- cut here ---------------------------------------------------------
3. -Create the HTML file which launches the applet
4. -Run the HTML file in your browser
--- cut here ---------------------------------------------------------
<HTML>
<TITLE>
ButtonList
</TITLE>
<APPLET
CODE = "ButtonList.class"
WIDTH = "400"
HEIGHT = "300"
>
</APPLET>
</HTML>
--- cut here ---------------------------------------------------------
---
---
Internet: see also:
---
[Internet: see also: http://www.google.com: search for 'showDocument
()': www.io.com/~maus/URLTest/SDTest.html]
[Internet: see also: radder: http://www1.askme.com/MyXpertise.asp?
pm=va&method=qb&cid=831&TR=50&vid=7638377&VA_return=16588413|7637558]
[Internet: see also: http://www.joegrip.com: Java GUI]
---
---
Book: see also:
---
[book: see also: Schildt, Herbert - Java 2 / The complete reference
(3th edition) - ISBN 0-07-211976-4 - p. 723 'Handling buttons']
[book: see also: Schildt, Herbert - Java 2 / The complete reference
(3th edition) - ISBN 0-07-211976-4 - p. 696 'A color demonstration
applet']
[book: see also: Jamsa, Kris - Java Now! - ISBN 1-884133-30-4 - p.
20 'font']
----------------------------------------------------------------------