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 |
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.SwingUtilities; public class SwingHello extends JFrame implements ActionListener { private JPanel contentPane; private JButton helloButton; private int count; public SwingHello(){ this.count = 0; this.setTitle("Hello World"); this.setSize(300,300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initControls(); this.setVisible(true); } public void initControls(){ contentPane = new JPanel(); helloButton = new JButton("Hello"); helloButton.addActionListener(this); contentPane.add(helloButton); this.add(contentPane); } @Override public void actionPerformed(ActionEvent e){ helloButton.setText("Clicked " + ++count); } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { @Override public void run(){ new SwingHello(); } }); } } |