展开全部
这位小弟是初入java吧,代636f70793231313335323631343130323136353331333332633639码敲的不对,有的地方还和C语言混淆了,我大致改了下代码,中间的逻辑可能还有问题,仅供你参考:package com.test;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* @作者 王建明
* @创建日期 Sep 21, 2013
* @创建时间 11:08:27 AM
* @版本号 V 1.0
*/
public class AppGraphicsAdvence {
public static void main(String args[]) {
new FrameInOut();
}
}
class FrameInOut extends Frame implements ActionListener {
JButton btn, btn1, btn2, btn3, btn4;
JTextArea ta, ta1, ta2;
JPanel p, p1, p2, p3, p4;
FrameInOut() {
super("10万元人民币分别按照不同利率存入银行5年内每种利率的存款余额");
this.setFont(new Font("隶体", Font.BOLD, 100));
this.setBackground(Color.pink);
btn = new JButton("5%利率");
btn1 = new JButton("8%利率");
btn2 = new JButton("12%利率");
btn3 = new JButton("3种利率比较");
btn4 = new JButton("退出");
ta = new JTextArea(8, 20);
ta1 = new JTextArea(8, 20);
p = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p.add(btn);
p1.add(btn1);
p2.add(btn2);
p3.add(btn3);
p4.add(btn4);
add(p);
add(p1);
add(p2);
add(p3);
add(p4);
setLayout(new FlowLayout());
p1.setBackground(Color.red);
btn.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setSize(900, 360);
setVisible(true);
}
private void calculate(int n) {
for (int i = 1; i <= 5; i++) {
System.out.println("第" + i + "年" + numberFormat(10 * Math.pow(1 + n / 100.0, i), 3) + "万\r\n");
}
}
/**
* @param val
* @return
* @作者 王建明
* @创建日期 Sep 21, 2013
* @创建时间 11:03:12 AM
* @描述 —— 四舍五入保留n位小数
*/
public double numberFormat(double val, int n) {
BigDecimal b = new BigDecimal(val);
double f1 = b.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
ta.setText(null);
ta.setForeground(Color.blue);
ta.setFont(new Font("隶体", Font.BOLD, 14));
System.out.println("利率为5%时:");
calculate(5);
}
if (e.getSource() == btn4) {
dispose();
System.exit(0);
}
}
}
本文提供了一个Java GUI编程案例,使用Swing组件实现了一个显示不同利率下存款余额变化的应用。通过按钮选择不同的利率,程序将计算并展示五年内每年的余额。
973

被折叠的 条评论
为什么被折叠?



