javaswing

Java Swing-05 Muloqot Oynalari

MULOQOT OYNALARI
Zamonaviy FGM dasturlarining aksariyatini muloqot oynalarisiz tasavvur etib bo’lmaydi. Muloqot ikki yoki undan ortiq kishilar o’rtasidagi suhbatdir. Komputer dasturlaridagi muloqot bu dasturga gapiruvchi oynadir. Muloqotlar ma’lumotlarni kiritish, o’zgartirish, dastur konfiguratsiyalarinin o’zgartirish, va h.k. uchun ishlatiladi. Muloqotlar foydalanuvchi va kompyuter dasturi o’rtasidagi aloqani yo’lga qo’yuvchi muhim vositadir.

Java Swing uskunalarida, ikki turdagi muloqotlarni yasashimiz mumkin. Odatiy va standart muloqotlar.
Odatiy muloqotlar – dasturchi tomonidan yasalgan muloqot, ular JDialog klassi asosiga quriladi.
Standart muloqotlar – Swing uskunalarida avvaldan yasalgan muloqotlar. Masalan, JColorChooser yoki JFileChooser. Bu turdagi moloqotlar dasturlashdagi umumiy holatda ko’p ishlatiladigan muloqotlar uchundir, masalan, matnni ko’rsatish, foydalanuvchidan ma’lumot olish, fayllarni yuklash va saqlash va h.k. Ular dasturchini vaqtini tejaydi.

Dasturlashda muloqotlar ikki turga bo’linadi. Modal va modal bo’lmagan muloqotlar. Modal muloqotlar dasturning boshqa oynalarini bloklaydi. Bu turdagi muloqotga javob bermasdan, uning ishini bir yoqlik qilmay turib dasturda boshqa ish qilib bo’lmaydi. Faylni ochish muloqotini Modal muloqotga misol qilish mumkin. Ya’ni faylni ochayotgan vaqtda dasturdagi boshqa oynaga o’tib bo’lmaydi. Ikkinchi modal bo’lmaga muloqot avvalgisining aksi, u ochilib turganda boshqa oynaga ham o’tish mumkin. Bunga misol matn qidirish muloqot oynasi. matn qidirish oynasi ochiq turganda dasturning boshqa oynalarini ochish mumkin. Xullas, sharoitga qarab istagan turingizdagi muloqotni o’zingiz tanlayverasiz.

SODDA ODATIY MULOQOT
Quyidagi misolda biz sodda odatiy muloqot yasaymiz. Bu oddiy, aksariyat FGM dasturlarda ko’rish topiladigan, odatda “Yordam’ menyusidan chiqadigan, “Dastur haqida” muloqoti.
*******************************************************************************

[highlighter]

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

class HaqidaMuloqot extends JDialog {

public HaqidaMuloqot() {
initFM();
}

public void initFM() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

add(Box.createRigidArea(new Dimension(0, 10)));

JLabel nomi = new JLabel("Dastur, versiya raqami ");
nomi.setFont(new Font("Serif", Font.BOLD, 13));
nomi.setAlignmentX(JComponent.CENTER_ALIGNMENT);
add(nomi);

add(Box.createRigidArea(new Dimension(0, 50)));

JButton yopishTugmasi = new JButton("Yopish");
yopishTugmasi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dispose();
}
});

yopishTugmasi.setAlignmentX(JComponent.CENTER_ALIGNMENT);
add(yopishTugmasi);

setModalityType(ModalityType.APPLICATION_MODAL);

setTitle("Dastur haqida");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setSize(170, 150);
}
}

public class DasturHaqidagaMisol extends JFrame {

public DasturHaqidagaMisol() {
initFM();
}

public final void initFM() {

JMenuBar menyuOrni = new JMenuBar();

JMenu fayl = new JMenu("Fayl");
fayl.setMnemonic(KeyEvent.VK_F);

JMenu yordam = new JMenu("Yordam");
yordam.setMnemonic(KeyEvent.VK_Y);

JMenuItem haqida = new JMenuItem("Dastur Haqida");
yordam.add(haqida);

haqida.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
HaqidaMuloqot hm = new HaqidaMuloqot();
hm.setVisible(true);
}
});

menyuOrni.add(fayl);
menyuOrni.add(yordam);
setJMenuBar(menyuOrni);

setTitle("Oddiy muloqot");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DasturHaqidagaMisol dhm = new DasturHaqidagaMisol();
dhm.setVisible(true);
}
});
}
}

*******************************************************************************
yuqoridagi namunaviy kod kichkina muloqot oynasini chiqaradi. Muloqotda matn va yopish tugmasi o’rin olgan.

class HaqidaMuloqot extends JDialog {
Odatiy muloqot JDialog klasidan nasl olgan holda yasaladi.
setModalityType(ModalityType.APPLICATION_MODAL);

Bu yerda muloqot turini Modal qilib belgilayapmiz

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Bu yerda yopilish operatsiyasi DISPOSE_ON_CLOSE etib belgilandi. Shu vaqtgacha oynani yopish setDefaultCloseOperation() metodi argumentiga EXIT_ON_CLOSE konstantini berib kelayotgandik, lekin dasturning ishlaydigan muloqot oynalari kabi qo’shimcha oynalarga buni ishlata olmaymiz.

HaqidaMuloqot hm = new HaqidaMuloqot();
hm.setVisible(true);

Bu yerda asosiy oynaning menyusidan muloqot oynasini ko’rsatyapmiz.

XABAR MULOQOTLARI (MessageBox lar)
Habar muloqotlari foydalanuvchini ahborot bilan ta’minlash uchun kerak.
************************************************************************************

[highlighter]

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities;
public class HabarMuloqotlarigaMisol extends JFrame {

private JPanel panel;
public HabarMuloqotlarigaMisol() { initFM(); }

public final void initFM() {

panel = new JPanel();
panel.setLayout((new GridLayout(2,2,5,5)));
JButton hatolikTugma = new JButton("Hatolik");
JButton diqqatTugma = new JButton("Diqqat"); 
JButton sorovTugma = new JButton("So'rov"); 
JButton habarTugma = new JButton("Habar");
hatolikTugma.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 JOptionPane.showMessageDialog(panel, "Faylni ochib bo'lmadi", "Hatolik", JOptionPane.ERROR_MESSAGE); } });
diqqatTugma.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent event) {
  JOptionPane.showMessageDialog(panel, "Diqqat! Ogoh bo'ling.", "Diqqat", JOptionPane.WARNING_MESSAGE); } });
sorovTugma.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent event) {
 JOptionPane.showMessageDialog(panel, "Dasturdan chiqishga ishonchingiz komilmi?", "So'rov", JOptionPane.QUESTION_MESSAGE); } });habarTugma.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(panel, "Yuklanish tugallandi", "Ahborot", JOptionPane.INFORMATION_MESSAGE); 
  } 
});
panel.add(hatolikTugma); panel.add(diqqatTugma);
panel.add(sorovTugma); 
panel.add(habarTugma);
add(panel);
setTitle("Habar muloqotlari");
setSize(200, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE); 
}
public static void main(String[] args) 
{
  SwingUtilities.invokeLater(new Runnable()
    { 
       public void run() {
            HabarMuloqotlarigaMisol hm = new HabarMuloqotlarigaMisol(); hm.setVisible(true);
              }
         });
     }
 }

************************************************************************************
Ushbu misol hatoli, so’rov, ogohlantiruvchi va habat muloqot habarlarini ko’rsatadi.

panel.setLayout((new GridLayout(2,2,5,5)));

Biz muloqot habarlarini chiqaradigan tugmalarni joylashtirishda GridLayout dan foydalandik. U 2 ta ustun, 2 ta qator dan tarkib topadi va har bir komponent orasiga 5 pikseldan bo’sh joy qo’yadi.

JButton hatolikTugma = new JButton("Hatolik");
JButton diqqatTugma = new JButton("Diqqat");
JButton sorovTugma = new JButton("So'rov");
JButton habarTugma = new JButton("Habar");

Bu yerda o’zimizga ishlatish uchun 4 ta tugma yasab oldik.

JOptionPane.showMessageDialog(panel, "Faylni ochib bo'lmadi",
"Hatolik", JOptionPane.ERROR_MESSAGE);

Muloqot xabari yasash uchun, JOptionPane klasining showMessageDialog statik metodi ishlatiladi. Uning argumentlariga biz komponent nomi, habar matni, sarlavhasi va habar turini ko’rsatdik. Habar turi biz bergan konstant bilan tayinlanadi. Biz quyidagi konstantlardan foydalanishimiz mumkin:
* ERROR_MESSAGE
* WARNING_MESSAGE
* QUESTION_MESSAGE
* INFORMATION_MESSAGE

