📄 e883. adding a custom color chooser panel to a jcolorchooser dialog.txt
字号:
This example creates a color chooser panel with three colored buttons. Clicking on a button changes the selected color to the color of the button.
JColorChooser chooser = new JColorChooser();
chooser.addChooserPanel(new MyChooserPanel());
public class MyChooserPanel extends AbstractColorChooserPanel {
// These are the methods that must be implemented
// in order to create a color chooser panel.
// This is called once to initialize the panel.
public void buildChooser() {
setLayout(new GridLayout(0, 3));
makeAddButton("Red", Color.red);
makeAddButton("Green", Color.green);
makeAddButton("Blue", Color.blue);
}
// This method is called whenever the user chooses this panel.
// This method should retrieve the currently selected color.
public void updateChooser() {
}
// This method is called to retrieve the label used
// in the tab that selects this panel.
public String getDisplayName() {
return "MyChooserPanel";
}
// This method is currently not used.
public Icon getSmallDisplayIcon() {
return null;
}
// This method is currently not used.
public Icon getLargeDisplayIcon() {
return null;
}
// These are helper methods specifically for this example
// Creates a color button and adds it to this panel.
private void makeAddButton(String name, Color color) {
JButton button = new JButton(name);
button.setBackground(color);
button.setAction(setColorAction);
add(button);
}
// This action takes the background color of the button
// and uses it to set the selected color.
Action setColorAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton)evt.getSource();
getColorSelectionModel().setSelectedColor(button.getBackground());
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -