• java ColorChooser


    简介

    java 核心编程
    创建了三个chooser。一个是模态,一个是非模态,一个是非模态实时。
    放入别的frame 进行了展示

    image

    code

    /*
     * @Author: your name
     * @Date: 2020-11-08 15:59:26
     * @LastEditTime: 2020-11-08 16:15:56
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/colorChooser/ColorChooserPanel.java
     */
    package optionDialog;
    
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class ColorChooserPanel extends JPanel {
        
        public ColorChooserPanel() {
            JButton modalButton = new JButton("Modal");
            modalButton.addActionListener(new ModalListener());
            add(modalButton);
    
            JButton modelessButton = new JButton("Modeless");
            modelessButton.addActionListener(new ModelessListener());
            add(modelessButton);
    
            JButton immediateButton = new JButton("Immediate");
            immediateButton.addActionListener(new ImmediateListener());
            add(immediateButton);
        }
    
        private class ModalListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                Color defaultColor = getBackground();
                Color selected = JColorChooser.showDialog(ColorChooserPanel.this, "Set background", defaultColor);
                if (selected != null) {
                    setBackground(selected);
                }
            }
        }
    
        private class ModelessListener implements ActionListener {
            private JDialog dialog;
            private JColorChooser chooser;
    
            public ModelessListener() {
                chooser = new JColorChooser();
                dialog = JColorChooser.createDialog(ColorChooserPanel.this, "Background Color", false, chooser,
                        event -> setBackground(chooser.getColor()), null);
            }
    
            public void actionPerformed(ActionEvent event) {
                chooser.setColor(getBackground());
                dialog.setVisible(true);
    
            }
        }
    
        private class ImmediateListener implements ActionListener {
            private JDialog dialog;
            private JColorChooser chooser;
    
            public ImmediateListener() {
                chooser = new JColorChooser();
                chooser.getSelectionModel().addChangeListener(event -> setBackground(chooser.getColor()));
    
                dialog = new JDialog((Frame) null, false);
                dialog.add(chooser);
                dialog.pack();
            }
    
            public void actionPerformed(ActionEvent event) {
                chooser.setColor(getBackground());
                dialog.setVisible(true);
            }
        }
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    为什么要把js代码写到<!--//-->中
    js逻辑与,或,非
    你不知道的JavaScript——第二章:this全面解析
    你不知道的JavaScript——this词法
    如何避免用户多次点击造成的多次请求
    通用模块设计UMD
    你不知道的JavaScript——第一章:作用域是什么?
    github 操作
    数组中元素累加 reduce
    流星雨
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13944780.html
Copyright © 2020-2023  润新知