问题解决了一半!
----------
if (dialog==null)
dialog=new DialogPanel();
else if(!dialog.isVisible())
{
dialog.setVisible(true);
}
else if (dialog.isVisible())
{
dialog.setVisible(false);
}----------------------------------
可是还是不能定位到父面板上面,,,
调用 super()出错,why?
----------
if (dialog==null)
dialog=new DialogPanel();
else if(!dialog.isVisible())
{
dialog.setVisible(true);
}
else if (dialog.isVisible())
{
dialog.setVisible(false);
}----------------------------------
可是还是不能定位到父面板上面,,,
调用 super()出错,why?
请帮我添一下,再setLocation()一下,谢谢!!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;class MySecondProgram extends JFrame {
public MySecondProgram() {
setTitle("mysecond");
setSize(400,300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane=getContentPane();
JPanel northpanel=new JPanel();
JButton button=new JButton("out");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Container c = dialog.getParent();
Point p = c.getLocation();
Dimension d = c.getSize();
dialog.setLocation(p.x + (d.width - dialog.getWidth())/2, p.y + (d.height - dialog.getHeight())/2);
dialog.show();
}
});
northpanel.add(button);
contentPane.add(northpanel ,BorderLayout.NORTH);
}
class DialogPanel extends JDialog implements ActionListener {
public DialogPanel(Frame f) {
super(f,true);
setSize(200,150);
}
public void actionPerformed(ActionEvent event) {
}
}
public static void main(String[] args)
{
JFrame f = new MySecondProgram();
f.setSize(640, 480);
f.show();
}
private JButton button;
private DialogPanel dialog = new DialogPanel(this);
}