?? text1.java
字號:
import java.awt.*;
import java.awt.event.*;
public class Text1 extends WindowAdapter
implements ActionListener,ItemListener
{
Frame f;
Label t1,t2;
TextField tf1,tf2;
TextArea ta1;
Checkbox cb1,cb2;
Panel p1 ;
public void display()
{
f = new Frame("Edit"); //默認(rèn)BorderLayout布局
f.setSize(400,300);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
p1 = new Panel(); //創(chuàng)建面板
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
f.add(p1,"North"); //面板添加到框架的北部
t1 = new Label("Size");
tf1 = new TextField(6);
p1.add(t1); //標(biāo)簽添加到面板上
p1.add(tf1);
tf1.addActionListener(this); //注冊文本行的事件監(jiān)聽程序
cb1 = new Checkbox("Bold"); //創(chuàng)建復(fù)選框
cb2 = new Checkbox("Italic");
p1.add(cb1);
p1.add(cb2);
cb1.addItemListener(this); //注冊復(fù)選框的事件監(jiān)聽程序
cb2.addItemListener(this);
ta1=new TextArea("TextArea"); //創(chuàng)建文本區(qū)
f.add(ta1); //文本區(qū)添加到框架的中部
f.addWindowListener(this);
f.setVisible(true);
}
public void windowOpened(WindowEvent e)
{
ta1.setFont(new Font("Dialog",0,18)); //設(shè)置文本區(qū)的初始字體
Font font1=ta1.getFont(); //獲得文本區(qū)的字體
ta1.append("\nFont: "+font1.toString());
tf1.setText(""+font1.getSize()); //獲得文本區(qū)的字號
cb1.setState(font1.isBold()); //設(shè)置復(fù)選框?yàn)檫x擇狀態(tài)
cb2.setState(font1.isItalic());
ta1.append("\nStyle = "+font1.getStyle()); //獲得文本區(qū)的字形
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{ //實(shí)現(xiàn)ActionListener接口中的方法,文本行中單擊回車鍵<Enter>時(shí)觸發(fā)
Font font1=ta1.getFont();
int size1 = (new Integer(tf1.getText())).intValue();
ta1.setFont(new Font(font1.getName(),font1.getStyle(),size1));
}
public void itemStateChanged(ItemEvent e)
{ //實(shí)現(xiàn)ItemListener接口中的方法,對復(fù)選框操作時(shí)觸發(fā)
Font font1=ta1.getFont();
int style1 = font1.getStyle();
if ((e.getSource()==cb1) || (e.getSource()==cb2) )
{
if (e.getSource()==cb1) style1 = style1 ^ 1; //異或運(yùn)算^
if (e.getSource()==cb2) style1 = style1 ^ 2;
ta1.setFont(new Font(font1.getName(),style1,font1.getSize()));
ta1.append("\nstyle= "+style1+" "+e.getItem()+" = ");
ta1.append(""+((Checkbox)e.getSource()).getState());
}
}
public static void main(String arg[])
{
(new Text1()).display();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -