package ish;
import java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.event.*;
public class
ColorChooserTest
{
public static void
main(String[] args)
{
ColorChooserFrame
frame=new ColorChooserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class
ColorChooserFrame extends JFrame
{
public
ColorChooserFrame()
{
setTitle("ColorChooserTest");
setSize(WIDTH,HEIGHT);
ColorChooserPanel
panel=new ColorChooserPanel();
Container
contentPane=getContentPane();
contentPane.add(panel);
}
public static final
int WIDTH=300;
public static final
int HEIGHT=200;
}
class
ColorChooserPanel extends JPanel
{
public
ColorChooserPanel()
{
JButton
modalButton=new JButton("Modal");
modalButton.addActionListener(new
ModalListener());
add(modalButton);
JButton
modalessButton=new JButton("Modaless");
modalessButton.addActionListener(new
ModelessListener());
add(modalessButton);
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);
setBackground(Selected);
}
}
private class
ModelessListener implements ActionListener
{
public
ModelessListener()
{
chooser=new
JColorChooser();
dialog=JColorChooser.createDialog(ColorChooserPanel.this,"Backgroundcolor",false,chooser,new
ActionListener()
{
public void
actionPerformed(ActionEvent event)
{
setBackground(chooser.getColor());
}
},null);
}
public void
actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.show();
}
private JDialog
dialog;
private
JColorChooser chooser;
}
private class
ImmediateListener implements ActionListener
{
public
ImmediateListener()
{
chooser=new
JColorChooser();
chooser.getSelectionModel().addChangeListener(new
ChangeListener()
{
public void
stateChanged(ChangeEvent event)
{
setBackground(chooser.getColor());
}
});
dialog=new
JDialog((Frame)null,false);
dialog.getContentPane().add(chooser);
dialog.pack();
}
public void
actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.show();
}
private JDialog
dialog;
private
JColorChooser chooser;
}
}
No comments:
Post a Comment