在设计JButton时不知道怎么应用动作监听器
//代码正文
package nuc.sw.animal;
import javax.swing.*;
import nuc.sw.animal2.Pet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PetJFrame extends JFrame{
private JButton b_cat, b_dog, b_parrot;
private JTextField t_cat, t_dog, t_parrot;
//窗体初始化
public PetJFrame() {
this.setTitle("动物模拟发声器一代");
this.setSize(400, 450);
this.setLocation(200, 300);
init();
this.setVisible(true);
}
public void init() {
this.setLayout(new GridLayout(3, 2, 5, 5));
b_dog = new JButton("修勾");
b_dog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
b_cat = new JButton("修猫");
b_cat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
b_parrot = new JButton("鹦鹉");
b_parrot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
t_cat = new JTextField();
t_dog = new JTextField();
t_parrot = new JTextField();
this.add(b_cat);
this.add(t_cat);
this.add(b_dog);
this.add(t_dog);
this.add(b_parrot);
this.add(t_parrot);
}
}
监听按钮点击事件,然后set到textField就好