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

项目三:实验六 图形用户界面 (教材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编程及文件对话框的使用 计算器_第1张图片


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

你可能感兴趣的:(《Java程序设计》第16周周四:GUI编程及文件对话框的使用 计算器)