The Artima Developer Community
Sponsored Link

Java Answers Forum
Can't get my subwindows to show up

1 reply on 1 page. Most recent reply: Jan 12, 2004 11:38 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Lynn Robertson

Posts: 4
Nickname: looloo
Registered: Dec, 2003

Can't get my subwindows to show up Posted: Dec 7, 2003 6:48 PM
Reply to this message Reply
Advertisement
Hi There,
Can anyone out there help? The subwindows won't show up in my applet. Any suggestions? Here's my code:

//MultiWin.java

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.setVisible(true);
calcFrame.setSize(400, 200);
calcFrame.setTitle("Calculator");
}
else if ("Hide Simple Calculator".equals(arg))
{
calcFrame.setVisible(false);
jbtCalc.setText("Simple Calculator");
}

else if("Mortgage".equals(arg))
{
jbtMort.setText("Hide Mortgage Calculator");
mortFrame.setVisible(true);
mortFrame.setSize(400, 200);
mortFrame.setTitle("Mortgage");
}
else if ("Hide Mortgage Calculator".equals(arg))
{
mortFrame.setVisible(false);
jbtMort.setText("Mortgage");
}
}
}

class SimpCalc extends JFrame 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;



// Default Constructor
public SimpCalc()
{

// Create menu bar
JMenuBar jmb = new JMenuBar();
JFrame calcFrame = new JFrame();

// 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 JFrame 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,Bo rderLayout.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.monthlyPay()));
jtfTotalPaym ent.setText(String.valueOf(m.totalPay()));
}
}
}
public class Mortgage
{
private double interest;
private int year;
private double loan;

// Construct a mortgage with specified interest rate, year and
// loan amount
public Mortgage(double i, int y, double l)
{
interest = i/1200.0;
year = y;
loan = l;
}

// Getter method for interest
public double getInterest()
{
return interest;
}

// Getter method for year
public double getYear()
{
return year;
}

// Getter method for loan
public double getLoan()
{
return loan;
}

// Find monthly pay
public double monthlyPay()
{
return loan*interest/(1-(Math.pow(1/(1+interest),year*12)));
}

// Find total pay
public double totalPay()
{
return monthlyPay()*year*12;
}
}
}

Thanks,
Lynn


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Can't get my subwindows to show up Posted: Jan 12, 2004 11:38 PM
Reply to this message Reply
subwindows ?? what do u mean by not shown up? i can see the sub windows.

Flat View: This topic has 1 reply on 1 page
Topic: security policy code Previous Topic   Next Topic Topic: Simulation of an algae colony

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use