⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 retire.java

📁 java核心技术源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
   @version 1.2 1999-10-13
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
import java.util.*;
import java.text.*;
import java.io.*;
import javax.swing.*;

/**
   This applet shows a retirement calculator. The UI is displayed
   in English, German, and Chinese.
*/
public class Retire extends JApplet
{
   public void init()
   {
      GridBagLayout gbl = new GridBagLayout();
      getContentPane().setLayout(gbl);

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.weightx = 100;
      gbc.weighty = 0;

      gbc.fill = GridBagConstraints.NONE;
      gbc.anchor = GridBagConstraints.EAST;
      add(languageLabel, gbc, 0, 0, 1, 1);
      add(savingsLabel, gbc, 0, 1, 1, 1);
      add(contribLabel, gbc, 2, 1, 1, 1);
      add(incomeLabel, gbc, 4, 1, 1, 1);
      add(currentAgeLabel, gbc, 0, 2, 1, 1);
      add(retireAgeLabel, gbc, 2, 2, 1, 1);
      add(deathAgeLabel, gbc, 4, 2, 1, 1);
      add(inflationPercentLabel, gbc, 0, 3, 1, 1);
      add(investPercentLabel, gbc, 2, 3, 1, 1);

      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.anchor = GridBagConstraints.WEST;
      add(localeCombo, gbc, 1, 0, 2, 1);
      add(savingsField, gbc, 1, 1, 1, 1);
      add(contribField, gbc, 3, 1, 1, 1);
      add(incomeField, gbc, 5, 1, 1, 1);
      add(currentAgeField, gbc, 1, 2, 1, 1);
      add(retireAgeField, gbc, 3, 2, 1, 1);
      add(deathAgeField, gbc, 5, 2, 1, 1);
      add(inflationPercentField, gbc, 1, 3, 1, 1);
      add(investPercentField, gbc, 3, 3, 1, 1);

      computeButton.setName("computeButton");
      computeButton.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               getInfo();
               updateData();
               updateGraph();
            }
         });
      add(computeButton, gbc, 5, 3, 1, 1);

      gbc.weighty = 100;
      gbc.fill = GridBagConstraints.BOTH;
      add(retireCanvas, gbc, 0, 4, 4, 1);
      add(new JScrollPane(retireText), gbc, 4, 4, 2, 1);
      retireText.setEditable(false);
      retireText.setFont(new Font("Monospaced", Font.PLAIN, 10));

      info.setSavings(0);
      info.setContrib(9000);
      info.setIncome(60000);
      info.setCurrentAge(35);
      info.setRetireAge(65);
      info.setDeathAge(85);
      info.setInvestPercent(0.1);
      info.setInflationPercent(0.05);

      localeCombo.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               setCurrentLocale(
                  locales[localeCombo.getSelectedIndex()]);
            }
         });

      locales = new Locale[]
         { Locale.US, Locale.CHINA, Locale.GERMANY };

      int localeIndex = 0; // US locale is default selection

      for (int i = 0; i < locales.length; i++)
         // if current locale one of the choices, we'll select it
         if (getLocale().equals(locales[i])) localeIndex = i;

      setCurrentLocale(locales[localeIndex]);
   }

   /**
      A convenience method to add a component to given grid bag
      layout locations.
      @param c the component to add
      @param gbc the grid bag constraints to use
      @param x the x grid position
      @param y the y grid position
      @param w the grid width
      @param h the grid height
   */
   public void add(Component c, GridBagConstraints gbc,
      int x, int y, int w, int h)
   {
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;
      getContentPane().add(c, gbc);
   }

   /**
      Sets the current locale.
      @param locale the desired locale
   */
   public void setCurrentLocale(Locale locale)
   {
      currentLocale = locale;

      int localeIndex = localeCombo.getSelectedIndex();
      localeCombo.removeAllItems();
      for (int i = 0; i < locales.length; i++)
      {
         String language
            = locales[i].getDisplayLanguage(currentLocale);
         localeCombo.addItem(language);
      }
      if (localeIndex >= 0)
         localeCombo.setSelectedIndex(localeIndex);

      res = ResourceBundle.getBundle("RetireResources",
         currentLocale);
      resStrings = ResourceBundle.getBundle("RetireStrings",
         currentLocale);
      currencyFmt
         = NumberFormat.getCurrencyInstance(currentLocale);
      numberFmt
         = NumberFormat.getNumberInstance(currentLocale);
      percentFmt
         = NumberFormat.getPercentInstance(currentLocale);

      updateDisplay();
      updateInfo();
      updateData();
      updateGraph();
   }

   /**
      Updates all labels in the display.
   */
   public void updateDisplay()
   {
      languageLabel.setText(resStrings.getString("language"));
      savingsLabel.setText(resStrings.getString("savings"));
      contribLabel.setText(resStrings.getString("contrib"));
      incomeLabel.setText(resStrings.getString("income"));
      currentAgeLabel.setText(
         resStrings.getString("currentAge"));
      retireAgeLabel.setText(resStrings.getString("retireAge"));
      deathAgeLabel.setText(resStrings.getString("deathAge"));
      inflationPercentLabel.setText(
         resStrings.getString("inflationPercent"));
      investPercentLabel.setText(
         resStrings.getString("investPercent"));
      computeButton.setText(
         resStrings.getString("computeButton"));

      validate();
   }

   /**
      Updates the information in the text fields.
   */
   public void updateInfo()
   {
      savingsField.setText(
         currencyFmt.format(info.getSavings()));
      contribField.setText(
         currencyFmt.format(info.getContrib()));
      incomeField.setText(currencyFmt.format(info.getIncome()));
      currentAgeField.setText(
         numberFmt.format(info.getCurrentAge()));
      retireAgeField.setText(
         numberFmt.format(info.getRetireAge()));
      deathAgeField.setText(
         numberFmt.format(info.getDeathAge()));
      investPercentField.setText(
         percentFmt.format(info.getInvestPercent()));
      inflationPercentField.setText(
         percentFmt.format(info.getInflationPercent()));
   }

   /**
      Updates the data displayed in the text area.
   */
   public void updateData()
   {
      retireText.setText("");
      MessageFormat retireMsg = new MessageFormat("");
      retireMsg.setLocale(currentLocale);
      retireMsg.applyPattern(resStrings.getString("retire"));

      for (int i = info.getCurrentAge();
           i <= info.getDeathAge(); i++)
      {
         Object[] args = { new Integer(i),
            new Double(info.getBalance(i)) };
         retireText.append(retireMsg.format(args) + "\n");
      }
   }

   /**
      Updates the graph.
   */
   public void updateGraph()
   {
      retireCanvas.setColorPre(
         (Color)res.getObject("colorPre"));
      retireCanvas.setColorGain(
         (Color)res.getObject("colorGain"));
      retireCanvas.setColorLoss(
         (Color)res.getObject("colorLoss"));
      retireCanvas.setInfo(info);
      repaint();
   }

   /**
      Reads the user input from the text fields.
   */
   public void getInfo()
   {
      try
      {
         info.setSavings(currencyFmt.parse(
            savingsField.getText()).doubleValue());
         info.setContrib(currencyFmt.parse(
            contribField.getText()).doubleValue());
         info.setIncome(currencyFmt.parse(
            incomeField.getText()).doubleValue());
         info.setCurrentAge(numberFmt.parse(
            currentAgeField.getText()).intValue());
         info.setRetireAge(numberFmt.parse(
            retireAgeField.getText()).intValue());
         info.setDeathAge(numberFmt.parse(
            deathAgeField.getText()).intValue());
         info.setInvestPercent(percentFmt.parse(
            investPercentField.getText()).doubleValue());
         info.setInflationPercent(percentFmt.parse(
            inflationPercentField.getText()).doubleValue());
      }
      catch (ParseException exception)
      {
      }
   }

   private JTextField savingsField = new JTextField(10);
   private JTextField contribField = new JTextField(10);
   private JTextField incomeField = new JTextField(10);
   private JTextField currentAgeField = new JTextField(4);
   private JTextField retireAgeField = new JTextField(4);
   private JTextField deathAgeField = new JTextField(4);
   private JTextField inflationPercentField = new JTextField(6);
   private JTextField investPercentField = new JTextField(6);
   private JTextArea retireText = new JTextArea(10, 25);
   private RetireCanvas retireCanvas = new RetireCanvas();
   private JButton computeButton = new JButton();
   private JLabel languageLabel = new JLabel();
   private JLabel savingsLabel = new JLabel();
   private JLabel contribLabel = new JLabel();
   private JLabel incomeLabel = new JLabel();
   private JLabel currentAgeLabel = new JLabel();
   private JLabel retireAgeLabel = new JLabel();
   private JLabel deathAgeLabel = new JLabel();
   private JLabel inflationPercentLabel = new JLabel();
   private JLabel investPercentLabel = new JLabel();

   private RetireInfo info = new RetireInfo();

   private Locale[] locales;
   private Locale currentLocale;
   private JComboBox localeCombo = new JComboBox();
   private ResourceBundle res;
   private ResourceBundle resStrings;
   private NumberFormat currencyFmt;
   private NumberFormat numberFmt;
   private NumberFormat percentFmt;
}

/**
   The information required to compute retirement income data.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -