📄 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"); //默认BorderLayout布局
f.setSize(400,300);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
p1 = new Panel(); //创建面板
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
f.add(p1,"North"); //面板添加到框架的北部
t1 = new Label("Size");
tf1 = new TextField(6);
p1.add(t1); //标签添加到面板上
p1.add(tf1);
tf1.addActionListener(this); //注册文本行的事件监听程序
cb1 = new Checkbox("Bold"); //创建复选框
cb2 = new Checkbox("Italic");
p1.add(cb1);
p1.add(cb2);
cb1.addItemListener(this); //注册复选框的事件监听程序
cb2.addItemListener(this);
ta1=new TextArea("TextArea"); //创建文本区
f.add(ta1); //文本区添加到框架的中部
f.addWindowListener(this);
f.setVisible(true);
}
public void windowOpened(WindowEvent e)
{
ta1.setFont(new Font("Dialog",0,18)); //设置文本区的初始字体
Font font1=ta1.getFont(); //获得文本区的字体
ta1.append("\nFont: "+font1.toString());
tf1.setText(""+font1.getSize()); //获得文本区的字号
cb1.setState(font1.isBold()); //设置复选框为选择状态
cb2.setState(font1.isItalic());
ta1.append("\nStyle = "+font1.getStyle()); //获得文本区的字形
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{ //实现ActionListener接口中的方法,文本行中单击回车键<Enter>时触发
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)
{ //实现ItemListener接口中的方法,对复选框操作时触发
Font font1=ta1.getFont();
int style1 = font1.getStyle();
if ((e.getSource()==cb1) || (e.getSource()==cb2) )
{
if (e.getSource()==cb1) style1 = style1 ^ 1; //异或运算^
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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -