import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JDialog;
public static void main(String[] args) {
JFrame frame = new JFrame("JColorChooser demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(400, 200); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 200
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Buttons لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
// frame و تحديد موقعه و حجمه بداخل الـ Button هنا قمنا بتعريف
JButton button = new JButton("Change background color");
button.setBounds(90, 70, 200, 30);
// frame في الـ button هنا قمنا بوضع الـ
// لإختيار الألوان مع تخصيص محتواها Panel و الذي يمثل JColorChooser هنا قمنا بتعريف كائن من الكلاس
JColorChooser colorChooser = new JColorChooser();
// هنا قمنا بتحديد اللون الإفتراضي الذي سيكون مختاراً فيه
colorChooser.setColor(0, 51, 255);
// مع تحديد خصائصه colorChooser هنا قمنا بتعريف الذي سنضعه بداخل الـ
JLabel previewLabel = new JLabel("Selected Color");
previewLabel.setFont(new Font("Serif", Font.BOLD, 34));
previewLabel.setSize(previewLabel.getPreferredSize());
// colorChooser الموجودة في الـ Preview الـ Panel بداخل previewLabel هنا قمنا بوضع الـ
colorChooser.setPreviewPanel(previewLabel);
// colorChooser الموجود في الـ Ok و الذي سنضع فيه ما سيحدث عند النقر على الزر ActionListener هنا قمنا بتعريف كائن من الكلاس
ActionListener okActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// فقط Frame سيتم وضع اللون المختار كخلفية للـ
frame.getContentPane().setBackground(colorChooser.getColor());
// colorChooser الموجود في الـ Cancel و الذي سنضع فيه ما سيحدث عند النقر على الزر ActionListener هنا قمنا بتعريف كائن من الكلاس
ActionListener cancelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// هنا يمكنك وضع أي أوامر تريد
// كمحتواه الوحيد colorChooser و سيتم وضع الـ dialog هنا سيتم إنشاء الـ
JDialog dialog = JColorChooser.createDialog(frame,
// مرئية frame هنا جعلنا الـ
// dialog سيتم فقط إظهار الـ button عند النقر
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JDialog;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JColorChooser demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(400, 200); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 200
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Buttons لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
// frame و تحديد موقعه و حجمه بداخل الـ Button هنا قمنا بتعريف
JButton button = new JButton("Change background color");
button.setBounds(90, 70, 200, 30);
// frame في الـ button هنا قمنا بوضع الـ
frame.add(button);
// لإختيار الألوان مع تخصيص محتواها Panel و الذي يمثل JColorChooser هنا قمنا بتعريف كائن من الكلاس
JColorChooser colorChooser = new JColorChooser();
// هنا قمنا بتحديد اللون الإفتراضي الذي سيكون مختاراً فيه
colorChooser.setColor(0, 51, 255);
// مع تحديد خصائصه colorChooser هنا قمنا بتعريف الذي سنضعه بداخل الـ
JLabel previewLabel = new JLabel("Selected Color");
previewLabel.setFont(new Font("Serif", Font.BOLD, 34));
previewLabel.setSize(previewLabel.getPreferredSize());
// colorChooser الموجودة في الـ Preview الـ Panel بداخل previewLabel هنا قمنا بوضع الـ
colorChooser.setPreviewPanel(previewLabel);
// colorChooser الموجود في الـ Ok و الذي سنضع فيه ما سيحدث عند النقر على الزر ActionListener هنا قمنا بتعريف كائن من الكلاس
ActionListener okActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
// فقط Frame سيتم وضع اللون المختار كخلفية للـ
frame.getContentPane().setBackground(colorChooser.getColor());
}
};
// colorChooser الموجود في الـ Cancel و الذي سنضع فيه ما سيحدث عند النقر على الزر ActionListener هنا قمنا بتعريف كائن من الكلاس
ActionListener cancelActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
// هنا يمكنك وضع أي أوامر تريد
}
};
// كمحتواه الوحيد colorChooser و سيتم وضع الـ dialog هنا سيتم إنشاء الـ
JDialog dialog = JColorChooser.createDialog(frame,
"Color Picker",
false,
colorChooser,
okActionListener,
cancelActionListener);
// مرئية frame هنا جعلنا الـ
frame.setVisible(true);
// dialog سيتم فقط إظهار الـ button عند النقر
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
});
}
}