你的位置:首页 > Java教程

[Java教程]Java按钮事件绑定


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");  }}}