Applet to JFrame GUI
Hello,
I am working on making an applet into a JFrame GUI and getting three
compile issues.
C:\j2sdk\bin\orderForm.java:189: 'class' or 'interface' expected
public static void main( String[] args )
^
C:\j2sdk\bin\orderForm.java:196: 'class' or 'interface' expected
} // end class orderForm
^
C:\j2sdk\bin\orderForm.java:196: 'class' or 'interface' expected
} // end class orderForm
^
3 errors
Tool completed with exit code 1
The code I am using is below. I am using the newest version of the Java
SDK and TextPad to compile. I have also included the Applet code to
show my transition from old to new. Can anybody help me get this JFrame
code running?
// JFrame code
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
public class help extends JFrame
{
//JLabel for name of calulator
private JLabel calcNameJLabel;
// JLabel and JTextField for Principal
private JLabel balFieldJLabel;
private JTextField balFieldJTextField;
// JLabel and JTextField for name
private JLabel intFieldJLabel;
private JTextField intFieldJTextField;
// JLabel and JTextField for addressline1
private JLabel nyrFieldJLabel;
private JTextField nyrFieldJTextField;
// JLabel and JTextArea for Amortization Results
private JLabel msgAreaJLabel;
private JTextField msgareaJTextArea;
// JButton to initiate calculation
private JButton CalcTextJButton;
// no-argument constructor
public help()
{
createUserInterface();
}
// create and position GUI components
public void createUserInterface()
{
// get content pane and set its layout
Container container = getContentPane();
container.setLayout( null );
// set up businessNameJLabel
calcNameJLabel = new JLabel();
calcNameJLabel.setText( "Calculator" );
calcNameJLabel.setLocation( 35, 20 );
calcNameJLabel.setSize( 550, 88 );
calcNameJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 )
);
calcNameJLabel.setHorizontalAlignment( JLabel.CENTER );
container.add( calcNameJLabel );
// set up balFieldJLabel
balFieldJLabel = new JLabel();
balFieldJLabel.setBounds( 26, 170, 134, 21 );
balFieldJLabel.setText( "Principal" );
container.add( balFieldJLabel );
// set up balFieldJTextField
balFieldJTextField = new JTextField();
balFieldJTextField.setBounds( 120, 170, 40, 21 );
balFieldJTextField.setText( "" );
container.add( balFieldJTextField );
// set up intFieldJLabel
intFieldJLabel = new JLabel();
intFieldJLabel.setBounds( 26, 200, 130, 21 );
intFieldJLabel.setText( "Name:" );
container.add( intFieldJLabel );
// set up intFieldJTextField
intFieldJTextField = new JTextField();
intFieldJTextField.setBounds( 120, 200, 220, 21 );
intFieldJTextField.setText( "" );
container.add( intFieldJTextField );
// set up nyrFieldJLabel
nyrFieldJLabel = new JLabel();
nyrFieldJLabel.setBounds( 26, 230, 120, 21 );
nyrFieldJLabel.setText( "Address Line 1:" );
container.add( nyrFieldJLabel );
// set up nyrFieldJTextField
nyrFieldJTextField = new JTextField();
nyrFieldJTextField.setBounds( 120, 230, 220, 21 );
nyrFieldJTextField.setText( "" );
container.add( nyrFieldJTextField );
// set up msgAreaJTextArea
msgAreaJTextArea = new JTextArea();
msgAreaJTextArea.setBounds( 360, 510, 60, 21 );
msgAreaJTextArea.setText( "$0.00" );
msgAreaJTextArea.setEditable( false );
msgAreaJTextArea.setHorizontalAlignment( JTextField.CENTER );
container.add( msgAreaJTextArea );
// set up CalcTextJButton
CalcTextJButton = new JButton();
CalcTextJButton.setBounds( 335, 550, 90, 24 );
CalcTextJButton.setText( "Calculate" );
container.add( CalcTextJButton );
CalcTextJButton.addActionListener(
new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
CalcTextJButtonActionPerformed (event);
}
}
);
// set properties of application's window
setTitle( "Calculator" ); // set title bar text
setSize( 650, 650 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// calculate total of order
private void CalcTextJButtonActionPerformed ( ActionEvent event )
{
//Declare
String balString = balFieldJTextField.getText();
String intString = intFieldJTextField.getText();
String nyrString = nyrFieldJTextField.getText();
if (balString.trim().length() == 0)
msgArea.setText("Principal amount missing");
else if (intString.trim().length() == 0)
msgArea.setText("Interest rate missing");
else if (nyrString.trim().length() == 0)
msgArea.setText("Number of years missing");
else // otherwise, do calculations
{
double bal = new Double(balString).doubleValue();
double intyr = new Double(intString).doubleValue() / 100.;
short nyears = (short)new Integer(nyrString).intValue();
StringBuffer msg = new StringBuffer();
msg.append("\nPrincipal = " + bal + "\n\nInterest =" +
intyr
+ " \n\nYears = " + nyears + "\n\n");
double intmo = intyr / 12.;
int npmts = nyears * 12;
double pmt = bal * (intmo / (1. - Math.pow(1. + intmo,
-npmts)));
msg.append("Number \t Payment \t Interest \t\t Principal
\t Current \n");
msg.append("of Payments \t Amount\t\t Paid \t\t Paid \t\t
Balance\n");
msg.append("*************************************************************************************************\n");
for (short mo = 1; mo <= npmts; ++mo)
{
double prinpmt, intpmt = bal * intmo;
if (mo < npmts)
prinpmt = pmt - intpmt;
else
prinpmt = bal;
bal -= prinpmt;
msg.append(mo + ".\t\t" + format(intpmt + prinpmt) +
"\t\t"
+ format(intpmt) + "\t\t" + format(prinpmt) +
"\t\t"
+ format(bal) + "\n\n\n");
}
msgArea.setText(msg.toString());
}
}
//
// Convert double to dollars and cents
private String format(double dollars)
{
String numString = Double.toString(dollars);
int dotpos = numString.indexOf('.');
if (dotpos < 0) // Check if whole number
return numString;
// Check for excess fraction digits
else if (dotpos < numString.length() - 2)
return numString.substring(0, dotpos + 3); // `.'+ 2 digits
else
return numString + "0"; // Assume only 1 fraction digit
}
}
// main method
public static void main( String[] args )
{
help application = new help();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class
// Beginning of old Applet code
//
//
// Here is the Applet code that I am attempting to emulate
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
public class Testing extends Applet implements ActionListener
{
// creates the text fields
private TextField balField = new TextField("",15);
private TextField intField = new TextField("",5);
private TextField nyrField = new TextField("",5);
// creates the button action strings
private final String CalcText = "Compute";
private final String ResetText = "Reset";
private final String CloseText = "Close";
// creates the buttons for the action triggers
private Button Calc = new Button(CalcText);
private Button Reset = new Button(ResetText);
private Button Close = new Button(CloseText);
private TextArea msgArea = new TextArea("",25,70);
public void init() // initialize the applet
{
Calc.addActionListener(this);
Reset.addActionListener(this);
Close.addActionListener(this);
msgArea.setEditable(false);
this.add(new Label("Enter principal"));
this.add(balField);
this.add(new Label("Enter annual interest rate"));
this.add(intField);
this.add(new Label("Enter number of years"));
this.add(nyrField);
this.add(Calc);
this.add(Reset);
this.add(Close);
this.add(msgArea);
}
public void actionPerformed(ActionEvent actionevent)
{
if (CalcText.equals(actionevent.getActionCommand()))
{
update();
}
else if (ResetText.equals(actionevent.getActionCommand()))
{
balField.setText("");
intField.setText("");
nyrField.setText("");
msgArea.setText("");
}
else if (CloseText.equals(actionevent.getActionCommand()))
{
this.stop();
}
}
private void update()
{
String balString = balField.getText();
String intString = intField.getText();
String nyrString = nyrField.getText();
if (balString.trim().length() == 0)
msgArea.setText("Principal amount missing");
else if (intString.trim().length() == 0)
msgArea.setText("Interest rate missing");
else if (nyrString.trim().length() == 0)
msgArea.setText("Number of years missing");
else
{
double bal = new Double(balString).doubleValue();
double intyr = new Double(intString).doubleValue() / 100.;
short nyears = (short)new Integer(nyrString).intValue();
StringBuffer msg = new StringBuffer();
msg.append("\nPrincipal = " + bal + "\n\nInterest =" +
intyr
+ " \n\nYears = " + nyears + "\n\n");
double intmo = intyr / 12.;
int npmts = nyears * 12;
double pmt = bal * (intmo / (1. - Math.pow(1. + intmo,
-npmts)));
msg.append("Number \t Payment \t Interest \t\t Principal
\t Current \n");
msg.append("of Payments \t Amount\t\t Paid \t\t Paid \t\t
Balance\n");
msg.append("*************************************************************************************************\n");
for (short mo = 1; mo <= npmts; ++mo)
{
double prinpmt, intpmt = bal * intmo;
if (mo < npmts)
prinpmt = pmt - intpmt;
else
prinpmt = bal;
bal -= prinpmt;
msg.append(mo + ".\t\t" + format(intpmt + prinpmt) +
"\t\t"
+ format(intpmt) + "\t\t" + format(prinpmt) +
"\t\t"
+ format(bal) + "\n\n\n");
}
msgArea.setText(msg.toString());
}
}
//
// Convert double to dollars and cents
private String format(double dollars)
{
String numString = Double.toString(dollars);
int dotpos = numString.indexOf('.');
if (dotpos < 0) // Check if whole number
return numString;
// Check for excess fraction digits
else if (dotpos < numString.length() - 2)
return numString.substring(0, dotpos + 3); // `.'+ 2 digits
else
return numString + "0"; // Assume only 1 fraction digit
}
}
Thanks in advance,
Mike