史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   字體設定程式 (http://forum.slime.com.tw/thread168522.html)

psac 2006-02-18 05:23 AM

字體設定程式
 
字體設定

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class java1 extends Applet implements ItemListener
{
TextField text=new TextField("實例文字",20);
CheckboxGroup fontStyle=new CheckboxGroup();
Checkbox plainstyle=new Checkbox("Plain",true,fontStyle);
Checkbox italicstyle=new Checkbox("Italic",false,fontStyle);
Checkbox boldstyle=new Checkbox("Bold",false,fontStyle);
Choice fontType=new Choice();
Font font;

public void init() //啟始化,把基本元件加入Applet中
{ add(text);

plainstyle.addItemListener(this);
add(plainstyle);

italicstyle.addItemListener(this);
add(italicstyle);

boldstyle.addItemListener(this);
add(boldstyle);

fontType.add("TimesRoman"); fontType.add("Courier");fontType.add("Helvetica");
fontType.addItemListener(this);
add(fontType);
}


public void itemStateChanged(ItemEvent e) //ItemListener的方法,對選項事件作回應
{
String fontname[]={"TimesRoman","Courier","Helvetica"};
String stylename[] ={"PLAIN","ITALIC","BOLD"};
int fontstyle[]={Font.PLAIN,Font.ITALIC,Font.BOLD},ch=0;
ch=fontType.getSelectedIndex();

ItemSelectable is=e.getItemSelectable();
if(is==plainstyle) {text.setText(fontname[ch]); font= new Font(fontname[ch],fontstyle[0],12);}
else if(is==italicstyle) {text.setText(fontname[ch]); font= new Font(fontname[ch],fontstyle[1],12);}
else if(is==boldstyle) {text.setText(fontname[ch]); font= new Font(fontname[ch],fontstyle[2],12);}

repaint();
}


public void paint (Graphics g)
{

text.setFont(font);
g.drawString("此為一個字體設定程序",130,80);

}

}


所有時間均為台北時間。現在的時間是 11:08 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1