码迷,mamicode.com
首页 > 编程语言 > 详细

《Java程序设计》第16周周四:GUI编程及文件对话框的使用 计算器

时间:2015-06-30 10:39:58      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

项目三:实验六 图形用户界面(教材P279-280)

目标:完成实验六的内容。

运行代码

import java.awt.Frame;
import java.awt.Button;
import java.awt.Label; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Dimension; 
import java.awt.Font;
import java.awt.Color;
import java.lang.Exception; 
public class ChatServer  implements ActionListener
{  
	private Frame  Fm  = new Frame();
	private Button quit = new Button("退出"); 
	private Button Add = new Button("+"); 
	private Button Sub = new Button("-"); 
	private Button Mul = new Button("×"); 
	private Button Div = new Button("÷");  
	private Label  Addend = new Label("操作数1:"); 
	private Label  Augend = new Label("操作数2:");
	private Label  Sum      = new Label("结果:"); 
	private TextField fieldAdd = new TextField(); 
	private TextField fieldAug = new TextField(); 
	private TextArea areaSum = new TextArea(); 
	private Font Ft = new Font("宋体",Font.BOLD,18);
	public ChatServer()
	{
		Fm.setTitle("TYH号计算器"); 
		Fm.setBounds(200,100,400,300); 
		Fm.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
		Fm.add(Addend);//标签--操作数1   
		Addend.setFont(Ft);//字体类型 
		Addend.setForeground(Color.black);//设置字体颜色   
		Addend.setPreferredSize(new Dimension(100,30));//设置组件尺寸   
		Fm.add(fieldAdd);//输入区--操作数1  
		fieldAdd.setPreferredSize(new Dimension(100,30));//设置组件尺寸 
		fieldAdd.setFont(Ft);//字体类型 
		fieldAdd.setForeground(Color.black);  
		Fm.add(Add);   
		Add.addActionListener(this);

		Fm.add(Sub); 
		Sub.addActionListener(this);
		Fm.add(Augend);//标签--操作数2   
		Augend.setFont(Ft);   
		Augend.setForeground(Color.black);  
		Augend.setPreferredSize(new Dimension(100,30));//设置组件尺寸   
		Fm.add(fieldAug);//输入区--操作数2   
		fieldAug.setPreferredSize(new Dimension(100,30));//设置组件尺寸  
		fieldAug.setFont(Ft);   
		fieldAug.setForeground(Color.black);  
		Fm.add(Mul);   
		Mul.addActionListener(this);    
		Fm.add(Div);   
		Div.addActionListener(this);
		Fm.add(Sum);//标签--结果   
		Sum.setFont(Ft);   
		Sum.setForeground(Color.black);   
		Sum.setPreferredSize(new Dimension(100,30));//设置组件尺寸   
		Fm.add(areaSum);   
		areaSum.setPreferredSize(new Dimension(100,30));//设置组件尺寸   
		areaSum.setFont(Ft);   
		areaSum.setForeground(Color.red); 
		Fm.add(quit);  
		quit.addActionListener(this);   
		Fm.setVisible(true);  
	}
	public void actionPerformed(ActionEvent e) 
	{  
		try  
		{    
			if(e.getSource()==quit)    
				System.exit(0);    
			else if(e.getSource()==Add)    
				areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText()) + Double.parseDouble(fieldAug.getText())));   
			else if(e.getSource()==Sub)

				areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText()) -  Double.parseDouble(fieldAug.getText())));   
			else if(e.getSource()==Mul)    
				areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText()) *  Double.parseDouble(fieldAug.getText())));   
			else if(e.getSource()==Div)    
				areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText()) /  Double.parseDouble(fieldAug.getText())));  
		}  
		catch(Exception Ex)  
		{    
			areaSum.setText("出错了:"+Ex);  
		}  
	} 
	public static void main(String args[]) 
	{  
		ChatServer call = new ChatServer();  
	} 

}

运行结果

技术分享


问题:背景不会弄,面板也不会布局

版权声明:本文为博主原创文章,未经博主允许不得转载。

《Java程序设计》第16周周四:GUI编程及文件对话框的使用 计算器

标签:

原文地址:http://blog.csdn.net/u013910357/article/details/46688165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!