import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
mf.setSize(300, 400);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
mf.setLocation(d.width / 2, 0);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.setVisible(true);
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
total = 0;
d = Toolkit.getDefaultToolkit().getScreenSize();
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
closeAll = new JButton("CloseAll");
newJFrame = new JButton("New Frame");
newJFrame.addActionListener(new ActionListener()
{
NewFrame f;
public void actionPerformed(ActionEvent e)
{
f = new NewFrame(d.width / (total + 100), d.width
/ (total + 100));
TotalnewFrame.add(f);
total++;
}
});
closeAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for (NewFrame f : TotalnewFrame)
{
f.dispose();
total--;
}
}
});
panel.add(newJFrame);
panel.add(closeAll);
this.add(panel);
}
private static final long serialVersionUID = 1L;
private ArrayList<NewFrame> TotalnewFrame = new ArrayList<NewFrame>();
private Dimension d;
private JButton closeAll;
private JButton newJFrame;
private int total;
private JPanel panel;
}
class NewFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private Point p;
public NewFrame(int i, int j)
{
cx += dx;
cy += dy;
this.setTitle("this is the new JFrame");
p = new Point(cx, cy);
this.setLocation(p);
this.setSize(200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private final static int dx = 20;
private final static int dy = 20;
private static int cx = 0;
private static int cy = 0;
}