JFILECHOOSER (FAYL TANLASH MULOQOTI)
JFileChooser tizimdan fayl tanlashning standart muloqoqotidir.
*******************************************************************

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FaylTanlashMuloqoti extends JFrame {
private JPanel panel;
private JTextArea matnMaydoni;public FaylTanlashMuloqoti() {
initFM();
}
public final void initFM() {panel = new JPanel();
panel.setLayout(new BorderLayout());
ImageIcon ikon = new ImageIcon("ochish.png");
JToolBar uskunaOrni = new JToolBar();
JButton ochishTugmasi = new JButton(ikon);
ochishTugmasi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
JFileChooser faylOchish = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("matn fayllar",
"txt");
faylOchish.addChoosableFileFilter(filter);

int natija = faylOchish.showDialog(panel, "Faylni Ochish");if (natija == JFileChooser.APPROVE_OPTION) {
File fayl = faylOchish.getSelectedFile();
String matn = faylniOqish(fayl);
matnMaydoni.setText(matn);
}
}
});
uskunaOrni.add(ochishTugmasi);
matnMaydoni = new JTextArea();
matnMaydoni.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));JScrollPane matnSurgich = new JScrollPane();
matnSurgich.getViewport().add(matnMaydoni);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(matnSurgich);
add(panel);add(uskunaOrni, BorderLayout.NORTH);
setTitle("Fayl Tanlash Muloqoti");
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public String faylniOqish(File fayl) {
StringBuffer faylBufer = new StringBuffer();
String faylString = null;
Scanner faylOqiyman;
try {
faylOqiyman = new Scanner(fayl);
while (faylOqiyman.hasNextLine()) {
faylBufer.append(faylOqiyman.nextLine()+"\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
faylString = faylBufer.toString();
return faylString;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FaylTanlashMuloqoti fcd = new FaylTanlashMuloqoti();
fcd.setVisible(true);
}
});
}
}

*******************************************************************
Misolda kod fayl tanlash muloqot oynasidan foydalanib, fayldagi ma’lumotlarni matn maydoni komponentiga yuklash ko’rsatib berilgan.

JFileChooser faylOchish = new JFileChooser();

Bu fayl tanlash muloqot oynasi konstruktori.

FileFilter filter = new FileNameExtensionFilter("matn fayllar", "txt");
faylOchish.addChoosableFileFilter(filter);

Bu yerda fayl filteri yasalgan. Biz txt kengaytmasilik matn fayllari uchun filtr qildik. Bizda yana oddiy hollarda barcha fayllar uchun ham tanlash imkonimiz bo’ladi.

int natija = faylOchish.showDialog(panel, "Faylni Ochish");

Bu yerda fayl tanlash muloqoti ko’rsatilgan. Ochish tugmasi bosilganda,

JFileChooser.APPROVE_OPTION javob qaytadi.

if (natija == JFileChooser.APPROVE_OPTION) {
File fayl = faylOchish.getSelectedFile();
String matn = faylniOqish(fayl);
matnMaydoni.setText(matn);
}

Bu yerda tanlangan fayl nomini olyapmiz. Biz faylni o’qib, matnMaydoni ga joylaymiz.

RANG TANLASH (JColorChooser)
JColorChooser rang tanlash uchun standart muloqot.
*******************************************************************

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class RangTanlashMuloqoti extends JFrame {
private JPanel panel;
private JPanel fonRangi;

public RangTanlashMuloqoti() {
initFM();
}
public final void initFM() {
panel = new JPanel();
panel.setLayout(new BorderLayout());
ImageIcon ochish = new ImageIcon("rang.png");
JToolBar uskunaOrni = new JToolBar();
JButton rangTanlashTugmasi = new JButton(ochish);
rangTanlashTugmasi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
JColorChooser rangTanlash = new JColorChooser();
Color rang = rangTanlash.showDialog(panel, "Rang tanlash",
fonRangi.getBackground());
if (rang != null)
fonRangi.setBackground(rang);
}
});
uskunaOrni.add(rangTanlashTugmasi);
fonRangi = new JPanel();
fonRangi.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createEmptyBorder(10, 8, 10, 8));
panel.add(fonRangi);
add(panel);
add(uskunaOrni, BorderLayout.NORTH);setTitle("Rang tanlash muloqoti");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RangTanlashMuloqoti rtm = new RangTanlashMuloqoti();
rtm.setVisible(true);
}
});
}
}


*******************************************************************
Misolimizdagi panelimizning foni oq rangda. Biz uning foni rangini rang tanlash muloqotidan tanlangan rangga o’zgartiryapmiz.
Bu kod rang tanlash muloqotini chiqaradi.

JColorChooser rangTanlash = new JColorChooser();

Bu yerda Rang tanlash muloqoti, JColorChooser klasidan ob’ekt oldik.

Color rang = rangTanlash.showDialog(panel, "Rang tanlash");

Bu yerda ‘rang’ning qiymatini rangTanlash dan kelgan qiymatga tayinladik.

if (rang != null)
fonRangi.setBackground(rang);

Agar ‘cancel’ bosilmasa, ya’ni biror bir rang tanlansa, o’sha tanlangan rangni fonRangi komponentining foniga rang qilib berdik.

Java Swing ning bu qismida biz muloqotlar bilan tanishdik.

(280 marta o'qilgan, bugun 1 marta o'qildi)

O'xshash maqolalar: