e884. listening for ok and cancel events in a jcolorchooser dialog.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 48 行

TXT
48
字号
This example defines an action that creates and shows a color chooser dialog. 
    // Create the chooser
    final JColorChooser chooser = new JColorChooser();
    
    // Define listener for ok events
    ActionListener okListener = new ActionListener() {
        // Called when user clicks ok
        public void actionPerformed(ActionEvent evt) {
            // Note: The source is an internal button in the dialog
            // and should not be used
    
            // Get selected color
            Color newColor = chooser.getColor();
        }
    };
    
    // Define listener for cancel events
    ActionListener cancelListener = new ActionListener() {
        // Called when user clicks cancel
        public void actionPerformed(ActionEvent evt) {
            // Note: The source is an internal button in the dialog
            // and should not be used
    
            // Note: The original color is not restored.
            // getColor() returns the latest selected color.
            Color newColor = chooser.getColor();
        }
    };
    
    // Choose whether dialog is modal or modeless
    boolean modal = false;
    
    // Create the dialog that contains the chooser
    dialog = JColorChooser.createDialog(null, "Dialog Title", modal,
        chooser, okListener, cancelListener);
    
    // Add listener for clicks to the close-window icon
    dialog.addWindowListener(new WindowAdapter() {
        // Called when user clicks the close-window icon.
        // This type of event is usually treated like a cancel.
        public void windowClosing(WindowEvent evt) {
            // Note: The original color is not restored.
            // getColor() returns the latest selected color.
            Color newColor = chooser.getColor();
        }
    }) ;

⌨️ 快捷键说明

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