📄 e884. listening for ok and cancel events in a jcolorchooser dialog.txt
字号:
This example defines an action that creates and shows a color chooser dialog.
// Create the chooser
final JColorChooser chooser = new JColorChooser();
// Define listener for ok events
ActionListener okListener = new ActionListener() {
// Called when user clicks ok
public void actionPerformed(ActionEvent evt) {
// Note: The source is an internal button in the dialog
// and should not be used
// Get selected color
Color newColor = chooser.getColor();
}
};
// Define listener for cancel events
ActionListener cancelListener = new ActionListener() {
// Called when user clicks cancel
public void actionPerformed(ActionEvent evt) {
// Note: The source is an internal button in the dialog
// and should not be used
// Note: The original color is not restored.
// getColor() returns the latest selected color.
Color newColor = chooser.getColor();
}
};
// Choose whether dialog is modal or modeless
boolean modal = false;
// Create the dialog that contains the chooser
dialog = JColorChooser.createDialog(null, "Dialog Title", modal,
chooser, okListener, cancelListener);
// Add listener for clicks to the close-window icon
dialog.addWindowListener(new WindowAdapter() {
// Called when user clicks the close-window icon.
// This type of event is usually treated like a cancel.
public void windowClosing(WindowEvent evt) {
// Note: The original color is not restored.
// getColor() returns the latest selected color.
Color newColor = chooser.getColor();
}
}) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -