Java dastur: Window ichida window yaratish!

GUI ichida GUI


Salom blogimning qadrli o’quvchilari! Bugungi maqolada Java dasturlash tilida kichik dastur tuzishni o’rganib chiqamiz.

Bugungi kunda ko’plab dasturlar ko’p soli hujjat interfeysini ishlatadi – ya’ni kichik windovlarni o’z ichiga oladigan asosiy window va shu orqali parallel ravishda bir nechta ochilgan dokumentlarga ishlov berila oladi.

Misol uchun, ko’plab e-mail dasturlar bir vaqtta ochilgan bir qancha windowlarga ega bo’lish imkonini beradi, demak biz bir vaqtni o’zida ko’plab e-mail xabarlarni yozishimiz va o’qishimiz mumkin.

Shuningdek, ko’plab word prosessorlar foydalanuvchiga asosiy window ichida alohida windowda ko’plab hujjatlarni ochish imkonini berida, alohida windowning yangisini ochish uchun oldingisini yopish shart emas.

Quyidagi dastur ko’p sonli hujjat interfeysini amalga oshirish uchun Swingning JDesktopPane va JInternalFrame klasidan foydalanishni ko’rsatib beradi.

 

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Random;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JDesktopPane;
  9. import javax.swing.JFrame;
  10. import javax.swing.JInternalFrame;
  11. import javax.swing.JMenu;
  12. import javax.swing.JMenuBar;
  13. import javax.swing.JMenuItem;
  14. import javax.swing.JPanel;
  15. public class DesktopFrame extends JFrame{
  16. private JDesktopPane desktop; // GUIni sozlash
  1. public DesktopFrame()
  2. {
  3. super( “JDesktopPane-dan foydalanish” );
  4. JMenuBar bar=new JMenuBar(); // Menu bar yaratish
  5. JMenu menu=new JMenu(“Qo’shish”); // Qo’shish menusini yaratish
  1. JMenuItem yangiFrame=new JMenuItem(“Ichki frame”);
  2. menu.add(yangiFrame); // Qo’shish menusiga yangi frame elementini qo’shish
  3. bar.add(menu); // menu bariga Qo’shish menusini qo’shish
  4. setJMenuBar(bar); // bu ilova uchun menu barni sozlash
  5. desktop=new JDesktopPane(); // desktop pane yaratish
  6. add(desktop);
  7. yangiFrame.addActionListener(
  8. new ActionListener()
  9. {
  10. public void actionPerformed(ActionEvent arg0) {
  11. JInternalFrame frame = new JInternalFrame(“Internal Frame”, true, true, true, true );
  12. MeningJPanel panel = new MeningJPanel(); // yangi panel yaratish
  13. frame.add( panel, BorderLayout.CENTER ); // panelni qo’shish
  14. frame.pack(); // kontentlar o’lchamiga ichki frameni qo’shish
  15. desktop.add( frame ); // ichki frameni bog’lash
  16. frame.setVisible( true ); // ichki frameni ko’rsatish
  17.                  }
  18.           }
  19.      );
  20. }
  21. }
  22. class MeningJPanel extends JPanel
  23. {
  24. private static Random generator = new Random();
  25. private ImageIcon picture; // ko’rsatiladigan rasm
  26. private static String[] images = { “oq.png”, “qipqizil.png”, “qizil.png”, “sariq.png”, “sovg’a.png” };
  1. public MeningJPanel()
  2. {
  3. int randomNumber = generator.nextInt( images.length );
  4. picture = new ImageIcon( getClass().getResource(images[ randomNumber ]) ); // ikonni sozlash
  5. }
  6. public void paintComponent( Graphics g )
  7. {
  8. super.paintComponent( g );
  9. picture.paintIcon( this, g, 0, 0 ); // ikonni ko’rsatish
  10. }
  11. public Dimension getPreferredSize()
  12. {
  13. return new Dimension( picture.getIconWidth(),
  14. picture.getIconHeight() );
  15. }
  16. }

Javada ichki window

