更改 JColorChooser 的预览面板
Change Preview Panel for JColorChooser
objective 是将预览部分更改为:
像这样,预览区域有一个边框,背景中有一个实心框和 "Hello World" 字符串,该字符串会更改为为预览选择的颜色。
从 java2 中的这个示例开始,它简单地显示了 JColorChooser。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class ColorChooserSample {
public static void main(String args[]) {
JFrame f = new JFrame("JColorChooser Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
final JButton button = new JButton("Pick to Change Background");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Color initialBackground = button.getBackground();
JColorChooser jColorChooser = new JColorChooser();
jColorChooser.setPreviewPanel(null);
Color background = jColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
if (background != null) {
button.setBackground(background);
}
}
};
button.addActionListener(actionListener);
content.add(button, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
只是为了看看我是否能影响预览区域,我修改了原来的代码:
Color background = JColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
对此:
JColorChooser jColorChooser = new JColorChooser();
jColorChooser.setPreviewPanel(null);
Color background = jColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
基本上这是为了尝试查看预览部分是否可以为空(空白),但它没有任何影响,这让我想知道 setPreviewPanel() 是否是正确的调用。
此外,代码更改后出现此警告:应以静态方式访问 JColorChooser 类型的静态方法 showDialog(Component, String, Color)
问题:
是否有改变颜色选择器预览部分的示例?
为什么上面的 null 不起作用。
如果警告指示应该以静态方式访问 JColorChooser,那么实际如何进行 setPreviewPanel() 调用?
修改 JColorChooser
预览面板的例子不多。我找到了四个例子。与 JColorChooser
的 Javadoc 一起,我创建了一个修改后的 JColorChooser
预览面板的工作示例。
我修改了原来的JFrame
/JPanel
,把JPanel
的背景色改了,这样会更容易看出来。
这是修改后的 JColorChooser
.
这里我把预览的背景颜色JPanel
改成了黄色
然后将主 JPanel
的背景颜色更改为黄色。
这是代码。我将代码分成 bite-sized 方法和 类,因此我可以一次专注于 GUI 数据的一部分。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorChooserSample implements Runnable{
public static void main(String args[]) {
SwingUtilities.invokeLater(
new ColorChooserSample());
}
private JPanel panel;
@Override
public void run() {
JFrame frame = new JFrame(
"JColorChooser Sample");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
JButton button = new JButton(
"Pick to Change JPanel Background");
button.addActionListener(new ColorListener());
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void setJPanelBackground(Color color) {
panel.setBackground(color);
panel.repaint();
}
public class ColorListener implements
ActionListener, ChangeListener {
private JColorChooser chooser;
private JPanel previewPanel;
@Override
public void actionPerformed(
ActionEvent actionEvent) {
Color backgroundColor = showDialog(panel,
"Set JPanel Background",
panel.getBackground());
setJPanelBackground(backgroundColor);
}
private Color showDialog(Component component,
String title, Color initialColor)
throws HeadlessException {
chooser = new JColorChooser(initialColor);
chooser.getSelectionModel()
.addChangeListener(this);
// configuring color chooser panel
previewPanel = new JPanel();
previewPanel.setBackground(initialColor);
JLabel label = new JLabel("Hello World!");
previewPanel.add(label);
chooser.setPreviewPanel(previewPanel);
// creating dialog
ColorTracker ok = new ColorTracker(chooser);
JDialog dialog = JColorChooser.createDialog(
component, title, true, chooser,
ok, null);
dialog.setVisible(true);
return ok.getColor();
}
@Override
public void stateChanged(ChangeEvent event) {
Color newColor = chooser.getColor();
previewPanel.setBackground(newColor);
}
}
private class ColorTracker implements ActionListener {
private Color color;
private JColorChooser chooser;
public ColorTracker(JColorChooser chooser) {
this.chooser = chooser;
}
@Override
public void actionPerformed(ActionEvent event) {
color = chooser.getColor();
}
public Color getColor() {
return color;
}
}
}
objective 是将预览部分更改为:
像这样,预览区域有一个边框,背景中有一个实心框和 "Hello World" 字符串,该字符串会更改为为预览选择的颜色。
从 java2 中的这个示例开始,它简单地显示了 JColorChooser。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class ColorChooserSample {
public static void main(String args[]) {
JFrame f = new JFrame("JColorChooser Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
final JButton button = new JButton("Pick to Change Background");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Color initialBackground = button.getBackground();
JColorChooser jColorChooser = new JColorChooser();
jColorChooser.setPreviewPanel(null);
Color background = jColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
if (background != null) {
button.setBackground(background);
}
}
};
button.addActionListener(actionListener);
content.add(button, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
只是为了看看我是否能影响预览区域,我修改了原来的代码:
Color background = JColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
对此:
JColorChooser jColorChooser = new JColorChooser();
jColorChooser.setPreviewPanel(null);
Color background = jColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
基本上这是为了尝试查看预览部分是否可以为空(空白),但它没有任何影响,这让我想知道 setPreviewPanel() 是否是正确的调用。
此外,代码更改后出现此警告:应以静态方式访问 JColorChooser 类型的静态方法 showDialog(Component, String, Color)
问题:
是否有改变颜色选择器预览部分的示例?
为什么上面的 null 不起作用。
如果警告指示应该以静态方式访问 JColorChooser,那么实际如何进行 setPreviewPanel() 调用?
修改 JColorChooser
预览面板的例子不多。我找到了四个例子。与 JColorChooser
的 Javadoc 一起,我创建了一个修改后的 JColorChooser
预览面板的工作示例。
我修改了原来的JFrame
/JPanel
,把JPanel
的背景色改了,这样会更容易看出来。
这是修改后的 JColorChooser
.
这里我把预览的背景颜色JPanel
改成了黄色
然后将主 JPanel
的背景颜色更改为黄色。
这是代码。我将代码分成 bite-sized 方法和 类,因此我可以一次专注于 GUI 数据的一部分。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorChooserSample implements Runnable{
public static void main(String args[]) {
SwingUtilities.invokeLater(
new ColorChooserSample());
}
private JPanel panel;
@Override
public void run() {
JFrame frame = new JFrame(
"JColorChooser Sample");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
JButton button = new JButton(
"Pick to Change JPanel Background");
button.addActionListener(new ColorListener());
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void setJPanelBackground(Color color) {
panel.setBackground(color);
panel.repaint();
}
public class ColorListener implements
ActionListener, ChangeListener {
private JColorChooser chooser;
private JPanel previewPanel;
@Override
public void actionPerformed(
ActionEvent actionEvent) {
Color backgroundColor = showDialog(panel,
"Set JPanel Background",
panel.getBackground());
setJPanelBackground(backgroundColor);
}
private Color showDialog(Component component,
String title, Color initialColor)
throws HeadlessException {
chooser = new JColorChooser(initialColor);
chooser.getSelectionModel()
.addChangeListener(this);
// configuring color chooser panel
previewPanel = new JPanel();
previewPanel.setBackground(initialColor);
JLabel label = new JLabel("Hello World!");
previewPanel.add(label);
chooser.setPreviewPanel(previewPanel);
// creating dialog
ColorTracker ok = new ColorTracker(chooser);
JDialog dialog = JColorChooser.createDialog(
component, title, true, chooser,
ok, null);
dialog.setVisible(true);
return ok.getColor();
}
@Override
public void stateChanged(ChangeEvent event) {
Color newColor = chooser.getColor();
previewPanel.setBackground(newColor);
}
}
private class ColorTracker implements ActionListener {
private Color color;
private JColorChooser chooser;
public ColorTracker(JColorChooser chooser) {
this.chooser = chooser;
}
@Override
public void actionPerformed(ActionEvent event) {
color = chooser.getColor();
}
public Color getColor() {
return color;
}
}
}