package rrr;import javax.swing.UIManager;
import java.awt.*;public class Application1
{
    private boolean packFrame = false;    //Construct the application
    public Application1()
    {
        Frame1 frame = new Frame1();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame)
        {
            frame.pack();
        }
        else
        {
            frame.validate();
        }
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height)
        {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width)
        {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }
    //Main method
    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        new Application1();
    }
}------------------------------------------------
package rrr;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Frame1 extends JFrame
{
    private JPanel contentPane;
    private BorderLayout borderLayout1 = new BorderLayout();
    private JButton jButton1 = new JButton();    //Construct the frame
    public Frame1()
    {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try
        {
            jbInit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    //Component initialization
    private void jbInit() throws Exception
    {
        //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                jButton1_actionPerformed(e);
            }
        });
        contentPane.setLayout(borderLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        contentPane.add(jButton1, BorderLayout.CENTER);
    }
    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e)
    {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING)
        {
            System.exit(0);
        }
    }    void jButton1_actionPerformed(ActionEvent e)
    {
        JDialog dialog=new JDialog(this);
        dialog.setModal(true);
        dialog.add(new JButton("haha"));
        dialog.setSize(400,300);
        dialog.show();
    }
}

解决方案 »

  1.   


    其实只要最后一个就行
        void jButton1_actionPerformed(ActionEvent e)
        {
            JDialog dialog=new JDialog(this);
            dialog.setModal(true);
            dialog.add(new JButton("haha"));
            dialog.setSize(400,300);
            dialog.show();
        }
      

  2.   

    同意楼上 alphazhao(绿色咖啡)
      

  3.   

    哈哈,我拷了整个工程给他.他应该感动的流泪了.其实核心在这里:jButton1.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    jButton1_actionPerformed(e);
                }
            });和
    void jButton1_actionPerformed(ActionEvent e)
        {
            JDialog dialog=new JDialog(this);
            dialog.setModal(true);
            dialog.add(new JButton("haha"));
            dialog.setSize(400,300);
            dialog.show();
        }