Swing常用事件
1.Java Swing中处理事件的一般步骤是:
(1)新建一个组件(JButton)
(2)将该组件添加到面板中(JPanel)
(3)注册监听器以监听事件源产生的事件(如ActionListener来响应客户点击按钮事件)
(4)定义处理事件的方法(如在ActionLinstener中的actionPerform中定义响应的方法)
以上步骤我们可以用多种方法实现,用的最多的是第二种
第一种方法是只利用一个监听器以及多个if语句来决定是哪个组件产生的事件;
第二种方法是使用多个内部类来响应不同组件产生的各种事件,其具体实现又分两种方式,一种是匿名内部类,一种是一般内部类。
package jf;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Tets extends JFrame{
JButton btn;
JPanel panel;
JTextField text;
private static final String TIP = "QQ号码/手机/邮箱";
//初始化窗口
public void init(){
this.setTitle("Test");
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setResizable(true);
}
//构造方法
public Tets() {
init();
text = new JTextField("qq",20);
btn = new JButton("提交");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//点击弹框显示
JOptionPane.showMessageDialog(null, "提交失败");
System.out.println("hhhh");
}
});
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(text);
this.getContentPane().add(btn);
this.setVisible(true);
}
public static void main(String[] args) {
new Tets();
}
}
2.ActionListener(按钮点击事件)
在文本框里面显示隐式字符
package jf;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Hint extends JFrame implements MouseListener
{
private static final long serialVersionUID = 1L;
private static final String TIP = "QQ号码/手机/邮箱";
public Hint ()
{
setTitle ("test");
setLayout (new BorderLayout ());
final JTextField tf = new JTextField (TIP);
tf.setName ("tf");
tf.setForeground (Color.gray);
tf.setLocation (0, 0);
tf.setSize (100, 30);
tf.addMouseListener (this);
JPanel panel = new JPanel ();
panel.setName ("panel");
panel.addMouseListener (this);
panel.setLayout (null);
panel.add (tf);
panel.setFocusable (true);
add (panel);
setSize (300, 300);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
}
public static void main ( String[] args )
{
new Hint ();
}
public void mouseClicked ( MouseEvent e )
{
Component component = e.getComponent ();
String name = component.getName ();
if (e.getButton () == MouseEvent.BUTTON1)
{
if ("tf".equals (name))
{
JTextField tf = (JTextField) component;
if (TIP.equals (tf.getText ()))
{
tf.setText ("");
}
}
else if ("panel".equals (name))
{
JTextField tf = (JTextField) ( (JPanel) component ).getComponents ()[0];
if ("".equals (tf.getText ()))
{
tf.setText (TIP);
}
}
}
}
public void mousePressed ( MouseEvent e )
{}
public void mouseReleased ( MouseEvent e )
{}
public void mouseEntered ( MouseEvent e )
{}
public void mouseExited ( MouseEvent e )
{}
}
密码
JTextField textField = new JTextField("密码");
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (textField.getText().isEmpty()){
textField.setText("密码");
}
}
});