zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    简单poi创建execl
    Orcale 存储过程实践总结
    PLSQL 创建自定义函数注意事项
    字符串算法模板整理
    多项式FFT/NTT模板(含乘法/逆元/log/exp/求导/积分/快速幂)
    UVALive
    Gym
    Gym
    Kattis
    Kattis
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13944780.html
Copyright © 2011-2022 走看看