Dropping in with a small tutorial how to create a colour palette !
Anyhow lets begin with it.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JPalette extends JFrame {
//Our buttons and functions
private JButton ChooseColour;
private Color Colour= Color.lightGray;
private Container c;
//Constructor
public JPalette ()
{
super( "JColour Changer" );
//The Layout
c = getContentPane();
c.setLayout( new FlowLayout() );
//The button and palettes function layout
ChooseColour= new JButton( "Colour Palette" );
ChooseColour.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e ) {
Colour =
JColorChooser.showDialog( JPalette.this,
"ChooseColour", Colour );
//If to check if listener works fine...
if ( Colour== null )
Colour= Color.lightGray;
c.setBackground(Colour);
c.repaint();
}
}
);
c.add( ChooseColour);
//Size of the layout
setSize( 400, 130 );
show();
}
//The main
public static void main( String args[] )
{
JPalette JP= new JPalette ();
//Listener to the main to be able to close whenever we use "default" closing
which is the X button...
JP.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
So there you go !
Have fun and enjoy :)


Sign In
Create Account



Back to top









