الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف اضيف لون للنص؟ ( مساعدة )

بدأه الأورتكواي في 21 مايو 2010 · 13 رد · 899 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمه الله هذا برنامج فيه اربع ايقونات راديو بتوم لـ اربع حالات للخط ( plan و bold و italic و bolditalic ـ)

ابغى اعدل على الكود بحيث يصبح لكل حاله خط لون معين ( اي لون ) ..

مثلا الـ plan يكون باللون الاخضر و bold باللون الاحمر وهكذا ..

// Fig. 11.19: RadioButtonFrame.java
// Creating radio buttons using ButtonGroup and JRadioButton.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;



public class RadioButtonFrame extends JFrame 
{
   private JTextField textField; // used to display font changes
   private Font plainFont; // font for plain text
   private Font boldFont; // font for bold text
   private Font italicFont; // font for italic text
   private Font boldItalicFont; // font for bold and italic text
   private JRadioButton plainJRadioButton; // selects plain text
   private JRadioButton boldJRadioButton; // selects bold text
   private JRadioButton italicJRadioButton; // selects italic text
   private JRadioButton boldItalicJRadioButton; // bold and italic
   private ButtonGroup radioGroup; // buttongroup to hold radio buttons //to choose one choise

   // RadioButtonFrame constructor adds JRadioButtons to JFrame
   public RadioButtonFrame()
   {
      super( "RadioButton Test" );
      setLayout( new FlowLayout() ); // set frame layout

      textField = new JTextField( "Watch the font style change", 25 );//25 digets
      add( textField ); // add textField to JFrame

      // create radio buttons
      plainJRadioButton = new JRadioButton( "Plain", true );
      boldJRadioButton = new JRadioButton( "Bold", false );
      italicJRadioButton = new JRadioButton( "Italic", false );
      boldItalicJRadioButton = new JRadioButton( "Bold/Italic", false );
      add( plainJRadioButton ); // add plain button to JFrame
      add( boldJRadioButton ); // add bold button to JFrame
      add( italicJRadioButton ); // add italic button to JFrame
      add( boldItalicJRadioButton ); // add bold and italic button

      // create logical relationship between JRadioButtons
      radioGroup = new ButtonGroup(); // create ButtonGroup
      radioGroup.add( plainJRadioButton ); // add plain to group
      radioGroup.add( boldJRadioButton ); // add bold to group
      radioGroup.add( italicJRadioButton ); // add italic to group
      radioGroup.add( boldItalicJRadioButton ); // add bold and italic

      // create font objects



      plainFont = new Font( "Serif", Font.PLAIN, 14 );
      boldFont = new Font( "Serif", Font.BOLD, 14 );
      italicFont = new Font( "Serif", Font.ITALIC, 14 );
      boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 );
      textField.setFont( plainFont ); // set initial font to plain

      // register events for JRadioButtons   
      plainJRadioButton.addItemListener( 
         new RadioButtonHandler( plainFont ) );
      boldJRadioButton.addItemListener(
         new RadioButtonHandler( boldFont ) );
      italicJRadioButton.addItemListener( 
         new RadioButtonHandler( italicFont ) );
      boldItalicJRadioButton.addItemListener( 
         new RadioButtonHandler( boldItalicFont ) );
   } // end RadioButtonFrame constructor

   // private inner class to handle radio button events    // inner= class intro class
   private class RadioButtonHandler implements ItemListener 
   {
      private Font font; // font associated with this listener

      public RadioButtonHandler( Font f )  //constuctor beacuse take the same name of class
      {
         font = f; // set the font of this listener
      } // end constructor RadioButtonHandler

      // handle radio button events
      public void itemStateChanged( ItemEvent event )
      {
         textField.setFont( font ); // set font of textField
      } // end method itemStateChanged
   } // end private inner class RadioButtonHandler
} // end class RadioButtonFrame

تم تعديل هذه المشاركة بواسطة الأورتكواي في 21 مايو 2010 في 22:34

#2

عن طريق استخدام هذة الدالة ::::

nameOfRadioButton.setForeground(Color.red);

و تستطيع بالفعل تغيير اللون الاحمر

هذا لو كنت تريد تغير لون ما يظهرمع الcomponent

ياريت يا اخ تعدل مشاركتك و تعدل على الكود

لان هذا المنظر مؤذى للعين

تم تعديل هذه المشاركة بواسطة اسراء شلبى في 21 مايو 2010 في 22:47

00020309t.gif

1958_1963.gif
#3

عفواً اختي .. أنا اريد تعديل لون النص الذي يظهر في المعاينة في textfield

#4

ايضا ستستخدم نفس الدالة و لكن ضع اسم ال text field بدلا من ال radio button

ولكن قم بعمل switch case

لثلاث حالات او حسب الخصائص المتوفرة للمستخدم

و فى كل حالة قم بتعير اللون الذى ستضيفة للدالة

اتمنى ان تكون الفكرة وصلت

تم تعديل هذه المشاركة بواسطة اسراء شلبى في 21 مايو 2010 في 22:48

00020309t.gif

1958_1963.gif
#5

المثال هذا فى كتاب How to program java

هذا ما تريد

// Fig. 11.19: RadioButtonFrame.java
// Creating radio buttons using ButtonGroup and JRadioButton.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class RadioButtonFrame extends JFrame {

    private JTextField textField; // used to display font changes
    private Font plainFont; // font for plain text
    private Font boldFont; // font for bold text
    private Font italicFont; // font for italic text
    private Font boldItalicFont; // font for bold and italic text
    private JRadioButton plainJRadioButton; // selects plain text
    private JRadioButton boldJRadioButton; // selects bold text
    private JRadioButton italicJRadioButton; // selects italic text
    private JRadioButton boldItalicJRadioButton; // bold and italic
    private ButtonGroup radioGroup; // buttongroup to hold radio buttons //to choose one choise

// RadioButtonFrame constructor adds JRadioButtons to JFrame
    public RadioButtonFrame() {
        super("RadioButton Test");
        setLayout(new FlowLayout()); // set frame layout

        textField = new JTextField("Watch the font style change", 25);//25 digets
        add(textField); // add textField to JFrame

// create radio buttons
        plainJRadioButton = new JRadioButton("Plain", true);
        plainJRadioButton.setForeground(Color.red);
        boldJRadioButton = new JRadioButton("Bold", false);
        boldJRadioButton.setForeground(Color.GREEN);
        italicJRadioButton = new JRadioButton("Italic", false);
        italicJRadioButton.setForeground(Color.BLUE);
        boldItalicJRadioButton = new JRadioButton("Bold/Italic", false);
        boldItalicJRadioButton.setForeground(Color.pink);
        add(plainJRadioButton); // add plain button to JFrame
        add(boldJRadioButton); // add bold button to JFrame
        add(italicJRadioButton); // add italic button to JFrame
        add(boldItalicJRadioButton); // add bold and italic button

// create logical relationship between JRadioButtons
        radioGroup = new ButtonGroup(); // create ButtonGroup
        radioGroup.add(plainJRadioButton); // add plain to group
        radioGroup.add(boldJRadioButton); // add bold to group
        radioGroup.add(italicJRadioButton); // add italic to group
        radioGroup.add(boldItalicJRadioButton); // add bold and italic

// create font objects



        plainFont = new Font("Serif", Font.PLAIN, 14);
        boldFont = new Font("Serif", Font.BOLD, 14);
        italicFont = new Font("Serif", Font.ITALIC, 14);
        boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        textField.setFont(plainFont); // set initial font to plain

// register events for JRadioButtons
        plainJRadioButton.addItemListener(
                new RadioButtonHandler(plainFont));
        boldJRadioButton.addItemListener(
                new RadioButtonHandler(boldFont));
        italicJRadioButton.addItemListener(
                new RadioButtonHandler(italicFont));
        boldItalicJRadioButton.addItemListener(
                new RadioButtonHandler(boldItalicFont));
    } // end RadioButtonFrame constructor

// private inner class to handle radio button events // inner= class intro class
    private class RadioButtonHandler implements ItemListener {

        private Font font; // font associated with this listener

        public RadioButtonHandler(Font f) //constuctor beacuse take the same name of class
        {
            font = f; // set the font of this listener
        } // end constructor RadioButtonHandler

// handle radio button events
        public void itemStateChanged(ItemEvent event) {
            textField.setFont(font); // set font of textField
        } // end method itemStateChanged
    } // end private inner class RadioButtonHandler

    // test thr class
    public static void main(String[] arg) {
        RadioButtonFrame buttonFrame = new RadioButtonFrame();
        buttonFrame.setVisible(true);
        buttonFrame.setSize(300, 100);
buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buttonFrame.setResizable(false);
    }// end of test class
} // end class RadioButtonFrame

والملف بالمرفقات ايضا

سلام عليكم

RadioButtonFrame.java

تم تعديل هذه المشاركة بواسطة Eng.Hamzawy في 21 مايو 2010 في 23:04

Software Developer
Mahmoudkelany.com


 

#6

الحمد لله ضبط معاي الي كنت عايزه

جزاك الله خير اختي اسراء و اخوي Eng.Hamzawy

وبارك الله فيكم

#7

اعتقد ان كود الاخ حمزاوى لا يقوم بالمطلوب...!

فهو كبرنامجك قبل التعديل....... :wacko:

00020309t.gif

1958_1963.gif
#8

أنا عدلت كود الاخ حمزاوي لهيك

// create radio buttons
        plainJRadioButton = new JRadioButton("Plain", true);
        textField.setForeground(Color.red);
        boldJRadioButton = new JRadioButton("Bold", false);
        textField.setForeground(Color.GREEN);
        italicJRadioButton = new JRadioButton("Italic", false);
        textField.setForeground(Color.BLUE);
        boldItalicJRadioButton = new JRadioButton("Bold/Italic", false);
        textField.setForeground(Color.pink);
        add(plainJRadioButton); // add plain button to JFrame
        add(boldJRadioButton); // add bold button to JFrame
        add(italicJRadioButton); // add italic button to JFrame
        add(boldItalicJRadioButton); // add bold and italic button

لكن النص لا ياخذ الا اخر لون (pink ) فقط

ليش ما يتغير لـ red و green و bule عند التغيير ؟!

#9
اقتباس
اعتقد ان كود الاخ حمزاوى لا يقوم بالمطلوب...!

فهو كبرنامجك قبل التعديل.......

انا اضفت عليه ما يريده الاخ الاورتكواى فاول مطلوب

سوف ارى المطلوب الاخر

Software Developer
Mahmoudkelany.com


 

#10
// Fig. 11.19: RadioButtonFrame.java
// Creating radio buttons using ButtonGroup and JRadioButton.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class RadioButtonFrame extends JFrame {

    private JTextField textField; // used to display font changes
    private Font plainFont; // font for plain text
    private Font boldFont; // font for bold text
    private Font italicFont; // font for italic text
    private Font boldItalicFont; // font for bold and italic text
    private JRadioButton plainJRadioButton; // selects plain text
    private JRadioButton boldJRadioButton; // selects bold text
    private JRadioButton italicJRadioButton; // selects italic text
    private JRadioButton boldItalicJRadioButton; // bold and italic
    private ButtonGroup radioGroup; // buttongroup to hold radio buttons //to choose one choise

// RadioButtonFrame constructor adds JRadioButtons to JFrame
    public RadioButtonFrame() {
        super("RadioButton Test");
        setLayout(new FlowLayout()); // set frame layout

        textField = new JTextField("Watch the font style change", 25);//25 digets
        add(textField); // add textField to JFrame

        plainJRadioButton = new JRadioButton("Plain", true);
        plainJRadioButton.setForeground(Color.red);
        boldJRadioButton = new JRadioButton("Bold", false);
        boldJRadioButton.setForeground(Color.GREEN);
        italicJRadioButton = new JRadioButton("Italic", false);
        italicJRadioButton.setForeground(Color.BLUE);
        boldItalicJRadioButton = new JRadioButton("Bold/Italic", false);
        boldItalicJRadioButton.setForeground(Color.pink);
        add(plainJRadioButton); // add plain button to JFrame
        add(boldJRadioButton); // add bold button to JFrame
        add(italicJRadioButton); // add italic button to JFrame
        add(boldItalicJRadioButton); // add bold and italic button

// create logical relationship between JRadioButtons
        radioGroup = new ButtonGroup(); // create ButtonGroup
        radioGroup.add(plainJRadioButton); // add plain to group
        radioGroup.add(boldJRadioButton); // add bold to group
        radioGroup.add(italicJRadioButton); // add italic to group
        radioGroup.add(boldItalicJRadioButton); // add bold and italic

// create font objects


        boldFont = new Font("Serif", Font.BOLD, 14);
        italicFont = new Font("Serif", Font.ITALIC, 14);
        boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        textField.setFont(plainFont); // set initial font to plain

// register events for JRadioButtons
        plainJRadioButton.addItemListener(
                new RadioButtonHandler(plainFont));
        boldJRadioButton.addItemListener(
                new RadioButtonHandler(boldFont));

        italicJRadioButton.addItemListener(
                new RadioButtonHandler(italicFont));
        boldItalicJRadioButton.addItemListener(
                new RadioButtonHandler(boldItalicFont));
    } // end RadioButtonFrame constructor

