example7_32.java

来自「不错的教程 适合中高级人员的使用」· Java 代码 · 共 44 行

JAVA
44
字号
import java.awt.*;import java.awt.event.*;
class MyWindow extends Frame implements FocusListener
{  Button button;TextField text; 
     
   MyWindow(String s)
   {   super(s);
	   button=new Button("确定");
      text=new TextField(10);
	  text.requestFocusInWindow();
	  setLayout(new FlowLayout());
	  add(button);
	  add(text);
	  text.addFocusListener(this);
	  button.addFocusListener(this);
	  addWindowListener(new WindowAdapter()
	      {public void windowClosing(WindowEvent e)
			  {System.exit(0);}
	       }
	  );
	  setBounds(100,100,150,150);
      setVisible(true);
	  validate();
    }   
   public void focusGained(FocusEvent e)
	{
      Component com=(Component) e.getSource();
	  com.setBackground(Color.blue);
	  if(com==text)
		  text.setText(null);	  
   }
   public void focusLost(FocusEvent e)
	{
      Component com=(Component) e.getSource();
	  com.setBackground(Color.red);
	}

}
 
public class Example7_32
{  public static void main(String args[])
   { new MyWindow("窗口");
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?