小弟写JAVA的一个小程序遇到了问题,求解。构造一个窗口,然后关闭这个窗口时,弹出一个提示的窗口。
importjava.awt.*;importjavax.swing.*;importjava.util.*;importjava.awt.event.*;publicc...
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Frame
{
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("OK");
//这里想弹一个窗口出来
messageFrame message = new messageFrame();
message.setVisible(true);
}
});
}
}
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel buttonPanel;
}
class messageFrame extends JFrame
{
public messageFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myText text = new myText();
add(text);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
我编译运行之后,第一个窗口显示了出来,但我一点关闭,第二个窗口好像闪了一下,就没了,怎么搞得?求解! 展开
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Frame
{
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("OK");
//这里想弹一个窗口出来
messageFrame message = new messageFrame();
message.setVisible(true);
}
});
}
}
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel buttonPanel;
}
class messageFrame extends JFrame
{
public messageFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myText text = new myText();
add(text);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
我编译运行之后,第一个窗口显示了出来,但我一点关闭,第二个窗口好像闪了一下,就没了,怎么搞得?求解! 展开
展开全部
这是你的源代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Frame
{
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("OK");
//这里想弹一个窗口出来
messageFrame message = new messageFrame();
message.setVisible(true);
}
});
}
}
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //注意你的这句错了,设置了默认的点击关闭按钮的时候执行关闭,应该去掉,你的代码比较多,我修改过了。
}
private JPanel buttonPanel;
}
class messageFrame extends JFrame
{
public messageFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myText text = new myText();
add(text);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
这是我修改后的代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
}
messageFrame message = new messageFrame(); //这里想弹一个窗口出来
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
new messageFrame().showFrame();
}
});
}
}
class messageFrame extends JFrame
{
public void showFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
myText text = new myText();
add(text);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Frame
{
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("OK");
//这里想弹一个窗口出来
messageFrame message = new messageFrame();
message.setVisible(true);
}
});
}
}
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //注意你的这句错了,设置了默认的点击关闭按钮的时候执行关闭,应该去掉,你的代码比较多,我修改过了。
}
private JPanel buttonPanel;
}
class messageFrame extends JFrame
{
public messageFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myText text = new myText();
add(text);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
这是我修改后的代码:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class myFrame extends JFrame
{
public myFrame()
{
setSize(400, 200);
setLocationByPlatform(true);
setTitle("want to close?");
}
messageFrame message = new messageFrame(); //这里想弹一个窗口出来
public static void main(String[] args)
{
myFrame frame = new myFrame();
frame.setVisible(true);
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
new messageFrame().showFrame();
}
});
}
}
class messageFrame extends JFrame
{
public void showFrame()
{
setSize(500, 500);
setLocationByPlatform(true);
setTitle("OK");
myText text = new myText();
add(text);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class myText extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawString("windowClosed", 100, 100);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询