代码如下:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class DialogTest
{
protected Mainframe mainframe;
public DialogTest()
{
mainframe=new Mainframe("Test");
}
public class Mainframe extends JFrame
{
protected Subdialog sub;
protected JButton ok=new JButton("OK");
public Mainframe(String title)
{
super(title);
setLayout(new FlowLayout());
setSize(200,200);
add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sub=new Subdialog(mainframe,"dialog",true);
}
}
);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class Subdialog extends JDialog
{
JButton dis=new JButton("dispose");
public Subdialog(Mainframe main,String title,boolean mode)
{
super(main,title,mode);
setSize(60,60);
add(dis);
setVisible(true);
dis.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sub.dispose();
}
}
);
}
}
}
public static void main(String[] args)
{
DialogTest app=new DialogTest();
}
}
如果是子窗口是Frame的话没有问题,如果是JDialog的话要怎么办?谢谢!
import java.awt.*;
import java.awt.event.*;public class DialogTest
{
protected Mainframe mainframe;
public DialogTest()
{
mainframe=new Mainframe("Test");
}
public class Mainframe extends JFrame
{
protected Subdialog sub;
protected JButton ok=new JButton("OK");
public Mainframe(String title)
{
super(title);
setLayout(new FlowLayout());
setSize(200,200);
add(ok);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sub=new Subdialog(mainframe,"dialog",true);
}
}
);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class Subdialog extends JDialog
{
JButton dis=new JButton("dispose");
public Subdialog(Mainframe main,String title,boolean mode)
{
super(main,title,mode);
setSize(60,60);
add(dis);
setVisible(true);
dis.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sub.dispose();
}
}
);
}
}
}
public static void main(String[] args)
{
DialogTest app=new DialogTest();
}
}
如果是子窗口是Frame的话没有问题,如果是JDialog的话要怎么办?谢谢!
{
public void actionPerformed(ActionEvent e)
{
sub.dispose();
}
}
);
setVisible(true);
因为你的JDialog是模态窗体,所以调用了setVisible(true)后,
后面的dis.addActionListener并没有执行,所以要放到setVisible前面
E:\MyDoc\JAVA>java DialogTest
Exception occurred during event dispatching:
java.lang.NullPointerException
at DialogTest$Mainframe$Subdialog$1.actionPerformed(DialogTest.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.Dialog$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at DialogTest$Mainframe$Subdialog.<init>(DialogTest.java:53)
at DialogTest$Mainframe$1.actionPerformed(DialogTest.java:28)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)