import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
/**
* This applet shows a retirement calculator. The UI is displayed in English, German, and Chinese.
* @version 1.22 2007-07-25
* @author Cay Horstmann
*/
public class Retire extends JApplet
{
public void init()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
initUI();
}
});
}
public void initUI()
{
setLayout(new GridBagLayout());
add(languageLabel, new GBC(0, 0).setAnchor(GBC.EAST));
add(savingsLabel, new GBC(0, 1).setAnchor(GBC.EAST));
add(contribLabel, new GBC(2, 1).setAnchor(GBC.EAST));
add(incomeLabel, new GBC(4, 1).setAnchor(GBC.EAST));
add(currentAgeLabel, new GBC(0, 2).setAnchor(GBC.EAST));
add(retireAgeLabel, new GBC(2, 2).setAnchor(GBC.EAST));
add(deathAgeLabel, new GBC(4, 2).setAnchor(GBC.EAST));
add(inflationPercentLabel, new GBC(0, 3).setAnchor(GBC.EAST));
add(investPercentLabel, new GBC(2, 3).setAnchor(GBC.EAST));
add(localeCombo, new GBC(1, 0, 3, 1));
add(savingsField, new GBC(1, 1).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(contribField, new GBC(3, 1).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(incomeField, new GBC(5, 1).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(currentAgeField, new GBC(1, 2).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(retireAgeField, new GBC(3, 2).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(deathAgeField, new GBC(5, 2).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(inflationPercentField, new GBC(1, 3).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(investPercentField, new GBC(3, 3).setWeight(100, 0).setFill(GBC.HORIZONTAL));
add(retireCanvas, new GBC(0, 4, 4, 1).setWeight(100, 100).setFill(GBC.BOTH));
add(new JScrollPane(retireText), new GBC(4, 4, 2, 1).setWeight(0, 100).setFill(GBC.BOTH));
computeButton.setName("computeButton");
computeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
getInfo();
updateData();
updateGraph();
}
});
add(computeButton, new GBC(5, 3));
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);
int localeIndex = 0; // US locale is default selection
for (int i = 0; i < locales.length; i++)
// if current locale one of the choices, select it
if (getLocale().equals(locales[i])) localeIndex = i;
setCurrentLocale(locales[localeIndex]);
localeCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setCurrentLocale((Locale) localeCombo.getSelectedItem());
validate();
}
});
}
/**
* Sets the current locale.
* @param locale the desired locale
*/
public void setCurrentLocale(Locale locale)
{
currentLocale = locale;
localeCombo.setSelectedItem(currentLocale);
localeCombo.setLocale(currentLocale);
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"));
}
/**
* 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 = { i, 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 e)
{
}
}
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
没有合适的资源?快使用搜索试试~ 我知道了~
Java 核心技术 第八版源代码【Core Java. Volume II. Advanced Features, 8th Ed...
共352个文件
java:184个
gif:71个
png:15个
需积分: 7 21 下载量 141 浏览量
2010-07-25
21:05:09
上传
评论
收藏 1.2MB RAR 举报
温馨提示
Java 核心技术 第八版源代码【Core Java. Volume II. Advanced Features, 8th Edition】
资源推荐
资源详情
资源评论
收起资源包目录
Java 核心技术 第八版源代码【Core Java. Volume II. Advanced Features, 8th Edition】 (352个子文件)
Win32RegKey.c 11KB
InvocationTest.c 4KB
Printf4.c 3KB
Printf3.c 2KB
Printf2.c 2KB
Employee.c 658B
Printf1.c 373B
HelloNative.c 232B
CalculatorPanel.caesar 2KB
CalculatorPanel$CommandAction.caesar 1KB
CalculatorPanel$InsertAction.caesar 1KB
CalculatorFrame.caesar 410B
Calculator.caesar 395B
CalculatorPanel$1.caesar 191B
jaas.config 81B
jaas.config 75B
employee.dat 340B
employee.dat 95B
employee.dat 93B
gridbag.dtd 823B
gridbag.dtd 823B
fortunes 24KB
clouds.gif 30KB
cj2.gif 13KB
e-mail.gif 9KB
cj2v1.gif 8KB
Saturn.gif 5KB
Saturn.gif 5KB
Saturn.gif 5KB
Saturn.gif 5KB
Saturn.gif 5KB
Jupiter.gif 3KB
Jupiter.gif 3KB
Jupiter.gif 3KB
Jupiter.gif 3KB
Jupiter.gif 3KB
Earth.gif 3KB
Earth.gif 3KB
Earth.gif 3KB
Earth.gif 3KB
Earth.gif 3KB
Venus.gif 3KB
Venus.gif 3KB
Venus.gif 3KB
Venus.gif 3KB
Venus.gif 3KB
Moons.gif 2KB
Neptune.gif 2KB
Neptune.gif 2KB
Neptune.gif 2KB
Neptune.gif 2KB
Neptune.gif 2KB
Uranus.gif 2KB
Uranus.gif 2KB
Uranus.gif 2KB
Uranus.gif 2KB
Uranus.gif 2KB
Mars.gif 2KB
Mars.gif 2KB
Mars.gif 2KB
Mars.gif 2KB
Mars.gif 2KB
blue-ball.gif 925B
yellow-ball.gif 925B
document.gif 925B
yellow-ball.gif 925B
ChartBean_MONO_16x16.gif 877B
ChartBean2_MONO_16x16.gif 877B
ChartBean_MONO_16x16.gif 877B
Mercury.gif 700B
Mercury.gif 700B
Mercury.gif 700B
Mercury.gif 700B
Mercury.gif 700B
face.gif 614B
red-ball.gif 527B
red-ball.gif 527B
Pluto.gif 402B
Pluto.gif 402B
Pluto.gif 402B
Pluto.gif 402B
Pluto.gif 402B
ChartBean_COLOR_32x32.gif 207B
ChartBean2_COLOR_32x32.gif 207B
ChartBean_COLOR_32x32.gif 207B
ChartBean_MONO_32x32.gif 166B
ChartBean2_MONO_32x32.gif 166B
ChartBean_MONO_32x32.gif 166B
ChartBean_COLOR_16x16.gif 132B
ChartBean_COLOR_16x16.gif 132B
ChartBean2_COLOR_16x16.gif 132B
ChartBean_INVERSE_16x16.gif 71B
ChartBean_INVERSE_16x16.gif 71B
HelloNative.h 387B
FileReadApplet.html 107B
VerifierTest.html 72B
Retire.html 64B
bcel-5.3-SNAPSHOT.jar 554KB
ChartBean.jar 27KB
ChartBean2.jar 17KB
共 352 条
- 1
- 2
- 3
- 4
资源评论
wenli103
- 粉丝: 0
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功