为什么我的applet不符合我指定的大小?

时间:2011-02-15 01:57:29

标签: java

它始终打开非常小,不会更改为我指定的任何大小。 Wat do?

    //import all needed functionality
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
import java.io.*;
public class ScrofaniWk3 extends JApplet{

/**
 * 
 */
private static final long serialVersionUID = 1L;

/**
 * @param args
 */
    // Declare variables and put code application logic here

    double mortgage;
    int num;
    String userInput = null;
    JLabel loanAmountLabel = new JLabel("Loan Amount: ");
    JTextField loanAmount = new JTextField();
    double[] ratesList = {0.0535, 0.055, 0.0575};
    JLabel rateLabel=new JLabel("Interest Rate: ");
    JTextField rate=new JTextField();
    String[] yearsList = {"7","15","30"};
    JLabel yearsLabel=new JLabel("Years of Payment: ");
    JTextField years=new JTextField();
    JComboBox chooseYears = new JComboBox(yearsList);
    JLabel payLabel=new JLabel("Monthly Payment: ");
    JLabel payment=new JLabel();
    private JButton calculate=new JButton("Calculate");
    private JButton clear=new JButton("Clear");
    private JButton exit=new JButton("Quit");
    JTextArea payments=new JTextArea();
    JScrollPane schedulePane=new JScrollPane(payments);
    Container mortCalc = getContentPane();


    public void init() {
    //This makes the chooseYears function
    chooseYears.setSelectedIndex(0);
    chooseYears.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent choose) {
    JComboBox cb = (JComboBox)choose.getSource();
    // JComboBox is the event's source
    String termYear = (String)cb.getSelectedItem();
    years.setText(termYear);
    int index=0;
    switch (Integer.parseInt(termYear)) {
    case 7: index=0; break;
    case 15: index=1; break;
    case 30: index=2; break;
    }
    rate.setText(ratesList[index]+"");
    }
    });
    calculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {    
                // Perform the calculation
                double yearsCalc=Integer.parseInt(years.getText())*12;
                double principalCalc=Double.parseDouble(loanAmount.getText());
                double rateCalc=Double.parseDouble(rate.getText())/12;
                double monthlyPayment=principalCalc*Math.pow(1+rateCalc,yearsCalc)*rateCalc/(Math.pow(1+rateCalc,yearsCalc)-1);

                DecimalFormat df = new DecimalFormat("$###,###.##");
                payment.setText(df.format(monthlyPayment));

                // Perform extra calculations to show the loan amount after each subsequent payoff
                double principal=principalCalc;
                int month;
                StringBuffer buffer=new StringBuffer();
                buffer.append("Month\tAmount\tInterest\tBalance\n");
                for (int f=0; f<yearsCalc; f++) {
                month=f+1;
                double interest=principal*rateCalc;
                double balance=principal+interest-monthlyPayment;
                buffer.append(month+"\t");
                buffer.append(new String(df.format(principal))+"\t");
                buffer.append(new String(df.format(interest))+"\t");
                buffer.append(new String(df.format(balance))+"\n");
                principal=balance;
                }
                payments.setText(buffer.toString());
                } catch(Exception ex) {
                System.out.println(ex);
            }
        }
    });
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            loanAmount.setText("");
            payment.setText("");
            payments.setText("");
        }
    });
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(1);
        }
    });
//Configure the graphical user interface for data input and output  
        JPanel panelMort=new JPanel();
        panelMort.setLayout(new GridLayout(5,2));
        panelMort.add(loanAmountLabel); panelMort.add(loanAmount);  
        panelMort.add(yearsLabel); panelMort.add(years);
        panelMort.add(new Label()); panelMort.add(chooseYears);
        panelMort.add(rateLabel); panelMort.add(rate);
        panelMort.add(payLabel); panelMort.add(payment);
        JPanel buttons=new JPanel();
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
        buttons.add(calculate); buttons.add(clear); buttons.add(exit);
        JPanel panelMort2=new JPanel();
        panelMort2.setLayout(new BoxLayout(panelMort2, BoxLayout.Y_AXIS));
        panelMort2.add(panelMort); panelMort2.add(buttons);
        mortCalc.add(BorderLayout.NORTH, panelMort);
        mortCalc.add(BorderLayout.CENTER, schedulePane);
    }
    public static void main(String[] args) {
        JApplet applet = new ScrofaniWk3();
        JFrame frame = new JFrame("ScrofaniWk3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.getContentPane().add(applet);
        frame.setSize(380,400);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
}

0 个答案:

没有答案