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();
}
}
我想关闭第二个窗口~然后第一个主窗口会显示出来```可是编译都通过了```主窗口还是没有Show出来`
帮看下应该怎么改。
----------------解决方案--------------------------------------------------------
你只给了代码片段,让我们怎么看
----------------解决方案--------------------------------------------------------
是啊,谁知道你前面做过什么
----------------解决方案--------------------------------------------------------
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();
}
}
}
不好意思```这回发完了~~我以为看这里可以看出问题```
----------------解决方案--------------------------------------------------------
我这里能运行啊
你想实现什么功能
----------------解决方案--------------------------------------------------------
但是你的程序用了很多过时的方法
比如show(),hide()
应该用setVisible(true) setVisible(false)来代替
----------------解决方案--------------------------------------------------------
我是想在第二个窗口弹出来后~~按窗口右上角的关闭来关闭窗口~~使第一次主窗口跳回出来~~
可是我这里不跳啊~~晕了```还要按Ctrl+C才能结束进程~~~
----------------解决方案--------------------------------------------------------
加事件监听嘛
是不是你事件监听写错了
你的代码太多了,没有仔细看
你还是自己仔细检查一下吧
----------------解决方案--------------------------------------------------------
你那可以实现这个事件吗?~~
可是我的编译都通过了,没找出错误出来呢~~~
----------------解决方案--------------------------------------------------------
你没有为你点第二个窗口程序上的关闭加个事件啊
把关闭事件重写下..........
----------------解决方案--------------------------------------------------------