?? focuseventdemo.java
字號(hào):
//焦點(diǎn)事件演示程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FocusEventDemo extends JFrame implements FocusListener
{
JButton btnOk,btnCancel;
JTextField txtUser,txtPassword;
JPanel pnlMain;
JLabel lblUser,lblPassword,lblInfo;
public FocusEventDemo()
{
pnlMain=new JPanel();
pnlMain.setLayout(new GridLayout(4,2));
lblUser=new JLabel("用戶名:");
txtUser=new JTextField();
txtUser.addFocusListener(this);
lblPassword=new JLabel("密碼");
txtPassword=new JTextField();
txtPassword.addFocusListener(this);
btnOk=new JButton("確定");
btnOk.addFocusListener (this);
btnCancel=new JButton("取消");
btnCancel.addFocusListener (this);
lblInfo=new JLabel("");
lblInfo.setForeground(Color.RED);
setContentPane(pnlMain);
pnlMain.add(lblUser);
pnlMain.add(txtUser);
pnlMain.add(lblPassword);
pnlMain.add(txtPassword);
pnlMain.add(btnOk);
pnlMain.add(btnCancel);
pnlMain.add(lblInfo);
setSize(250,150);
setTitle("焦點(diǎn)事件演示");
setVisible(true);
}
public void focusGained(FocusEvent fe)
{
if (fe.getSource()==txtUser)
lblInfo.setText("請(qǐng)輸入6-16位用戶名");
if (fe.getSource()==txtPassword)
lblInfo.setText("請(qǐng)輸入6-16位密碼");
}
public void focusLost(FocusEvent fe)
{
if (fe.getSource()==btnOk)
lblInfo.setText("確定按鈕失去焦點(diǎn)!");
if (fe.getSource()==txtUser)
lblInfo.setText("");
if (fe.getSource()==txtPassword)
lblInfo.setText("");
}
public static void main(String []args)
{
FocusEventDemo fd=new FocusEventDemo();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -