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

GUI

مغلق
بدأه المبرمج1 في 23 مارس 2004 · 3 رد · 915 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

simple code that creat GUI components , check box , Radio button, dropdown list , and sellect list..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class GUITest extends JFrame{

  //the viriables
  private JTextField field;
  private JCheckBox bold , italic;
  private ButtonGroup RadioButtons;
  private JRadioButton black , red , blue;
  private JComboBox Icone;
  private JList BackGround;
  private JLabel label;

  private String IconNames[]  = {"icon1.gif","icon2.gif","icon3.gif","icon4.gif","icon5.gif",}; // <== put some *.gif images with the same names
  private String ColorNames[] = {"White", "Black", "Blue", "Red", "Green", "Yellow" };

  private Icon icons[] = {new ImageIcon(IconNames[0]) ,new ImageIcon(IconNames[1]) ,
                          new ImageIcon(IconNames[2]) ,new ImageIcon(IconNames[3]) ,
                          new ImageIcon(IconNames[4]) ,};

  private final Color colors[] = {Color.white , Color.black , Color.BLUE , Color.red , Color.GREEN , Color.yellow  };

  //set up the GUI
  public GUITest()
    {
    super(" GUI Test");

    final Container container = getContentPane();
    container.setLayout(new FlowLayout());


    //the text------------------------------------------------------------------
    field = new JTextField("\tWelcom in this GUI..\t");
    field.setFont(new Font("Serif", Font.PLAIN , 14));
    field.setEditable(false);
    field.setBackground(Color.lightGray);

    container.add(field);

    // the check boxs-----------------------------------------------------------
    bold = new JCheckBox("Bold");
    italic = new JCheckBox("Italic");
    container.add(bold);
    container.add(italic);
       //add event handler to the check boxs
    EventHandler handler = new EventHandler();
    bold.addItemListener(handler);
    italic.addItemListener(handler);

    // the RadioButon-----------------------------------------------------------
    black = new JRadioButton("Black", true);
    red = new JRadioButton("Red", false);
    blue = new JRadioButton("Blue", false);
       //add them to the form
    container.add(black);
    container.add(red);
    container.add(blue);
       // group them
    RadioButtons = new ButtonGroup();
    RadioButtons.add(black);
    RadioButtons.add(red);
    RadioButtons.add(blue);
       // add event handler to the radio button
    black.addItemListener(handler);
    red.addItemListener(handler);
    blue.addItemListener(handler);

    // drop down list-----------------------------------------------------------
    Icone = new JComboBox(IconNames);
    Icone.setMaximumRowCount(3);
        //add event handler
    Icone.addItemListener(   //S1
        new ItemListener(){  // S2
            public void itemStateChanged(ItemEvent event){ //S3
            if(event.getStateChange() == ItemEvent.SELECTED)
              label.setIcon(icons[Icone.getSelectedIndex()]);
            }   //E3
        }       //E2
        );// end add item Listners - E1

    container.add(Icone);
    label = new JLabel(icons[0]);
    container.add(label);

    // selection list-----------------------------------------------------------
    BackGround = new JList(ColorNames);
    BackGround.setVisibleRowCount(4);
    BackGround.setBackground(Color.LIGHT_GRAY);
    BackGround.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    container.add(new JScrollPane(BackGround));
         //add event handler
    BackGround.addListSelectionListener( // S1
        new ListSelectionListener(){     //S2
        public void valueChanged (ListSelectionEvent event){ //S3
          Color BG = colors[BackGround.getSelectedIndex()];
          container.setBackground(BG);
            // just to make all the item with the same BackGround
          BackGround.setBackground(BG);

          bold.setBackground(BG);
          italic.setBackground(BG);

          black.setBackground(BG);
          blue.setBackground(BG);
          red.setBackground(BG);

          Icone.setBackground(BG);

          field.setBackground(BG);

        } //E3
    }     //E2
    );// end ListSelectionListener E1


    // the frame setting--------------------------------------------------------
    setSize(500 , 300);
    setVisible(true);



    }//end the constructor


//
// class for Event Handler------------------------------------------------------
private class EventHandler implements ItemListener
  {

       private int ValBold   = Font.PLAIN;
       private int ValItalic = Font.PLAIN;
       private Color FontColor;
       public void itemStateChanged(ItemEvent event)
       {
         if (event.getSource() == bold)
             ValBold = bold.isSelected() ? Font.BOLD : Font.PLAIN;

         if (event.getSource() == italic)
             ValItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN;

         if (black.isSelected()){FontColor = Color.black;}
         else if (red.isSelected()){FontColor = Color.red;}
         else if (blue.isSelected()){FontColor = Color.BLUE;}

         field.setFont(new Font("Serif", ValBold+ValItalic , 14));
         field.setForeground (FontColor);
       }// end method CheckBox

  }


/// end class EventHandler









// the main method
 public static void main(String[] args) {
   System.out.println("\n\n\tStart the main Method:\n");

   // the call for the classes start from hear ---VV--V--Vv ----vV-------------

    GUITest test = new GUITest();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   // the call for the classes end hear ------------^^-^^^---^-^^^^^-----------

 }//end the main method

}// end class GUITest

if you have any question just ask..

تم تعديل هذه المشاركة بواسطة المبرمج1 في 23 مارس 2004 في 16:01

#2

جاري الاطلاع ...

#3

مشكور سوف يتم نقله للارشيف قريبا

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#4

thank you Rame , this will motivate me to do more ..

تم تعديل هذه المشاركة بواسطة المبرمج1 في 28 مارس 2004 في 15:53

هذا الموضوع مغلق.

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