日期:2014-05-20 浏览次数:20709 次
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Singleton extends JFrame { int a = 1; private JTextField titleField; private static Singleton singleton=null; private JButton jb; private Singleton() { JPanel p = new JPanel(); p.setLayout(new GridLayout(3,1)); p.add(new JLabel("Book title:")); this.titleField = new JTextField(15); this.jb = new JButton(); p.add(this.titleField); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { a++; String s = new String(); Integer i = new Integer(a); titleField.setText(i.toString()); } catch (Exception e) { } } }); p.add(jb); Integer i = new Integer(a); titleField.setText(i.toString()); this.add(p); } public static Singleton instance() { if(singleton==null) singleton = new Singleton(); return singleton; } public static void main(String[] args) { instance().show(); } }