1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.EventHandler; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities;
public class ClickMe extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private JButton northBtn; private JButton westBtn; private JButton eastBtn; private JButton southBtn; private JButton centerBtn; private static int hit = 0;
public ClickMe() { super(new BorderLayout()); northBtn = new JButton("点我点我"); northBtn.setPreferredSize(new Dimension(200, 50)); add(northBtn, BorderLayout.NORTH); northBtn.addActionListener(this); westBtn = new JButton("点我点我"); westBtn.setPreferredSize(new Dimension(200, 50)); add(westBtn, BorderLayout.WEST); westBtn.addActionListener(this);
eastBtn = new JButton("点我点我"); eastBtn.setPreferredSize(new Dimension(200, 50)); add(eastBtn, BorderLayout.EAST); eastBtn.addActionListener(EventHandler.create(ActionListener.class, this, "ClickBtnAction"));
southBtn = new JButton("点我点我"); southBtn.setPreferredSize(new Dimension(200, 50)); add(southBtn, BorderLayout.SOUTH); southBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { southBtn.setText("方法 3 点击:" + (++ClickMe.hit)); } });
centerBtn = new JButton("点我点我"); centerBtn.setPreferredSize(new Dimension(200, 50)); add(centerBtn, BorderLayout.CENTER); centerBtn.addActionListener(new ClickBtnActionListener("方法 4 点击:")); }
public void ClickBtnAction() { eastBtn.setText("方法 2 点击:" + (++ClickMe.hit)); }
private class ClickBtnActionListener implements ActionListener { private String prefix; public ClickBtnActionListener(String prefix) { this.prefix = prefix; } @Override public void actionPerformed(ActionEvent e) { centerBtn.setText(prefix + (++ClickMe.hit)); } }
public void actionPerformed(ActionEvent e) { JButton btn = (JButton) e.getSource(); btn.setText("方法 1 点击:" + (++ClickMe.hit)); }
private static void createAndShowGui() { JFrame frame = new JFrame("Click Me"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ClickMe clickMe = new ClickMe(); frame.setContentPane(clickMe); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); }
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
|