1. 程式人生 > >《Java程式設計》第16週週四:GUI程式設計及檔案對話方塊的使用 計算器

《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();  
	} 

}

執行結果


問題:背景不會弄,面板也不會佈局