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.
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.
> 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
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 ?
> 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.
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
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
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
> 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.
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.