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

📄 exercise23_1.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
// Exercise23_1.java: Compute future valueimport java.awt.*;import java.awt.event.*;import java.text.*;import java.util.*;import javax.swing.*;public class Exercise23_1 extends JApplet implements ActionListener {  private JTextField jtfInvestmentAmount;  private JTextField jtfYears;  private JTextField jtfInterestRate;  private JTextField jtfFutureValue;  private JButton jbtCalculate;  private JMenuItem jmiCalculate = new JMenuItem("Calculate");  private JMenuItem jmiExit = new JMenuItem("Exit");  private JMenuItem jmiAbout = new JMenuItem("About");  public Exercise23_1() {    // Create the menu bar    JMenuBar jmb = new JMenuBar();    this.setJMenuBar(jmb);    // Create menus    JMenu operationMenu = new JMenu("Operation");    jmb.add(operationMenu);    JMenu helpMenu = new JMenu("Help");    jmb.add(helpMenu);    // Add menu items    operationMenu.add(jmiCalculate);    operationMenu.addSeparator();    operationMenu.add(jmiExit);    helpMenu.add(jmiAbout);    // Create a new panel with GridLayout to hold labels and text boxes    JPanel p = new JPanel();    p.setLayout(new GridLayout(4, 2));    p.add(new JLabel("Investment Amount"));    p.add(jtfInvestmentAmount = new JTextField(8));    p.add(new JLabel("Years"));    p.add(jtfYears = new JTextField(8));    p.add(new JLabel("Annual Interest Rate"));    p.add(jtfInterestRate = new JTextField(8));    p.add(new JLabel("Future value"));    p.add(jtfFutureValue = new JTextField(8));    jtfFutureValue.setEditable(false);    // Add the panel and a button to the frame    getContentPane().setLayout(new BorderLayout());    getContentPane().add(p, BorderLayout.CENTER);    getContentPane().add(jbtCalculate = new JButton("Calculate"), BorderLayout.SOUTH);    // Register listener    jbtCalculate.addActionListener(this);    jmiCalculate.addActionListener(this);    jmiExit.addActionListener(this);    jmiAbout.addActionListener(this);  }  // Handle ActionEvent  public void actionPerformed(ActionEvent e) {    if ((e.getSource() == jbtCalculate) ||      (e.getSource() == jmiCalculate)) {      calculate();    }    else if (e.getSource() == jmiExit) {      System.exit(0);    }    else if (e.getSource() == jmiAbout) {      JOptionPane.showMessageDialog(this,        "Compute Future Investment Value",        "About This Program", JOptionPane.INFORMATION_MESSAGE);    }  }  // Calculate future value  public void calculate() {    double investmentAmount =    Double.valueOf(jtfInvestmentAmount.getText().trim()).doubleValue();    int years =      Integer.valueOf(jtfYears.getText().trim()).intValue();    double interestRate = Double.valueOf(jtfInterestRate.getText().trim()).doubleValue();    double futureValue = investmentAmount * Math.pow((1 + interestRate / (12 * 100)), 12 * years);    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);    jtfFutureValue.setText(nf.format(futureValue));//    jtfFutureValue.setText((int)(futureValue * 100) / 100.0 + "");  }  public static void main(String[] args) {    Exercise23_1 applet = new Exercise23_1();    JFrame frame = new JFrame();    //EXIT_ON_CLOSE == 3    frame.setDefaultCloseOperation(3);    frame.setTitle("Exercise23_1");    frame.getContentPane().add(applet, BorderLayout.CENTER);    applet.init();    applet.start();    frame.setSize(400,320);    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();    frame.setLocation((d.width - frame.getSize().width) / 2,      (d.height - frame.getSize().height) / 2);    frame.setVisible(true);  }}

⌨️ 快捷键说明

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