关于dispose
为什么 我先dispose当前窗口再new一个新窗口 原窗口没有关掉但是先new新窗口在dispose当前窗口 窗口就关掉了
import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class FrameT extends JFrame{ private JButton button; public FrameT(){ super(""); button = new JButton("按钮"); Container container = getContentPane(); container.setLayout(new FlowLayout()); container.add(button); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ dispose(); JFrame frame = new JFrame(); frame.setSize(400,400); frame.getContentPane().setBackground(Color.RED); frame.setVisible(true); } } ); setSize(200,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new FrameT(); } }