62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
public static void main(String[] args) throws Exception{
JFrame f = instance();
f.pack();
f.setVisible(true);
Thread.sleep(2000);
JFrame f2= instance();
f2.setVisible(false);
f2.setTitle("JFrame 2");
f2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f2.setVisible(true);
}