简明现代魔法 -> Java编程语言 -> SWT之路:外币兑换

SWT之路:外币兑换

2009-10-04

程序运行演示

程序代码

package SWT;

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

public class ExchangeRate {

	protected Shell shell;
	private Text text;
	static Label resultShow;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ExchangeRate window = new ExchangeRate();
			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, 161);
			{
				text = new Text(group, SWT.BORDER);
				text.setBounds(10, 24, 70, 18);
				text.setText("0");
			}
			{
				Button button = new Button(group, SWT.RADIO);
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
					}
				});
				button.setBounds(101, 24, 93, 16);
				button.setText("美元");
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = dollarToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
				//double money = Double.parseDouble(text.getText());
				//double rmb =dollarToRMB(money);
				//resultShow.setText(rmb + "");
			}
			{
				Button button = new Button(group, SWT.RADIO);
				button.setBounds(101, 46, 93, 16);
				button.setText("欧元");
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = europedollarToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
			}
			{
				Button button = new Button(group, SWT.RADIO);
				button.setBounds(101, 68, 93, 16);
				button.setText("日元");
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = japaneseToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
			}
			{
				Button button = new Button(group, SWT.RADIO);
				button.setBounds(101, 90, 93, 16);
				button.setText("英镑");
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = englishToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
			}
			{
				Button button = new Button(group, SWT.RADIO);
				button.setBounds(101, 112, 93, 16);
				button.setText("港币");
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = hongkongToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
			}
		}
		{
			Group group = new Group(shell, SWT.NONE);
			group.setText("兑换成人民币");
			group.setBounds(10, 177, 422, 79);
			{
				resultShow = new Label(group, SWT.NONE);
				resultShow.setAlignment(SWT.CENTER);
				resultShow.setBounds(10, 31, 402, 38);
				resultShow.setText("");
			}
		}

	}
	
	public double dollarToRMB(double money) {
		double rmb = money * 6.8290;
		return rmb;
	}
	
	public double europedollarToRMB(double money) {
		double rmb = money * 9.9659;
		return rmb;
	}
	
	public double japaneseToRMB(double money) {
		double rmb = money * 7.5747/100;
		return rmb;
	}
	
	public double englishToRMB(double money) {
		double rmb = money * 10.9090;
		return rmb;
	}
	
	public double hongkongToRMB(double money) {
		double rmb = money * 0.88114;
		return rmb;
	}
}

单选按钮的事件响应代码如下:

button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent arg0) {
						double money = Double.parseDouble(text.getText());
						double rmb = dollarToRMB(money);
						resultShow.setText(rmb + "");
					}
				});
  

先从text控件获取数值,然后调用兑率计算方法,再在label上显示结果。

随机文章推荐
网站分类


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

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


 

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

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