By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

Creating multiple windows from an applet

P: 8
I'm suppose to write an applet that contains two buttons Investment calculator and Loan Calculator. When the Investment Calculator button is clicked, a frame appears in a new window for calculating future investment values. When you click Loan Calculator button, a frame appears in a separate new window for computong loan payments.

Here's my coding:

// Lab3.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Lab3 extends JApplet implements ActionListener {
private Investment investmentFrame = new Investment();
private JFrame loanFrame = new JFrame("Loan Calculator");
private JButton jbtInvestment = new JButton("Investment Calculator");
private JButton jbtLoan = new JButton("Loan Calculator");

public void init() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtInvestment);
getContentPane().add(jbtLoan);
jbtInvestment.addActionListener(this);
jbtLoan.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtInvestment) {
investmentFrame.setSize(300, 200);
investmentFrame.setTitle("Investment Calculator");
investmentFrame.setVisible(true);
}
else if (e.getSource() == jbtLoan) {
loanFrame.setSize(300, 200);
LoanApplet applet = new LoanApplet();
loanFrame.getContentPane().add(applet);
applet.init();
loanFrame.setVisible(true);
}
}
public static void main(String[] args) {
//Create a frame
JFrame frame = new JFrame("Lab3");

//Create an instance of the applet
Lab3 applet = new Lab3();

//Add the applet to the frame
frame.add(applet, BorderLayout.CENTER);

//Invoke init and start
applet.init();
applet.start();

//Display the frame
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(300, 300);
frame.setVisible(true);
}
}



Here's the coding for the LoanApplet:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class LoanApplet extends JApplet {
// Declare and create text fields for interest rate
// year, loan amount, monthly payment, and total payment
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumberOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JTextField jtfMonthlyPayment = new JTextField();
private JTextField jtfTotalPayment = new JTextField();

// Declare and create a Compute Payment button
private JButton jbtComputeLoan = new JButton("Compute Payment");

/** Initialize user interface */
public void init() {
// Set properties on the text fields
jtfMonthlyPayment.setEditable(false);
jtfTotalPayment.setEditable(false);

// Right align text fields
jtfAnnualInterestRate.setHorizontalAlignment(JText Field.RIGHT);
jtfNumberOfYears.setHorizontalAlignment(JTextField .RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RI GHT);
jtfMonthlyPayment.setHorizontalAlignment(JTextFiel d.RIGHT);
jtfTotalPayment.setHorizontalAlignment(JTextField. RIGHT);

// Panel p1 to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(5, 2));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new JLabel("Number of Years"));
p1.add(jtfNumberOfYears);
p1.add(new JLabel("Loan Amount"));
p1.add(jtfLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(jtfMonthlyPayment);
p1.add(new JLabel("Total Payment"));
p1.add(jtfTotalPayment);
p1.setBorder(new
TitledBorder("Enter interest rate, year and loan amount"));

// Panel p2 to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtComputeLoan);

// Add the components to the applet
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);

// Register listener
jbtComputeLoan.addActionListener(new ButtonListener());
}

/** Handle the Compute Payment button */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get values from text fields
double interest =
Double.parseDouble(jtfAnnualInterestRate.getText() );
int year =
Integer.parseInt(jtfNumberOfYears.getText());
double loanAmount =
Double.parseDouble(jtfLoanAmount.getText());

// Create a loan object
Loan loan = new Loan(interest, year, loanAmount);

// Display monthly payment and total payment
jtfMonthlyPayment.setText(String.format("%.2f",
loan.getMonthlyPayment()));
jtfTotalPayment.setText(String.format("%.2f",
loan.getTotalPayment()));
}
}

public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Applet is in the frame");

// Create an instance of the applet
LoanApplet applet = new LoanApplet();

// Add the applet to the frame
frame.add(applet, BorderLayout.CENTER);

// Invoke applet's init method
applet.init();

// Display the frame
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(300, 300);
frame.setVisible(true);
}
}



Here's my command results I don't see what I'm doing wrong please help:

K:\Lab3.java:28: cannot find symbol
symbol : class LoanApplet
location: class Lab3
LoanApplet applet = new LoanApplet();
^
K:\Lab3.java:28: cannot find symbol
symbol : class LoanApplet
location: class Lab3
LoanApplet applet = new LoanApplet();
^
2 errors

Tool completed with exit code 1
Oct 26 '07 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
I'm suppose to write an applet that contains two buttons Investment calculator and Loan Calculator. When the Investment Calculator button is clicked, a frame appears in a new window for calculating future investment values. When you click Loan Calculator button, a frame appears in a separate new window for computong loan payments.

Here's my coding:

// Lab3.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Lab3 extends JApplet implements ActionListener {
private Investment investmentFrame = new Investment();
private JFrame loanFrame = new JFrame("Loan Calculator");
private JButton jbtInvestment = new JButton("Investment Calculator");
private JButton jbtLoan = new JButton("Loan Calculator");

public void init() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtInvestment);
getContentPane().add(jbtLoan);
jbtInvestment.addActionListener(this);
jbtLoan.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtInvestment) {
investmentFrame.setSize(300, 200);
investmentFrame.setTitle("Investment Calculator");
investmentFrame.setVisible(true);
}
else if (e.getSource() == jbtLoan) {
loanFrame.setSize(300, 200);
LoanApplet applet = new LoanApplet();
loanFrame.getContentPane().add(applet);
applet.init();
loanFrame.setVisible(true);
}
}
public static void main(String[] args) {
//Create a frame
JFrame frame = new JFrame("Lab3");

//Create an instance of the applet
Lab3 applet = new Lab3();

//Add the applet to the frame
frame.add(applet, BorderLayout.CENTER);

//Invoke init and start
applet.init();
applet.start();

//Display the frame
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(300, 300);
frame.setVisible(true);
}
}



Here's the coding for the LoanApplet:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class LoanApplet extends JApplet {
// Declare and create text fields for interest rate
// year, loan amount, monthly payment, and total payment
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumberOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JTextField jtfMonthlyPayment = new JTextField();
private JTextField jtfTotalPayment = new JTextField();

// Declare and create a Compute Payment button
private JButton jbtComputeLoan = new JButton("Compute Payment");

/** Initialize user interface */
public void init() {
// Set properties on the text fields
jtfMonthlyPayment.setEditable(false);
jtfTotalPayment.setEditable(false);

// Right align text fields
jtfAnnualInterestRate.setHorizontalAlignment(JText Field.RIGHT);
jtfNumberOfYears.setHorizontalAlignment(JTextField .RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RI GHT);
jtfMonthlyPayment.setHorizontalAlignment(JTextFiel d.RIGHT);
jtfTotalPayment.setHorizontalAlignment(JTextField. RIGHT);

// Panel p1 to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(5, 2));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new JLabel("Number of Years"));
p1.add(jtfNumberOfYears);
p1.add(new JLabel("Loan Amount"));
p1.add(jtfLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(jtfMonthlyPayment);
p1.add(new JLabel("Total Payment"));
p1.add(jtfTotalPayment);
p1.setBorder(new
TitledBorder("Enter interest rate, year and loan amount"));

// Panel p2 to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtComputeLoan);

// Add the components to the applet
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);

// Register listener
jbtComputeLoan.addActionListener(new ButtonListener());
}

/** Handle the Compute Payment button */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get values from text fields
double interest =
Double.parseDouble(jtfAnnualInterestRate.getText() );
int year =
Integer.parseInt(jtfNumberOfYears.getText());
double loanAmount =
Double.parseDouble(jtfLoanAmount.getText());

// Create a loan object
Loan loan = new Loan(interest, year, loanAmount);

// Display monthly payment and total payment
jtfMonthlyPayment.setText(String.format("%.2f",
loan.getMonthlyPayment()));
jtfTotalPayment.setText(String.format("%.2f",
loan.getTotalPayment()));
}
}

public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Applet is in the frame");

// Create an instance of the applet
LoanApplet applet = new LoanApplet();

// Add the applet to the frame
frame.add(applet, BorderLayout.CENTER);

// Invoke applet's init method
applet.init();

// Display the frame
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(300, 300);
frame.setVisible(true);
}
}



Here's my command results I don't see what I'm doing wrong please help:

K:\Lab3.java:28: cannot find symbol
symbol : class LoanApplet
location: class Lab3
LoanApplet applet = new LoanApplet();
^
K:\Lab3.java:28: cannot find symbol
symbol : class LoanApplet
location: class Lab3
LoanApplet applet = new LoanApplet();
^
2 errors

Tool completed with exit code 1
Please use code tags when posting code.
Did you compile the LoanApplet class successfully first?
Oct 26 '07 #2

P: 8
Please use code tags when posting code.
Did you compile the LoanApplet class successfully first?
Yes the LoanApplet compiled and ran successfully
Oct 26 '07 #3

P: 8
Please use code tags when posting code.
Did you compile the LoanApplet class successfully first?
Yes it complied and ran successfully
Oct 26 '07 #4

10K+
P: 13,264
Yes it complied and ran successfully
Are these classes in the same folder?
Oct 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.