⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 closewindowdemo.java

📁 此文件能系统的介绍Java初级知识
💻 JAVA
字号:
package swing.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


/**
 Demonstration of programming the close-window button.
*/
public class CloseWindowDemo extends JFrame
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    public static void main(String[] args)
    {
        CloseWindowDemo gui = new CloseWindowDemo( );
        gui.setVisible(true);
    }

    public CloseWindowDemo( )
    {
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(
                WindowConstants.DO_NOTHING_ON_CLOSE);//设置关闭窗口时的动作
        addWindowListener(new InnerDestroyer( ));
        setTitle("Close Window Demo"); 
        Container contentPane = getContentPane( );
        contentPane.setLayout(new BorderLayout( ));

        JLabel message = new JLabel(
                         "Please don't click that button.");
        contentPane.add(message, BorderLayout.CENTER);
    }

    //Displays a window that checks if the user wants to exit.
    private class InnerDestroyer extends WindowAdapter
    {
        public void windowClosing(WindowEvent e) 
        {
            ConfirmWindow askWindow = new ConfirmWindow( );
            askWindow.setVisible(true);
        }
    }
    
    //Designed to be used with the inner class InnerDestroyer.
    //Checks if the user wants to exit.
    private class ConfirmWindow extends JFrame
                                implements ActionListener
    {
        public static final int WIDTH = 200;
        public static final int HEIGHT = 100;

        public ConfirmWindow( )
        {
            setSize(WIDTH, HEIGHT);
            Container confirmContent = getContentPane( );
            confirmContent.setBackground(Color.WHITE);
            confirmContent.setLayout(new BorderLayout( ));

            JLabel msgLabel = new JLabel(
                           "Are you sure you want to exit?");
            confirmContent.add(msgLabel, BorderLayout.CENTER);

            JPanel buttonPanel = new JPanel( );
            buttonPanel.setLayout(new FlowLayout( ));

            JButton exitButton = new JButton("Yes");
            exitButton.addActionListener(this);
            buttonPanel.add(exitButton);

            JButton cancelButton = new JButton("No");
            cancelButton.addActionListener(this);
            buttonPanel.add(cancelButton);

            confirmContent.add(buttonPanel, BorderLayout.SOUTH);
        }

        public void actionPerformed(ActionEvent e) 
        {
            if (e.getActionCommand( ).equals("Yes")) 
                System.exit(0);
            else if (e.getActionCommand( ).equals("No"))
                dispose( );//Destroys only the ConfirmWindow.
            else
                System.out.println("Error in Confirm Window.");
        }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -