JavaSE series code 46: simple event handler (event processing has been added)

The idea of interface is that it can add many functions that need to be implemented by many classes. Using the same interface class does not necessarily have an inheritance relationship, just like a variety of goods, they may belong to different companies. The industrial and commercial department requires that they all have the function of displaying trademarks (to realize the same interface), but the specific production of trademarks is implemented by each company.

import java.awt.*; 
import java.awt.event.*;
public class Javase_46 extends Frame implements ActionListener
{
  static app13_1 frm=new app13_1();
  static Button bt=new Button("设置字体颜色");
  static TextArea ta =new TextArea ("字体颜色",5,20);
  public static void main(String[] args)
  {
    bt.addActionListener(frm);   //把监听者frm向事件源bt注册
    frm.setTitle("操作事件");
    frm.setLayout(new FlowLayout());
    frm.setSize(260,170);
    frm.add(ta);
    frm.add(bt);
    frm.setVisible(true);
  }
  public void actionPerformed(ActionEvent e)  //事件发生时的处理操作
  {
    ta.setForeground(Color.red);    //把文本区内的文字设置为红色
  }
}
Published 52 original articles · Like 162 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/blog_programb/article/details/105394621