Java Swing | JColorChooser类

JColorChooser提供了一个控件窗格,允许用户操作和选择颜色。这门课提供 三级API :

null
  1. 一种静态便利方法,显示模式颜色选择器对话框,并返回用户选择的颜色。
  2. 创建颜色选择器对话框的静态便捷方法,其中 行动听众 可以指定在用户按下其中一个对话框按钮时调用。
  3. 创建 颜色选择器 直接(在任何容器内)窗格。 财产变动 可以添加侦听器来检测当前“颜色”属性何时更改。

类的构造函数:

  1. JColorChooser(): 创建初始颜色为白色的颜色选择器窗格。
  2. JColorChooser(颜色初始颜色): 使用指定的初始颜色创建颜色选择器窗格。
  3. JColorChooser(颜色选择模型): 创建具有指定颜色的颜色选择器窗格 颜色选择模型 .

常用方法:

方法 描述
设置颜色(颜色) 将颜色选择器的当前颜色设置为指定的颜色。
设置颜色(int c) 将颜色选择器的当前颜色设置为指定的颜色。
setColor(整数r、整数g、整数b) 将颜色选择器的当前颜色设置为指定的RGB颜色。
showDialog(组件cmp、字符串标题、颜色初始颜色) 显示一个模式颜色选择器对话框,并阻止该对话框,直到该对话框被隐藏。
updateUI() 来自UIManager的L&F变更通知
setChooserPanels(AbstractColorChooserPanel[]面板) 指定用于选择颜色值的颜色面板。
addChooserPanel(AbstractColorChooserPanel) 将颜色选择器面板添加到颜色选择器。
setUI(颜色选择器ui) 设置渲染此组件的L&F对象。
setSelectionModel(颜色选择Model newModel) 设置包含选定颜色的模型。
setPreviewPanel(JComponent preview) 设置当前预览面板。

创建自定义选择器面板: 默认颜色选择器提供五个选择器面板:

  1. 样本: 用于从样本集合中选择颜色。
  2. HSV: 用于使用色调饱和度值颜色表示选择颜色。在JDK 7之前,它被称为HSB,用于色调饱和亮度。
  3. HSL: 用于使用色调饱和度亮度颜色表示选择颜色。
  4. RGB: 用于使用红-绿-蓝颜色模型选择颜色。
  5. CMYK: 用于使用过程颜色或四色模型选择颜色。

以下程序说明了JColorChooser类的使用:

1.使用ChangeListener实现JColorChooser类的Java程序: 在这个程序中,我们首先在窗口顶部创建一个标签,其中显示一些文本,我们将在其中应用颜色更改。设置前景色和背景色。设置字体的大小和类型。创建面板并设置其布局。现在设置颜色选择器来设置文本颜色。使用 stateChanged() 方法生成文本颜色更改的事件 getColor() 方法现在创建GUI,创建一个设置窗口。设置窗口的默认关闭操作。创建并设置内容窗格,将内容添加到框架并显示窗口。

JAVA

// Java program to implement JColorChooser
// class using ChangeListener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
public class ColorChooserDemo extends JPanel
implements ChangeListener {
protected JColorChooser Jcc;
protected JLabel label;
public ColorChooserDemo()
{
super ( new BorderLayout());
// Set up the Label at the top of the window
label = new JLabel( "Welcome to GeeksforGeeks" ,
JLabel.CENTER);
// set the foreground color of the text
label.setForeground(Color.green);
// set background color of the field
label.setBackground(Color.WHITE);
label.setOpaque( true );
// set font type and size of the text
label.setFont( new Font( "SansSerif" , Font.BOLD, 30 ));
// set size of the label
label.setPreferredSize( new Dimension( 100 , 65 ));
// create a Panel and set its layout
JPanel bannerPanel = new JPanel( new BorderLayout());
bannerPanel.add(label, BorderLayout.CENTER);
bannerPanel.setBorder(BorderFactory.createTitledBorder( "Label" ));
// Set up color chooser for setting text color
Jcc = new JColorChooser(label.getForeground());
Jcc.getSelectionModel().addChangeListener( this );
Jcc.setBorder(BorderFactory.createTitledBorder(
"Choose Text Color" ));
add(bannerPanel, BorderLayout.CENTER);
add(Jcc, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent e)
{
Color newColor = Jcc.getColor();
label.setForeground(newColor);
}
// Create the GUI and show it.  For thread safety,
// this method should be invoked from the
// event-dispatching thread.
private static void createAndShowGUI()
{
// Create and set up the window.
JFrame frame = new JFrame( "ColorChooserDemo" );
// set default close operation of the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new ColorChooserDemo();
// content panes must be opaque
newContentPane.setOpaque( true );
// add content pane to the frame
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible( true );
}
// Main Method
public static void main(String[] args)
{
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run()
{
createAndShowGUI();
}
});
}
}


输出:

2.使用ActionListener实现JColorChooser类的Java程序: 创建一个按钮和一个容器,并设置容器的布局。添加 ActionListener() 添加到按钮,并将按钮添加到容器中。 ActionListerner() 有一种方法 执行的动作() 单击按钮后立即执行。选择颜色并设置容器的背景色。

JAVA

// Java program to implement JColorChooser
// class using ActionListener
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ColorChooserExample extends
JFrame implements ActionListener {
// create a button
JButton b = new JButton( "color" );
Container c = getContentPane();
// Constructor
ColorChooserExample()
{
// set Layout
c.setLayout( new FlowLayout());
// add Listener
b.addActionListener( this );
// add button to the Container
c.add(b);
}
public void actionPerformed(ActionEvent e)
{
Color initialcolor = Color.RED;
// color chooser Dialog Box
Color color = JColorChooser.showDialog( this ,
"Select a color" , initialcolor);
// set Background color of the Container
c.setBackground(color);
}
// Main Method
public static void main(String[] args)
{
ColorChooserExample ch = new ColorChooserExample();
ch.setSize( 400 , 400 );
ch.setVisible( true );
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}


输出:

注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。 参考: https://docs.oracle.com/javase/7/docs/api/javax/swing/JColorChooser.html

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享