import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Image;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class StudentFrame
{
public static void main(String[] args)
{
OpenFrame frame = new OpenFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class OpenFrame extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
public OpenFrame()
{
Container con = getContentPane();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
int x = (width - WIDTH)/2;
int y = (height - HEIGHT)/2;
setBounds(x, y, WIDTH, HEIGHT);
Image image = kit.getImage("fuck.gif");
setIconImage(image);
setTitle("主窗口");
setResizable(false);
OpenButtonPanel panel = new OpenButtonPanel(this);
con.add(panel);
}
}
class OpenButtonPanel extends JPanel
{
private JButton makeNewFrameButton;
private JFrame parFrame;
public OpenButtonPanel(JFrame frame)
{
parFrame = frame;
makeNewFrameButton = new JButton("Open New Frame");
add(makeNewFrameButton);
makeNewFrameButton.addActionListener(new MakeFrameAction());
}
private class MakeFrameAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFrame subFrame = new CloseFrame(parFrame);
subFrame.show();
subFrame.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent event)
{
if(!parFrame.isShowing());
parFrame.show();
}
});
parFrame.hide();
}
}
}
class CloseFrame extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JFrame parFrame;
public CloseFrame(JFrame frame)
{
parFrame = frame;
Container con = getContentPane();
setSize(WIDTH, HEIGHT);
setTitle("第二个窗口");
setResizable(false);
CloseButtonPanel panel = new CloseButtonPanel(this);
con.add(panel);
}
class CloseButtonPanel extends JPanel
{
public CloseButtonPanel(JFrame subFrame)
{
JButton closeNewFrameButton = new JButton("Close New Button");
add(closeNewFrameButton);
closeNewFrameButton.addActionListener(new CloseNewFrameAction(subFrame));
}
}
class CloseNewFrameAction implements ActionListener
{
private JFrame subFrame;
public CloseNewFrameAction(JFrame subFrame)
{
this.subFrame = subFrame;
}
public void actionPerformed(ActionEvent e)
{
subFrame.hide();
parFrame.show();
}
}
}
不好意思```这回发完了~~我以为看这里可以看出问题```