// private inner class to handle radio button events // inner= class intro class
    private class RadioButtonHandler implements ItemListener {

        private Font font; // font associated with this listener

        public RadioButtonHandler(Font f) //constuctor beacuse take the same name of class
        {
            font = f; // set the font of this listener
        } // end constructor RadioButtonHandler

// handle radio button events
        public void itemStateChanged(ItemEvent event) {
            textField.setFont(font); // set font of textField
            if (font == boldFont) {
                textField.setForeground(Color.red);
            }
            if (font == italicFont) {
                textField.setForeground(Color.green);
            }
            if (font == plainFont) {
                textField.setForeground(Color.BLUE);
            }
            if (font == boldItalicFont) {
                textField.setForeground(Color.LIGHT_GRAY);
            }
        } // end method itemStateChanged
    } // end private inner class RadioButtonHandler

    // test thr class
    public static void main(String[] arg) {
        RadioButtonFrame buttonFrame = new RadioButtonFrame();
        buttonFrame.setVisible(true);
        buttonFrame.setSize(300, 100);
        buttonFrame.setResizable(false);
    }// end of test class
} // end class RadioButtonFrame

تفضل هذا هو الكود بعد التعديل

والتعديل كان فى هذا الجزء

// handle radio button events
        public void itemStateChanged(ItemEvent event) {
            textField.setFont(font); // set font of textField
            if (font == boldFont) {
                textField.setForeground(Color.red);
            }
            if (font == italicFont) {
                textField.setForeground(Color.green);
            }
            if (font == plainFont) {
                textField.setForeground(Color.BLUE);
            }
            if (font == boldItalicFont) {
                textField.setForeground(Color.LIGHT_GRAY);
            }
        } // end method itemStateChanged

والكود بالمرفقات

اقتباس

لكن النص لا ياخذ الا اخر لون (pink ) فقط

ليش ما يتغير لـ red و green و bule عند التغيير ؟!

لان فى ترتيب الكود ان اللون pink هو اخر لون للـtextfield

فيتم تغيير اللون حسب اخر امر موجود

انما فالتعديل اللى انا وضعته لحضرتك

انه يقارن الخط اللى موجود بنوع الخط وليكن bold

اذا كان متساويان يضع لون الـ textfield بلون احمر مثلا

وهكذا فى باقى الخطوط

اتمنى ان تكون استفدت

والحمدلله اننا قدرنا ساعدك

سلام عليكم

RadioButtonFrame.java

Software Developer
Mahmoudkelany.com


 

#11

جزاك الله خير اخوي وبارك الله فيك وكثر من امثالك يارب

مشكور وما قصرت

#12
اقتباس
جزاك الله خير اخوي وبارك الله فيك وكثر من امثالك يارب

مشكور وما قصرت

العفو يا اخى

وجزاك الله مثل دعاءك

تحت امرك فى اى وقت

ان شاء الله

سلام عليكم

Software Developer
Mahmoudkelany.com


 

#13
اقتباس
اضفت عليه ما يريده الاخ الاورتكواى فاول مطلوب

سوف ارى المطلوب الاخر

بل اضفت ما فهمته انا خطأ

ايضا ستستخدم نفس الدالة و لكن ضع اسم ال text field بدلا من ال radio button 
ولكن قم بعمل switch case
لثلاث حالات او حسب الخصائص المتوفرة للمستخدم
و فى كل حالة قم بتعير اللون الذى ستضيفة للدالة
اتمنى ان تكون الفكرة وصلت

لو قرأت مشاركتى جيدا كنت فعلتها

لانك تضع اكثر من لون لل text field بدون شرط فطبيعى ان البرنامج سيأخذ اخر قيمة او اخر لون تضيفه

مثل ذلك

int x;
x=2;
x=3;

ستكون قيمة xفى النهاية 3

00020309t.gif

1958_1963.gif
#14
اسراء شلبى كتب:

بل اضفت ما فهمته انا خطأ

ايضا ستستخدم نفس الدالة و لكن ضع اسم ال text field بدلا من ال radio button 
ولكن قم بعمل switch case
لثلاث حالات او حسب الخصائص المتوفرة للمستخدم
و فى كل حالة قم بتعير اللون الذى ستضيفة للدالة
اتمنى ان تكون الفكرة وصلت

لو قرأت مشاركتى جيدا كنت فعلتها

لانك تضع اكثر من لون لل text field بدون شرط فطبيعى ان البرنامج سيأخذ اخر قيمة او اخر لون تضيفه

مثل ذلك

int x;
x=2;
x=3;

ستكون قيمة xفى النهاية 3

لم افهم ما قولتيه

فانا فعلا كنت اعمل ما يريده فالطلب الاول

ولكن عندما وضعت الرد

وجدت انها طلب طلبا اخر

ولكن انا وضعتها بدون switch فقد تعاملت مع نوع الخط

وقارنت وبالتالى اعطيت الامر بتغيير لون النص

Software Developer
Mahmoudkelany.com


 

مواضيع مشابهة