Applet conversion assistance

Need to convert source file to applet not sure what is wrong

Page 1 of 1

1 Replies - 2851 Views - Last Post: 29 May 2008 - 02:08 PM Rate Topic: -----

#1 MBAStudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-May 08

Applet conversion assistance

Post icon  Posted 29 May 2008 - 01:40 PM

Greeting Java surfers -

I'm a business student - taking intro to java as an elective. Working on assignment that requires me to convert a source code into an Applet. I read the chapters and I think I understand what "not" to do in an applet - but because this is my first programming experience I'm not really sure what I'm doing wrong. Below is my original code that I am attempting to convert. I tried to compile it so I can then create my .html file - but getting lots of errors. It worked as a normal source code - before I started making changes. Your assistance is greatly appreciate. I want to understand what I'm doing - so if you could - please provide your inputs and responses in the most basic terms so I can get this - otherwise it really isn't going to help me down the road. THANKS!!!!

CODE:


import java.io.*;
import java.swing.*;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.swing.event.*;

public class WhiteBoard extend JApplet implements ActionListener
{
// Establishes Width, Heigh, Row and Column settings
private static int WIDTH = 550;
private static int HEIGHT = 350;

private int row = 10;
private int col = 20;

private JLabel headingL;
private JTextField lineTF;
private JTextArea whiteboardTA;
private JButton exitB, appendB;

public void init()
{
setTitle("White Board"); // should I take this out since applets do
not use setTitle according to book?
Container pane = getContentPane();
setSize(WIDTH, HEIGHT); // should I take this out?

headingL = new JLabel("Welcome to White Board");
lineTF = new JTextField(20);

whiteBoardTA = new JTextArea(row, col);
exitB = new JButton("Exit");
exitB.addActionListener(this);

appendB = new JButton("Append");
appendB.addActionListener(this);

pane.setLayout(null);

headingL.setLocation(50, 20);
lineTF.setLocation(20, 100);
whiteBoardTA.setLocaton(320, 50);
appendB.setLocation(230, 100);
exitB.setLocation(230, 250);

headingL.setSize(200, 30);
lineTF.setSize(200, 30);
whiteBoardTA.setSize(200, 200);
appendB.setSize(80, 30);
exitB.setSize(80, 30);

pane.add(headingL);
pane.add(lineTF);
pane.add(whiteBoardTA);
pane.add(appendB);
pane.add(exitB);

setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

Public static void main (String[] args)
{
WhiteBoard board = new WhiteBoard();
}

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Append"))
whiteBoardTA.append(lineTF.getText());
else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Applet conversion assistance

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Applet conversion assistance

Posted 29 May 2008 - 02:08 PM

Beside the confusion between whiteBoardTA and whiteboardTA

OK with switch from Applet to JFrame so the WhiteBoard class should extends JFrame instead of JApplet

OK an Application needs a contructor

public WhiteBoard() {
}

that basically do what is in the init() method of the Applet.

So lets make the constructor and have this constructor to call the init() method

public WhiteBoard() {
	init();
}



Now in your init() method nothing really changes

and here is your new code:

import java.io.*;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;

public class WhiteBoard extends JFrame implements ActionListener {

//	Establishes Width, Heigh, Row and Column settings 
	private static int WIDTH = 550;
	private static int HEIGHT = 350;

	private int row = 10;
	private int col = 20;

	private JLabel headingL;
	private JTextField lineTF;
	private JTextArea whiteboardTA;
	private JButton exitB, appendB;

	WhiteBoard() {
		super();
		init();
	}
	public void init()
	{
		setTitle("White Board"); // should I take this out since applets do 
		setSize(WIDTH, HEIGHT); // should I take this out?

		headingL = new JLabel("Welcome to White Board");
		lineTF = new JTextField(20);

		whiteboardTA = new JTextArea(row, col);
		exitB = new JButton("Exit");
		exitB.addActionListener(this);

		appendB = new JButton("Append");
		appendB.addActionListener(this);

		this.setLayout(null);

		headingL.setLocation(50, 20);
		lineTF.setLocation(20, 100);
		whiteboardTA.setLocation(320, 50);
		appendB.setLocation(230, 100);
		exitB.setLocation(230, 250);

		headingL.setSize(200, 30);
		lineTF.setSize(200, 30);
		whiteboardTA.setSize(200, 200);
		appendB.setSize(80, 30);
		exitB.setSize(80, 30);

		Container pane = getContentPane();
		pane.add(headingL);
		pane.add(lineTF);
		pane.add(whiteboardTA);
		pane.add(appendB);
		pane.add(exitB);

		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	public static void main (String[] args)
	{
		WhiteBoard board = new WhiteBoard();
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Append"))
			whiteboardTA.append(lineTF.getText());
		else if (e.getActionCommand().equals("Exit"))
			System.exit(0);
	}
} 



Hope this help
In the future please post your code like this :code:

This post has been edited by pbl: 29 May 2008 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1