Make Color Chooser in Java Swing
Make Color Chooser in Java Swing:
Make Color Chooser in Java Swing - YouTube ________________________________________________ How to Make Color Chooser in Java Swing?
package Test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
public class ColorChooser extends JPanel implements ChangeListener {
protected JColorChooser clrchooser;
protected JLabel lbl;
public ColorChooser()
{
super(new BorderLayout());
lbl = new JLabel("Text Color",JLabel.CENTER);
lbl.setForeground(Color.cyan);
lbl.setBackground(Color.WHITE);
lbl.setOpaque(true);
lbl.setFont(new Font("SansSerif", Font.BOLD, 30));
lbl.setPreferredSize(new Dimension(100, 65));
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(lbl, BorderLayout.CENTER);
pnl.setBorder(BorderFactory.createTitledBorder("Text"));
clrchooser = new JColorChooser(lbl.getForeground());
clrchooser.getSelectionModel().addChangeListener(this);
clrchooser.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));
add(pnl, BorderLayout.CENTER);
add(clrchooser, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent e)
{
Color newColor = clrchooser.getColor();
lbl.setForeground(newColor);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Color Chooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new ColorChooser();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setLocation(550,100);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run(){
createAndShowGUI();
}
});
}
}
________________________________________________
Dark Hers
YouTube
https://instagram.com/darkhers
https://www.facebook.com/darkhers
TikTok
https://www.tiktok.com/@darkhers
https://www.pinterest.co.uk/darkhers
Comments
Post a Comment