Difference between Applet and Frame?

hello I want ask,what is the difference between Applet and frame and if i can change Applet to Frame or no ?Thanks a lot
[141 byte] By [Alenaa] at [2007-11-27 6:41:20]
# 1

Applets are embedded into an existing window environment (the browser), whereas Frames are independent program windows. Applet is derived from Panel, so theoretically (ignoring the rest of the Applet infrastructure) it could be used within a Frame. If you don't use any Applet-specific method, it's easy to turn an Applet into a Frame. It'd be easier though if you concentrated on a window-independent UI which can be put into either an Applet or a Frame.

quittea at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 2

Some differences are (summary from Lervik and Havdal: Java the UML Way, 2002, page 446):

An applet must be a public class, frame doesn't have to.

In an applet, construction happens in the init() method; in the frame, it's in the constructor.

The browser starts the applet. You start the frame from main() or another method.

Yes, given the source code of an applet, you can make the necessary changes to change it into a frame.

OleVVa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 3
thank you for reply.one more question can i change Applet to Frame >?if yes what are the possibilities ?
Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 4
> one more question can i change Applet to Frame >?I think we already gave two different answers to that question. If not, you'll have to state more clearly what it is you want to know.
OleVVa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 5

> one more question can i change Applet to Frame >?

If you do this, you have to take care of

- setting the Frame's size (fixed for Applet)

- making the Frame visible (automatic for Applet)

- providing a main method for starting the application (page load for Applet)

- providing a clean shutdown of your application

- not using any Applet methods or fields

quittea at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 6
thank you for replypublic class Calc extends JApplet implements ActionListener:yhis is given when i want change and i wrote public class Calc extends frame implements ActionListener:this does not work why ?
Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 7
> this does not work why ?I could ask you the same. What errors are given?
quittea at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 8
this is for : quitteif i show yoy the program and i need to change it to frame .can you show me what i need to do .and what are the step to follow .please.
Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 9
Have you remembered to rewrite the init() method to a constructor?have you remembered to set the frame visible?
OleVVa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 10
i receive Calc it is not abstract and doesw not override abstract method action performed(java.awt.event.Actionevent)ina java.awt .actionEvenet.this is what is written .
Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 11
Olew.yes i have this two things.and it is correct.if i show you the program and take a look on it.it can be easier to expalin me what to do please.?
Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 12

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.lang.Math.*;

import java.math.BigInteger;

public class BigIntCalc extends JApplet implements ActionListener{

JLabel lN1, lN2;

JTextField tN1, tN2.

JButton btnAdd,btnSub,btnMul,btnDiv,btnClear; JTextArea outArea;

public void init(){

Box box=Box.createVerticalBox();

lN1=new JLabel("Write first number"); box.add(lN1);

tN1=new JTextField(100); tN1.setEditable(true); box.add(tN1);

lN2=new JLabel("write second number"); box.add(lN2);

tN2=new JTextField(100); tN2.setEditable(true); box.add(tN2);

btnAdd=new JButton("+"); btnAdd.addActionListener(this); box.add(btnAdd);

btnSub=new JButton("-"); btnSub.addActionListener(this); box.add(btnSub);

btnMul=new JButton("*"); btnMul.addActionListener(this); box.add(btnMul);

btnDiv=new JButton("/"); btnDiv.addActionListener(this); box.add(btnDiv);

btnClear=new JButton("Clear"); btnClear.addActionListener(this); box.add(btnClear);

String desc="This is a simple integer calculator\n";

outArea=new JTextArea(desc,30,60);

ScrollPane scrollPane=new ScrollPane();//.

scrollPane.add(outArea);

box.add(scrollPane);

Container c=getContentPane();

c.add(box);

setSize(600,300);

setVisible(true);

showStatus("Enter operands and operator");

}

public void actionPerformed(ActionEvent actionEvent){

if(actionEvent.getSource()==btnClear){

outArea.selectAll(); outArea.cut();

} else {

String sN1=tN1.getText(), sN2=tN2.getText(), sOp="";

if(sN1.equals("")){sN1="0"; showStatus("Enter first operation");} else

if(sN2.equals("")){sN2="0"; showStatus("Enter second operation");} else {

showStatus("Select operator or Clear");}

BigInteger bN1=new BigInteger(sN1);

BigInteger bN2=new BigInteger(sN2);

BigInteger bRes=BigInteger.ZERO, bResDiv[]={BigInteger.ZERO,BigInteger.ZERO};

BigInteger bRemainder=BigInteger.ZERO;

if(actionEvent.getSource()==btnAdd){bRes=bN1.add(bN2); sOp=" + ";}

if(actionEvent.getSource()==btnSub){bRes=bN1.subtract(bN2); sOp=" - ";}

if(actionEvent.getSource()==btnMul){bRes=bN1.multiply(bN2); sOp=" * ";}

if(actionEvent.getSource()==btnDiv){sOp=" / ";

if(bN2.signum()!=0)bResDiv=bN1.divideAndRemainder(bN2);

bRes=bResDiv[0]; bRemainder=bResDiv[1];

}

if(sOp.equals(" / ") && bN2.signum()==0){

showStatus("Cannot divide by zero");

} else {

outArea.append(bN1.toString() + sOp+bN2.toString() + " = " + bRes.toString() + "\n");

if(actionEvent.getSource()==btnDiv)outArea.append("Remainder = "+bRemainder.toString()+"\n");

showStatus("Calculations completed");

}

}

}

}

this is the main program ;

which steps i need to follow to make the change ?

Alenaa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 13

> this is for : quitte

> if i show yoy the program and i need to change it to

> frame .can you show me what i need to do .and what

> are the step to follow .

> please.

The above wasn't for me, but anyway: If you still cannot get it to work, it's probably better to show us your attempt to change it, and someone may be able to point out what you've done wrong. First of all, tell us the exact behaviour, including all error messages etc.

OleVVa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 14
And please use the code formatting button above the message field when posting code. I should have said that before.
OleVVa at 2007-7-12 18:10:54 > top of Java-index,Java Essentials,Java Programming...
# 15
thank you Olew.this is the main program and it work correctly i just ask if i can change it to Frame .and what are the step to follow.like which codes i need to chage.and so on.
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 16

Hmm, are you sure this was working?

Here's what I found:

- There's a typo in the line "JLabel lN1, lN2", it should end with a ";" instead of a dot

- Don't use heavy-weight components such as ScrollPane in a Swing UI; there's a Swing component called JScrollPane

- Method showStatus() does not exist

- Change "extends JApplet" to "extends JFrame"

- Change method init() to be the constructor

- Add a main method creating an instance of BigIntCalc

quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 17
ok quitte.i ll try this thank you so much .and if it does not work i will ask also for your help.thank you so much
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 18
what to use in place of showStatus() method?
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 19
Just create one of your own, maybe put the message into a JLabel somewhere at the bottom of the frame.
quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 20

thx a lot...you are so kind....

one moe question .please.

how can i change the size of the button.if you run the program i receive the button are with different size .how can i make all with the same size.

thank you so much you really help me wnd now it works...

big thanks

Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 21
Call setPreferredSize() on each button giving the size you like.
quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 22
this.setBackground(Color.blue);i want change the color of the frame but it does not work.do u know why ?
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 23
Set it on the content pane (= Container c in your case), not on the frame.
quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 24
Container c=getContentPane();c.add(box);this what i have when i change the new frame does not work correctly and nothing inside it.i need to write:setBackground(color.blue)in place of what ?container c or getContenetPane().?
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 25
Don't replace these, just callc.setBackground(Color.BLUE);
quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 26

Thank you so much.

i just want from you tow things?

1-how to change the button because i do not have the same button.i tryed what u told me but it does not work ?how to do it.

2-and the second thing is that i delete this method showstatus().and when i divide by zero i do not receive any thing.what i need to write in the place of it.

thank you

Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 27

> 1-how to change the button because i do not have the

> same button.i tryed what u told me but it does not

> work ?how to do it.

What I described would mean that for each single button instance you use, you'd have to call setPreferredSize(). However, I think a better way is to group these buttons into a container which adjusts its components' sizes equally. For instance you could create a new JPanel, set its layout manager to GridLayout, add all buttons to this new panel, and finally add this panel to the content pane.

> 2-and the second thing is that i delete this method

> showstatus().and when i divide by zero i do not

> receive any thing.what i need to write in the place of it.

The easiest way would be to add another JLabel to your user interface and implement a new showStatus() method like this:

private void showStatus(String status) {

yourNewLabel.setText(status);

}

Initialize the text of the label to a blank " " so it doesn't get shrunk to a size of 0/0 on program start.

quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 28
Store the record of the session in a text file. how can i store each calculation which i do when i use this calculator.what i need to create ?
Alenaa at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...
# 29

You need to open a stream to a file and write the log to this stream. Here's a tutorial on Input/Output:

http://java.sun.com/docs/books/tutorial/essential/io/

Instead of writing each calculation directly to the file, I'd suggest to log to a list and write the contents of this list to the file when the application is closed.

See

http://java.sun.com/docs/books/tutorial/collections/index.html

for information on how to use lists and other collections classes and

http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html

for information on how to get notified when a frame is closed.

quittea at 2007-7-21 22:02:30 > top of Java-index,Java Essentials,Java Programming...