java swing 关闭弹出自定义窗口

containMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { containDialog = new JDialog(); containDialog.setLayout(new GridLayout(4,4,20,20)); JButton button1 = new JButton(); button1.setIcon(new ImageIcon(getImagePath("images/menu/2001.jpg"))); JButton button2 = new JButton(); button2.setIcon(new ImageIcon(getImagePath("images/menu/2002.jpg"))); JButton button3 = new JButton(); button3.setIcon(new ImageIcon(getImagePath("images/menu/2001.jpg"))); JButton button4 = new JButton(); button4.setIcon(new ImageIcon(getImagePath("images/menu/2002.jpg"))); JButton button5 = new JButton(); button5.setIcon(new ImageIcon(getImagePath("images/menu/2001.jpg"))); JButton button6 = new JButton(); button6.setIcon(new ImageIcon(getImagePath("images/menu/2002.jpg"))); JButton button7 = new JButton(); button7.setIcon(new ImageIcon(getImagePath("images/menu/2001.jpg"))); JButton button8 = new JButton(); button8.setIcon(new ImageIcon(getImagePath("images/menu/2002.jpg"))); JButton button9 = new JButton(); button9.setIcon(new ImageIcon(getImagePath("images/menu/2001.jpg"))); containDialog.add(button1); containDialog.add(button2); containDialog.add(button3); containDialog.add(button4); containDialog.add(button5); containDialog.add(button6); containDialog.add(button7); containDialog.add(button8); containDialog.add(button9); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { MtiRecColumn mrc = new MtiRecColumn(); mrc.setSize(100, 100); mcc.addObject(mrc); mcc.repaint(); containDialog.dispose(); } }); containDialog.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { containDialog.dispose(); } }); containDialog.pack();//必须加这一句,否则面板初始尺寸为0 containDialog.setVisible(true); } });

 

 

前面定义public JDialog containDialog = null; 

 

 关键:containDialog.dispose();   

你可能感兴趣的:(java swing 关闭弹出自定义窗口)