|
| 1. Introduction | |
| 2. Hello World Example | |
| 3. More complex example using dialog boxes | |
| 4. Example with content pane. |
Java Swing includes a class JApplet contained in the javax.swong package) which replaces the Applet found in previous examples. A trivail example using the JApplet class is presented below in Table 1:
The copde given in Table 1 is the the standrad "Hello World" program as a JApplet.
// Hello World Japplet Example
// Frans Coenen
// 13 June 3003
// University of Liverpool
// Java Core Packages
import java.awt.Graphics;
// Jave extension packages
import javax.swing.JApplet;
public class HelloWorldJApplet extends JApplet {
public void paint(Graphics g) {
// Call super class constuctors
super.paint(g);
// Hello word
g.drawString("Helo Word in a JApplet",25,25);
}
}
|
Table 1: Hellow Word JApplet
The code presented in Table 2 gives a more complex JApplet using input and message dialog boxes.
// ADDITION Japplet Example
// Frans Coenen
// 13 June 3003
// University of Liverpool
// Java Core Packages
import java.awt.Graphics;
// Jave extension packages
import javax.swing.*;
public class AdditionJApplet extends JApplet {
/* ------ FIELDS ------ */
int num1, num2 = 0;
boolean errorFlag=false;
final int START = 1;
final int END = 10;
/* INITILISE JAPPLET */
public void init() {
String stNum1, stNum2;
try{
// Get first number]
while (true) {
stNum1 = JOptionPane.showInputDialog("Enter first integer value between " + START +
" and " + END);
num1 = Integer.parseInt(stNum1);
if (num1>=START && num1<=END) break;
JOptionPane.showMessageDialog(null,"ERROR 1: Input for first integer value " +
" must be a between " + START + " and " + END);
}
// Get second number
while (num2
|
Table 2: More complicated JApplet
Here is the applet:
Code in Table 3 retrieves the applrets content pane and then adds to this.
// TEXT AREA Japplet Example
// Frans Coenen
// 13 June 3003
// University of Liverpool
// Java Core Packages
import java.awt.*;
import java.awt.event.*;
// Jave extension packages
import javax.swing.*;
public class TextAreaJApplet extends JApplet implements ActionListener {
/* ------ FIELDS ------ */
JTextArea textArea1, textArea2;
/* INITILISE JAPPLET */
public void init() {
// Get the applet's GUI component display area
Container container = getContentPane();
container.setBackground(Color.pink);
container.setLayout(new FlowLayout());
// Text area 1
String string = "Some text in here, Some text in here, Some text in here";
textArea1 = new JTextArea(string, 10, 15);
container.add(new JScrollPane(textArea1));
// Copy button
JButton copyButton = new JButton("COPY");
copyButton.addActionListener(this);
container.add(copyButton);
// Text area 2
textArea2 = new JTextArea(10, 15);
textArea2.setEditable(false);
container.add(new JScrollPane(textArea2));
}
public void actionPerformed(ActionEvent event) {
textArea2.setText(textArea1.getSelectedText());
}
}
|
Table 3: JApplet with content pane
Here is the applet:
Created and maintained by Frans Coenen. Last updated 14 June 2003