简明现代魔法 -> Java编程语言 -> SWT之路:计算银行利息

SWT之路:计算银行利息

2009-10-02

运行演示

程序代码

package SWT;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class BankRate {

	protected Shell shell;
	private Text text;
	private Text text_1;
	static Label rateShow;
	double money;
	double month;
	double rate;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BankRate window = new BankRate();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("银行利息计算");
		{
			Group group = new Group(shell, SWT.NONE);
			group.setText("信息提交");
			group.setBounds(10, 10, 422, 115);
			{
				Label label = new Label(group, SWT.NONE);
				label.setBounds(10, 23, 127, 12);
				label.setText("你想存多少钱?(元)");
			}
			{
				text = new Text(group, SWT.BORDER);
				text.setBounds(143, 17, 180, 18);
				//text.setText("以月份为单位");
			}
			{
				Label label = new Label(group, SWT.NONE);
				label.setBounds(10, 56, 127, 12);
				label.setText("你想存多久?(月)");
			}
			{
				text_1 = new Text(group, SWT.BORDER);
				text_1.setBounds(143, 50, 180, 18);
			}
			{
				Button button = new Button(group, SWT.NONE);
				button.setBounds(340, 46, 72, 22);
				button.setText("计算");
				button.addSelectionListener(new SelectionAdapter() {  
                    public void widgetSelected(SelectionEvent e) {  
                        // 读取输入  
                        money = Double.parseDouble(text.getText());
                        month = Double.parseDouble(text_1.getText());
                        rate = compute(money, month);
                        System.out.println(rate + "RMB");
                        rateShow.setText(money + "元存了 " + month + " 个月得到的" + "利息为 " + rate + " 元");
                    }  
                });  
			}
		}
		{
			Group group = new Group(shell, SWT.NONE);
			group.setText("结果显示");
			group.setBounds(10, 131, 422, 125);
			{
				rateShow = new Label(group, SWT.NONE);
				rateShow.setAlignment(SWT.CENTER);
				rateShow.setBounds(10, 33, 402, 42);
				
			}
		}

	}
	
	public double compute(double money, double month){
		double rate = money * month * 0.0036/12;
		return rate;
	}
}

用于显示结果的label控件需要先声名为static,static Label rateShow;,那样这个名为rateShow的label控件就在堆里存在存储空间了。之后创建这个label的对象就不必再声名其类型了。

关于按钮的事件代码编写如下,首先从两个text空间读取输入并转化为double类型。然后调用compute()方法计算利息,再在rateShow这个空间上显示输出信息。

button.addSelectionListener(new SelectionAdapter() {  
                    public void widgetSelected(SelectionEvent e) {  
                        // 读取输入  
                        money = Double.parseDouble(text.getText());
                        month = Double.parseDouble(text_1.getText());
                        rate = compute(money, month);
                        System.out.println(rate + "RMB");
                        rateShow.setText(money + "元存了 " + month + " 个月得到的" + "利息为 " + rate + " 元");
                    }  
                }); 

计算利息的方法代码如下:

public double compute(double money, double month){
		double rate = money * month * 0.0036/12;
		return rate;
	}

随机文章推荐
网站分类


注:如需转载本文,请注明出处(原文链接),谢谢。更多精彩内容,请进入简明现代魔法首页。

进入新博客
喜欢本文,就分享它吧
给我留言
您的名字:
您的邮件:
您的网站:


 

copyright © 2009 简明现代魔法    学习、分享、进步

power by Gonn 感谢所有关心和支持本站的朋友们