 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tmilewski@gmail.com Guest
|
Posted: Wed Nov 23, 2005 12:57 am Post subject: Application to Applet |
|
|
Can someone please help me... I've been trying, and searching, for a
way to convert my Application to an Applet. I cant figure it out, it
just never wants to work.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
private JTextField txtIO;
private double result = 0.0;
private double reset = 0.0;
public static final String[] numbers = { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "0" };
public static final String[] functions = { "+", "-", "*", "/", "CE"
};
/////////////////////////////////////////////////////
// Method: main
// Calls GUI
/////////////////////////////////////////////////////
public static void main(String []args) {
GUI calculator = new GUI();
}
/////////////////////////////////////////////////////
// Method: GUI
// Constructs the Graphic User Interface
/////////////////////////////////////////////////////
public GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame cp = new JFrame("GUI Calculator by Tom Milewski");
//Set Layout ------------------------------------
JPanel bpMain = new JPanel(new GridLayout(4, 3));
JPanel bpFunc = new JPanel(new GridLayout(3, 2)); //row, col
JPanel tpScreen = new JPanel();
//bpMain Buttons --------------------------------
JButton btnMain;
for (int i = 0; i < numbers.length; i++) {
btnMain = new JButton(numbers[i]);
btnMain.addActionListener(this);
bpMain.add(btnMain);
}
//bpFunc Buttons --------------------------------
JButton btnFunc;
for (int i = 0; i < functions.length; i++) {
btnFunc = new JButton(functions[i]);
btnFunc.addActionListener(this);
bpFunc.add(btnFunc);
}
//txtIO Field -----------------------------------
txtIO = new JTextField("Press CE To Continue", 23);
tpScreen.add(txtIO);
//Add Content -----------------------------------
cp.getContentPane().add(tpScreen, BorderLayout.NORTH);
cp.getContentPane().add(bpMain, BorderLayout.CENTER);
cp.getContentPane().add(bpFunc, BorderLayout.WEST);
cp.pack();
cp.setVisible(true);
}
/////////////////////////////////////////////////////
// Method: actionPerformed
// Trys/Catches NumberFormatException Errors,
// Calls tryingCorrectNumberFormats
/////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e) {
try {
tryingCorrectNumberFormats(e);
} catch(NumberFormatException e2) {
txtIO.setText("Error: Press CE To Continue");
}
}
/////////////////////////////////////////////////////
// Method: tryingCorrectNumberFormats
// Executes the appropriate function
/////////////////////////////////////////////////////
public void tryingCorrectNumberFormats(ActionEvent e) {
if(e.getActionCommand().equals("+")) { // ADDITION
double input = stringToDouble( txtIO.getText() );
result = result+input;
txtIO.setText(Double.toString(result));
} else if(e.getActionCommand().equals("-")) { // SUBTRACTION
double input = stringToDouble( txtIO.getText() );
result = result-input;
txtIO.setText(Double.toString(result));
} else if(e.getActionCommand().equals("*")) { // MULTIPLICATION
double input = stringToDouble( txtIO.getText() );
result = result*input;
txtIO.setText(Double.toString(result));
} else if(e.getActionCommand().equals("/")) { // DIVISION
double input = stringToDouble( txtIO.getText() );
if(input != 0) {
result = result/input;
txtIO.setText(Double.toString(result));
} else {
txtIO.setText("Divide By Zero Error: Press CE To
Continue");
}
} else if(e.getActionCommand().equals("CE")) { // CE
txtIO.setText(Double.toString(0.0));
} else { // Numbers - Error
if(stringToDouble(e.getActionCommand()) >= 0 &&
stringToDouble(e.getActionCommand()) <= 9) {
txtIO.setText(e.getActionCommand());
} else {
txtIO.setText("Error: Press CE To Continue");
}
}
}
/////////////////////////////////////////////////////
// Method: tryingCorrectNumberFormats
// Executes the appropriate function
/////////////////////////////////////////////////////
private static double stringToDouble(String stringObject) {
return Double.parseDouble(stringObject.trim());
}
}
|
|
| Back to top |
|
 |
tmilewski@gmail.com Guest
|
Posted: Wed Nov 23, 2005 12:59 am Post subject: Re: Application to Applet |
|
|
--- also, I do understand that this is not an applet ... i posted the
original application.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Wed Nov 23, 2005 1:06 am Post subject: Re: Application to Applet |
|
|
On 22 Nov 2005 16:57:06 -0800, [email]tmilewski (AT) gmail (DOT) com[/email] wrote, quoted or
indirectly quoted someone who said :
| Quote: | Can someone please help me... I've been trying, and searching, for a
way to convert my Application to an Applet.
|
You can't do it by simply adding something. You can convert an Applet
to a hybrid Applet/applications though.
You need to rethink your logic to fit into the init/start/stop/destroy
pattern.
See http://mindprod.com/jgloss/applet.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
Fabien Bergeret Guest
|
Posted: Wed Nov 23, 2005 10:48 am Post subject: Re: Application to Applet |
|
|
Your GUI class should not extend JFrame but JPanel.
Then you would have your application class extending JFrame and adding
your GUI in it, and you applet class doing the same.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Wed Nov 23, 2005 4:42 pm Post subject: Re: Application to Applet |
|
|
On Wed, 23 Nov 2005 11:48:55 +0100, Fabien Bergeret
<fabien.bergeret (AT) asupprimer (DOT) laposte.net> wrote, quoted or indirectly
quoted someone who said :
| Quote: | Your GUI class should not extend JFrame but JPanel.
Then you would have your application class extending JFrame and adding
your GUI in it, and you applet class doing the same.
it should extend JApplet if you in the end want to run it as a |
hybrid. For standalone use you instantiate a JFrame and add the
JApplet to it. for applet use, you use the JApplet directly.
See http://mindprod.com/jgloss/japplet.html
for the code, or http://mindprod.com/jgloss/applet.html
for the corresponding code for Applets.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
sanjay manohar Guest
|
Posted: Thu Nov 24, 2005 4:46 pm Post subject: Re: Application to Applet |
|
|
Really? So I should add the JApplet to the JFrame for a standalone...
So who calls the JApplet start(), init() etc.? or should I place only
the applet-specific stuff in those methods?
|
|
| Back to top |
|
 |
Thomas Fritsch Guest
|
Posted: Thu Nov 24, 2005 5:27 pm Post subject: Re: Application to Applet |
|
|
sanjay manohar wrote:
| Quote: | Really? So I should add the JApplet to the JFrame for a standalone...
So who calls the JApplet start(), init() etc.?
Good point! You'll have to applet.init() and applet.start() by yourself, |
probably from your main function.
Even worse: You have to call applet.setStub(...) even earlier. Otherwise
many Applet methods (getParameter, getCodeBase, getDocumentBase,
showDocument, showStatus, ...) will throw plenty of NullPointerException's.
| Quote: | or should I place only
the applet-specific stuff in those methods?
You should do so anyway.
|
See "Turning an Applet into a Standalone Application"
<http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/>
--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Thu Nov 24, 2005 5:29 pm Post subject: Re: Application to Applet |
|
|
On 24 Nov 2005 08:46:01 -0800, "sanjay manohar"
<sgmanohar (AT) hotmail (DOT) com> wrote, quoted or indirectly quoted someone who
said :
| Quote: | Really? So I should add the JApplet to the JFrame for a standalone...
So who calls the JApplet start(), init() etc.? or should I place only
the applet-specific stuff in those methods?
|
For the third time, please see the code at
http://mindprod.com/jgloss/japplet.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|