标签:
1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JButton; 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 8 public class Test_16 extends JFrame{ 9 10 public Test_16(){ 11 JP J = new JP(); 12 add(J); 13 } 14 15 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 Test_16 t1 = new Test_16(); 19 t1.setTitle("print"); 20 t1.setSize(200,200); 21 t1.setFocusable(true); 22 t1.setLocationRelativeTo(null); 23 t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24 t1.setVisible(true); 25 } 26 27 static class JP extends JPanel{ 28 JButton j1 = new JButton("Button1"); 29 JButton j2 = new JButton("Button2"); 30 public JP(){ 31 add(j1); 32 add(j2); 33 j1.addActionListener(new ActionListener(){ 34 public void actionPerformed(ActionEvent e){ 35 System.out.println("j1 is pressed"); 36 } 37 }); 38 j2.addActionListener(new ActionListener(){ 39 public void actionPerformed(ActionEvent e){ 40 System.out.println("j2 is pressed"); 41 } 42 }); 43 } 44 } 45 46 }
效果图:
标签:
原文地址:http://www.cnblogs.com/wanjiang/p/5635398.html