Java按钮事件绑定

12/12/2015来源:Java教程人气:1658

package PResent;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main  {
    JButton lable;
    JButton font;
    public static void main(String[] args) {
        new Main().go();
    }

    public void go() {
        JFrame frame = new JFrame();
        lable = new JButton("click lable");
        lable.addActionListener(new LableButton());
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(BorderLayout.EAST,lable);
        font = new JButton("click font");
        font.addActionListener(new FontButton());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(BorderLayout.WEST,font);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

class LableButton implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        lable.setText("lable");
    }
}
class FontButton implements ActionListener{
    
    @Override
    public void actionPerformed(ActionEvent e) {
        font.setText("font");
    }
}

}