Javada ichki window

20 – 25 qatorlar JMenuBar, JMenu va JMenuItem-ni yaratadi, JMenu-ga JMenuItemni qo’shib qo’yadi, JMenuBar-ga JMenuni qo’shadi va ilova oynasi (window) uchun JMenuBarni sozlab qo’yadi. Foydalanuvchi JMenuItem yangiFrame-ni tanlaganida, ilova (dastur) rasmli yangi JInternalFrame obyektini yaratadi va ko’rsatadi.

26 – qator desktop o’zgaruvchisini JInternalFrame bola oynalarni (window) boshqarish uchun ishlatiladigan yangi JDesktopPane obyektiga tenglashtirib qo’yadi. 27 – qator JFramega JDesktopPaneni qo’shadi. Boshlang’ich holatda, JDesktopPane kontent oynasining BorderLayouti markaziga qo’shiladi, JDesktopPane to’liq ilova oynasini to’ldirish uchun kengayadi.

28 – 40 qatorlar foydalanuvchi yangiFrame menu elementini tanlaganda, hodisani boshqarish uchun ActionListenerdan ro’yxatdan o’tkazadi. Hodisa sodir bo’lganda, actionPerformed() metodi ( 31 – 38 qatorlar) 32 – qatordagi JInternalFrame obyektini yaratadi.

Bu yerda foydalanilgan JInternalFrame konstraktori beshta argumentni oladi – ichki windowning sarlavha bari uchun String, ichki frame o’lchami foydalanuvchi tomonidan o’zgartirilishi mumkin yoki yo’qligini ko’rsatuvchi boolean, ichki frame foydalanuvchi tomonidan yopilishi mumkin yoki yo’qligini ko’rsatuvchi boolean, ichki frameni foydalanuvchi maksimallashtira (kattalashtirish) olish olmasligini ko’rsatadigan boolean va ichki frameni foydalanuvchi kichiklashtira oladimi yo’qmi ko’rsatadigan boolean.

Boolean argumentlarning har qaysisi uchun, true qiymat operatsiya ruxsat etilishini ko’rsatadi.

JFrame va JApplet singari, JInternalFrame GUI komponentlari mahkamlanishi mumkin bo’lgan kontent oynasiga ega. 33 – qator 34 – qatordagi JInternalFramega qo’shilgan MeningJPanel sinfning ( 43 – 63 qatorlarda e’lon qilingan ) namunasini yaratadi.

 

  1. import javax.swing.JFrame;
  2. public class MainSinf extends JFrame {
  3. public static void main(String[] args) {
  4. DesktopFrame desktopFrame = new DesktopFrame();
  5. desktopFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  6. desktopFrame.setSize( 600, 480 ); // frame o’lchamlarini sozlash
  7. desktopFrame.setVisible( true ); // frameni ko’rsatish
  8. }
  9. }

35 – qator ichki windowning o’lchamini sozlash uchun JInternalFrame pack() metodidan foydalanadi. pack() metodi windows o’lchamini aniqlash uchun komponentlarning ma’qul ko’rilgan o’lchamidan foydalanadi.

MeningJPanel sinfi getPreferredSize() metodini e’lon qiladi (58 – 62). Bu metod pack() metodi faoliyati uchun panelning ma’qul o’lchamini belgilaydi. 36 – qator JDesktopPanega JInternalFrameni qo’shadi va 37 – qator JInternalFrameni ko’rsatadi.

Shu bilan Javada kichik dasturni tuzib chiqdik. Dasturni tuzish bo’yicha savollar bo’ladigan bo’lsa kommentda qoldiring!

Endi esa keyingi maqolalarda ko’rishguncha xayr!

 

Muallif: Oxunjon G’aybullayev

BULAR BILAN HAM TANISHING:

Publication author

offline 1 day

Oxunjon G'aybullayev

10
Comments: 122Publics: 122Registration: 04-10-2017
Like this post? Please share to your friends:
Leave a Reply
Tarmoq orqali kiring: 

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: