Get your CodeRanch badge!*
The moose likes Applets and the fly likes Multiple Windows in Applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Multiple Windows in Applets" Watch "Multiple Windows in Applets" New topic
Author

Multiple Windows in Applets

Marie Jeanne Thibault
Greenhorn

Joined: Nov 15, 2003
Posts: 15
Hi!
Can anyone see where I went off the rails with this one? I can't seem to get the windows to show up. Here's my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class MultiWin extends JApplet implements ActionListener
{
private JButton jbtCalc;
private JButton jbtMort;

//Create the frames to hold the calculator and mortgage calculators
JFrame calcFrame = new JFrame();
JFrame mortFrame = new JFrame();
//Construct the frame
public void init()
{
//Place the buttons in the frame
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtCalc = new JButton("Simple Calculator"));
getContentPane().add(jbtMort = new JButton("Mortgage"));
//Register Listeners
jbtCalc.addActionListener(this);
jbtMort.addActionListener(this);
}
//Handle the buttons
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (e.getSource() instanceof JButton)
if("Simple Calculator".equals(arg))
{
jbtCalc.setText("Hide Simple Calculator");
calcFrame.pack();
calcFrame.setVisible(true);
}
else if ("Hide Simple Calculator".equals(arg))
{
calcFrame.setVisible(false);
jbtCalc.setText("Simple Calculator");
}
else if("Mortgage".equals(arg))
{
mortFrame.pack();
jbtMort.setText("Hide Mortgage Calculator");
mortFrame.setVisible(true);
}
else if ("Hide Mortgage Calculator".equals(arg))
{
mortFrame.setVisible(false);
jbtMort.setText("Mortgage");
}
}
}
class SimpCalc extends JApplet implements ActionListener
{
// Text fields for Number 1, Number 2, and Result
private JTextField jtfNum1, jtfNum2, jtfResult;
// Buttons "Add", "Subtract", "Multiply" and "Divide"
private JButton jbtAdd, jbtSub, jbtMul, jbtDiv;
// Menu items "Add", "Subtract", "Multiply","Divide" and "Close"
private JMenuItem jmiAdd, jmiSub, jmiMul, jmiDiv, jmiClose;
//Main Method
public void main(String[] args)
{
SimpCalc frame = new SimpCalc();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
// Default Constructor
public SimpCalc()
{
setTitle("Simple Calculator");
// Create menu bar
JMenuBar jmb = new JMenuBar();

// Add menu "Operation" to menu bar
JMenu operationMenu = new JMenu("Operation");
operationMenu.setMnemonic('O');
jmb.add(operationMenu);
// Add menu "Exit" in menu bar
JMenu exitMenu = new JMenu("Exit");
exitMenu.setMnemonic('E');
jmb.add(exitMenu);
// Add menu items with mnemonics to menu "Operation"
operationMenu.add(jmiAdd= new JMenuItem("Add", 'A'));
operationMenu.add(jmiSub = new JMenuItem("Subtract", 'S'));
operationMenu.add(jmiMul = new JMenuItem("Multiply", 'M'));
operationMenu.add(jmiDiv = new JMenuItem("Divide", 'D'));
exitMenu.add(jmiClose = new JMenuItem("Close", 'C'));
// Set keyboard accelerators
jmiAdd.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
jmiSub.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
jmiMul.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
jmiDiv.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
// Panel p1 to hold text fields and labels
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(new JLabel("Number 1"));
p1.add(jtfNum1 = new JTextField(3));
p1.add(new JLabel("Number 2"));
p1.add(jtfNum2 = new JTextField(3));
p1.add(new JLabel("Result"));
p1.add(jtfResult = new JTextField(4));
jtfResult.setEditable(false);
// Panel p2 to hold buttons
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jbtAdd = new JButton("Add"));
p2.add(jbtSub = new JButton("Subtract"));
p2.add(jbtMul = new JButton("Multiply"));
p2.add(jbtDiv = new JButton("Divide"));
// Add menubar to the frame
setJMenuBar(jmb);

//Add the panels to the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
// Register listeners
jbtAdd.addActionListener(this);
jbtSub.addActionListener(this);
jbtMul.addActionListener(this);
jbtDiv.addActionListener(this);
jmiAdd.addActionListener(this);
jmiSub.addActionListener(this);
jmiMul.addActionListener(this);
jmiDiv.addActionListener(this);
jmiClose.addActionListener(this);
}
// Handle ActionEvent from buttons and menu items
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
// Handle button events
if (e.getSource() instanceof JButton)
{
if ("Add".equals(actionCommand))
calculate('+');
else if ("Subtract".equals(actionCommand))
calculate('-');
else if ("Multiply".equals(actionCommand))
calculate('*');
else if ("Divide".equals(actionCommand))
calculate('/');
}
else if (e.getSource() instanceof JMenuItem)
{
// Handle menu item events
if ("Add".equals(actionCommand))
calculate('+');
else if ("Subtract".equals(actionCommand))
calculate('-');
else if ("Multiply".equals(actionCommand))
calculate('*');
else if ("Divide".equals(actionCommand))
calculate('/');
else if ("Close".equals(actionCommand))
System.exit(0);
}
}
// Calculate and show the result in jtfResult
private void calculate(char operator)
{
// Obtain Number 1 and Number 2
int num1 = (Integer.parseInt(jtfNum1.getText().trim()));
int num2 = (Integer.parseInt(jtfNum2.getText().trim()));
int result = 0;
// Perform selected operation
switch (operator)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
}
// Set result in jtfResult
jtfResult.setText(String.valueOf(result));
}
public class MortCalc extends JApplet implements ActionListener
{
//Declare and create textfields for interest rate
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JTextField jtfMonthlyPayment = new JTextField();
private JTextField jtfTotalPayment = new JTextField();
private JButton jbtComputeMortgage = new JButton("Compute Mortgage");
//Initialize the interface
public void init()
{
//Set properties for the text fields
jtfMonthlyPayment.setEditable(false);
jtfTotalPayment.setEditable(false);
//Align the text Fields
jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);
//Panel p1 to hold labels and text fields
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout (5,2));
p1.add(new Label("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new Label("Number of Years"));
p1.add(new Label("Loan Amount"));
p1.add(jtfLoanAmount);
p1.add(new Label("Monthly Payment"));
p1.add(jtfMonthlyPayment);
p1.add(new Label("Total Payment"));
p1.add(jtfTotalPayment);
p1.setBorder(new TitledBorder("Enter interest rate, year and loan amount"));
//panel p2 holds the button
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtComputeMortgage);
//Add the components to the applet
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
//Register Listener
jbtComputeMortgage.addActionListener(this);
}
//Handler for the button
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtComputeMortgage)
{
double interest = (Double.valueOf(jtfAnnualInterestRate.getText())).doubleValue();
int year = (Integer.valueOf(jtfNumOfYears.getText())).intValue();
double loan = (Double.valueOf(jtfLoanAmount.getText())).doubleValue();
//Create a mortgage object
double Mortgage;
Mortgage m = new Mortgage(interest, year, loan);
//Display monthly pament and total payment
jtfMonthlyPayment.setText(String.valueOf(m.monthlyPayment()));
jtfTotalPayment.setText(String.valueOf(m.totalPayment()));
}
}
}
}
It won't compile. I debug halfway through but the windows wouldn't show up. Help!
Thanks,
Marie
[ November 29, 2003: Message edited by: Marie Jeanne Thibault ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ur code is too long to get into it. plz ask ur que in precise. like what exactly u need
 
 
subject: Multiple Windows in Applets
 
Similar Threads
Setting the Background color in a panel.
calculation problem in my applet
calculation problem with applet
Critique my beginner basic GUI calculation program and need some clarifications
No compilation error in 1 dir, but compilation error in other, why?