星期五, 三月 25, 2011

如何删除JDialog右上角的X 关闭按钮

<code>
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing
{
public void buildGUI()
{
JDialog.setDefaultLookAndFeelDecorated(true);

JDialog d = new MyDialog();
d.setModal(true);
d.getRootPane().setDefaultButton(null);


JPanel p = new JPanel(new GridBagLayout());

JButton btn = new JButton("Exit");

p.add(btn,new GridBagConstraints());

d.getContentPane().add(p);

d.setSize(400,300);

d.setLocationRelativeTo(null);

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

System.exit(0);

}

});

d.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable()
{
public void run(){
new Testing().buildGUI();
}
});
}
}
class MyDialog extends JDialog
{
public MyDialog()
{
System.out.println("....");
removeX(this);
}
public void removeX(Component comp)
{
if (comp instanceof JButton)
{
System.out.println("..."+comp);
Icon i=((JButton)comp).getIcon();
System.out.println("..2."+i.getClass());

//System.out.println("...2"+(AbstractButton)comp.getDefaultIcon());
comp.getParent().remove(comp);
}

if (comp instanceof Container)
{
Component[] comps = ((Container)comp).getComponents();
for(int x = 0, y = comps.length; x < y; x++)
{
removeX(comps[x]);
}
}
}
}
</code>