简介
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);
}
}
}
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》