javaswing

Java Swing-06 Asosiy komponentlar 1

ASOSIY JAVA SWING KOMPONENTLAR
Swing komponentlari FGM dasturi qurishning asosiy vositalaridir. Swing uskunalari juda ko’plab narsalardan tarkib topgan. Jumladan, tugmalar, ”checkBox’lar, aylantirgichlar va h.k. hullas, dasturchiga zarur bo’lgan barcha narsalardan. Bu bo’limda biz bir qancha foydali komponentlar bilan tanishamiz.

JLABEL KOMPONENT
JLabel matn, rasm yoki har ikkalasini birgalikda ko’rsatish uchun ishlatiladi. Bunga FGM dasuri foydalanuvchisi ma’lumot kiritishi uchun emas.
******************************************
[highlighter]

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class LabelgaMisol extends JFrame {

private Toolkit toolkit;

public LabelgaMisol() {
initFM();
}

private void initFM() {
setTitle("JLabel bilan ishlash");

String matn =  "<html><h2>Sarlavha</h2><br>" +
"<b>Birinchi qatorga yozilgan qalin matn</b><br>" +
"<i>Ikkinchi qator qiya matn </i><br>" +
"<u>Uchinchi qator osti chiziqli matni</u><br>" +
"<b><i><u>To'rtinchi qator matni</u></i><b><br>" +
"</html>";

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(10, 10));

JLabel label = new JLabel(matn);
label.setFont(new Font("Georgia", Font.PLAIN, 14));
label.setForeground(new Color(0, 200, 0));

panel.add(label);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(panel);
pack();

toolkit = getToolkit();
Dimension ekranOlchami = toolkit.getScreenSize();
setLocation((ekranOlchami.width - getWidth())/2,
(ekranOlchami.height - getHeight())/2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

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

******************************************
Ushbu misolda JLabel ga matn yozib oynaga chiqardik. JLabel komponentida biz html teglaridan foydalanishimiz mumkin. Biz <h3> dan sarlavhani ajratib ko’rsatish uchun, <br> dan yangi qatorga o’tish uchun, <b> dan yozuvni qalinlashtirish uchun, <i> dan yozuvcni qiyalatish uchun va nihoyat <u> dan yozuv ostiga chizish uchun foydalandik.

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(10, 10));

Biz panel yasab unga BorderLayout boshqaruvchisini chetlaridan 10 piksel chagara tashlab o’rnatdik.

JLabel label = new JLabel(matn);
label.setFont(new Font("Georgia", Font.PLAIN, 14));
label.setForeground(new Color(0, 200, 0));

 

Bu yerda JLabel dan ob’ekt yasab konstruktoriga matnni uzatdik. uning shrifti nomini “Georgia” hamda o’lchamini 14 etib belgiladik. hamda yozuvga rang berdik. Rang berayotganda uning Color klassi konstruktoriga qizil, yashil, ko’k ranglar 0-256 oraliqda istalgancha qo’shiladi.

panel.add(label);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

panelga labelni joyladik. E’tibor bering BorderLayout lik asosga komponent joylashda uning o’rni ko’rsatilmasa u avtomatik BorderLayout ning markazidan o’rin oladi.

add(panel);
pack();

 

panelni oynaga joyladik, hamda oyna o’lchamini imkon qadar kichiklatish uchun pack() metodini ishga soldik.

toolkit = getToolkit();
Dimension ekranOlchami = toolkit.getScreenSize();
setLocation((ekranOlchami.width - getWidth())/2,
(ekranOlchami.height - getHeight())/2);

ekranning markaziy koordinatasini aniqlab, oynani o’sha yerga qo’ydik.

JCHECKBOX
JCheckBox ikki hil holatga tushishi mumkin bo’lgan komponent. Yoniq va o’chiq. Buning o’zini alohida labeli ham bor. Agar checkBox holati checked bo’lsa, unga belgi qo’yilganligini anglatadi. Bu komponentdan boshqa narsalarni o’chirish yoki yoqishda foydalanishimiz mumkin.
***************************************************

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

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CheckBoxgaMisol extends JFrame
implements ActionListener {

private String sarlavha="'JCheckBox'ga misol";

public CheckBoxgaMisol() {
initFM();
}

public final void initFM() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(Box.createRigidArea(new Dimension(15, 20)));

JCheckBox checkbox = new JCheckBox("Sarlavhini ko'rsatish", true);
checkbox.setAlignmentX(JComponent.CENTER_ALIGNMENT);
checkbox.addActionListener(this);
add(checkbox);

setSize(260,110);
setTitle(sarlavha);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent hodisa) {
JCheckBox manba = (JCheckBox) hodisa.getSource();
boolean state = manba.isSelected();

if (state) {
this.setTitle(sarlavha);
} else {
this.setTitle("");
}
}

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

***************************************************
Bizning misolimizdagi kod ‘JCheckBox’ning holatiga ko’ra sarlavhani ko’rsatadi yoki yashiradi.

setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(Box.createRigidArea(new Dimension(15, 20)));

Bu misolda BoxLayout boshqaruvidan foydalanilgan. Checkbox qirg’oqqa yopishib ko’rinmasligi uchun chetlaridan biroz joy tashladik.

JCheckBox checkbox = new JCheckBox("Sarlavhini ko'rsatish", true);

JCheckBox konstruktorida uni matni va holatini ko’rsatdik.

JCheckBox manba = (JCheckBox) hodisa.getSource();
boolean state = manba.isSelected();

if (state) {
this.setTitle(sarlavha);
} else {
this.setTitle("");
}

hodisa ob’ektidan hodisaning  manbasini aniqlab olyapmiz. Biznign holatimizda bu checkBox bo’ladi. checkBox tanlangan yo’qligini tekshirib, agar tanlangan bo’lsa unga sarlavha qo’yamiz, bo’lmasa sarlavhani bo’sh qator etib tayinlaymiz.

(JSlider) JSLIDER (1) sirpanmoq; 2) sezdirmay yurmoq, yashirincha yurmoq)
JSlider komponentidan foydalanib chegaralangan oraliqda dastakni yurgazish orqali kerakli qiymatni tanlashimiz mumkin.

****************************************************************

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class SlidergaMisol extends JFrame {

private JSlider slider;
private JLabel label;

private ImageIcon ovozsiz;
private ImageIcon pastOvoz;
private ImageIcon ortachaOvoz;
private ImageIcon balandOvoz;

public SlidergaMisol() {
rasmlarniYuklash();
initFM();
}

private void rasmlarniYuklash() {
ovozsiz = new ImageIcon("ovozsiz.png");
pastOvoz = new ImageIcon("pastOvoz.png");
ortachaOvoz = new ImageIcon("ortachaOvoz.png");
balandOvoz = new ImageIcon("balandOvoz.png");
}

public final void initFM() {

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(20, 100, 20, 100));
setLayout(new BorderLayout());

slider = new JSlider(SwingConstants.VERTICAL, 0, 100, 0);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(25);
slider.setPaintTicks(true);

slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent hodisa) {
int qiymat = slider.getValue();
if (qiymat == 0) {
label.setIcon(ovozsiz);
} else if (qiymat > 0 && qiymat <= 30) {
label.setIcon(pastOvoz);
} else if (qiymat > 30 && qiymat < 80) {
label.setIcon(ortachaOvoz);
} else {
label.setIcon(balandOvoz);
}
}
});

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

label = new JLabel(ovozsiz, JLabel.CENTER);
panel.add(label);
panel.add(Box.createHorizontalGlue());
add(panel, BorderLayout.CENTER);

pack();

setTitle("JSlider ga misol");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

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

****************************************************************
Misolimizda JSlider va JLabel joylashtirilgan oyna yasadik, JSlider dastagini surish orqali JLabel dagi ikonni almashtirish munkin.

private void rasmlarniYuklash() {
ovozsiz = new ImageIcon("ovozsiz.png");
pastOvoz = new ImageIcon("pastOvoz.png");
ortachaOvoz = new ImageIcon("ortachaOvoz.png");
balandOvoz = new ImageIcon("balandOvoz.png");
}

rasmlarniYuklash() metodida diskdagi rasmlarni ob’ektlarga yuklab oldik.

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panelimizga BoxLayout boshqaruvi tatinlandi.
panel.setBorder(BorderFactory.createEmptyBorder(20, 100, 20, 100));

panel chegaralaridan bo’sh joy tashlandi.

slider = new JSlider(SwingConstants.VERTICAL, 0, 100, 0);

Vertikal, (tik) joylashgan, qiymatlari minum 0, maksimum 100 bo’lgan, dastlab ko’rinishdagi qiymati 0 dan boshlanadigan slayder yasaldi.

slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(25);
slider.setPaintTicks(true);

 

Slayderning o’lchov birliklarining kichigi har 5, kattasi 25 birlikka bittadan qo’yib chiqildi, va u korinarli holga keltirildi.

slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
...
});

slider ga ChangeListener qo’shdik, bu orqali ‘slider’ning joriy qiymatini aniqlab, unga ko’ra ‘label’ dagi rasmni almashtiramiz.

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

 

Ikkala komponent orasiga 10 piksellik qo’zg’almas hudud tashladik.

(JComboBox) JCOMBOBOX
ComboBox tugma, tahrirlanish maydoni va pastga tushuvchi ro’yhat birgalikda ishlatiladigan komponent. Foydalanuvchi pastga tushadigan ro’yhatni ochib, undan kerakli qismni tanlashi mumkin. Agar ComboBox ni tahrirlanish hususiyatini ham yoqsangiz, foydalanuvchi u yerga o’ziga kerakli qiymatni yoza oladigan holga keladi.
****************************************************************************

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class ComboBoxgaMisol extends JDialog implements
ActionListener, ItemListener {

final String[] shakllar = {
"doira", "oval", "romb",
"to'rtburchak", "uchburchak", "yulduz"
};

final String[] rasmlar = {
"doira.jpg", "oval.jpg", "romb.jpg",
"to'rtburchak.jpg", "uchburchak.jpg", "yulduz.jpg"
};

private JLabel rasmOrni = null;
private JComboBox combobox = null;
private JButton tugma = null;

ImageIcon rasm = new ImageIcon("doira.jpg");

public ComboBoxgaMisol() {
setLayout(new BoxLayout(getContentPane(),
BoxLayout.Y_AXIS));
add(Box.createRigidArea(new Dimension(0, 10)));

rasmOrni = new JLabel(rasm);
rasmOrni.setMaximumSize(new Dimension(130, 160));
rasmOrni.setAlignmentX(Component.CENTER_ALIGNMENT);
rasmOrni.setBorder(LineBorder.createGrayLineBorder());
add(rasmOrni);

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

combobox = new JComboBox(shakllar);
combobox.setSelectedIndex(0);
combobox.setMaximumSize(new Dimension(100, 22));
combobox.addItemListener(this);
add(combobox);

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

tugma = new JButton("Close");
tugma.setAlignmentX(Component.CENTER_ALIGNMENT);
tugma.addActionListener(this);
add(tugma);

setTitle("JComboBox");
setSize(180, 290);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

public void itemStateChanged(ItemEvent hodisa) {
if (hodisa.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) hodisa.getSource();
int index = combo.getSelectedIndex();
rasmOrni.setIcon(new ImageIcon(rasmlar[index]));
}
}

public void actionPerformed(ActionEvent e) {
System.exit(0);
}

public static void main(String[] args) {
new ComboBoxgaMisol();
}
}

****************************************************************************
Misolimizda uchta komponent bor, Label, combobox va tugma. Tugma oynani yopadi. Comboboxda oltita shaklning nomi yolzilgan. Shaklning nomini tanlasak, uning rasmi labelda ko’rinadi.

 public class ComboBoxgaMisol extends JDialog implements
ActionListener, ItemListener {
Bu dastur JDialog asosiga qurilgan.
rasmOrni = new JLabel(rasm);
Rasmlarni 'JLabel' ga joylaganmiz.
rasmOrni.setBorder(LineBorder.createGrayLineBorder());

 

Bu ifoda atrofiga chiziq chiqarish uchun yozilgan.

 combobox = new JComboBox(shakllar);
 combobox.setSelectedIndex(0);

JComboBox konstruktoriga String massivi uzatiladi. Bizning misolda unga shakllar uzatildi. Sababi kodimizda ‘shakllar’ String massivdir. setSelectedIndex() metodi argumentida ko’rsatilgan ComboBox da tanlangan holga kelaypdi. Agar argument -1 bo’lsa hech qaysi qism tanlanmagan bo’ladi.

combobox.addItemListener(this);

Biz ‘ComboBox’ga ItemListener qo’shdik. Bu hodisa tutuvchida ‘ComboBox’ning tanlangan indeksini olib, ‘Label’ ga shunga most rasm o’rnatyapmiz. Tanlangan qism rasmlar massiving indeksiga most keladi.

(JProgressBar) JPROGRESSBAR
JProgressBar bu jarayonning holatini ko’rsatish uchun ishlatiladi. Bu harakatlanib turganligidan biz jarayon davom etayotganini bilishimiz mumkin. Buni biz o’z hohishimizga ko’ra vertikal yoki gorizontal qilib yasashimiz mumkin. JProgressBar ning boshlang’ich va minimum qiymatlari 0, maksimum qiymati esa 100 dir.
************************************************************************************

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

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class ProgressBargaMisol extends JFrame {

private ActionListener progressBarHolatiniOzgartirish;
private Timer taymer;
private JProgressBar progressBar;
private JButton tugma;

public ProgressBargaMisol() {
initFM();
}

private void initFM() {
setTitle("JProgressBar");

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
progressBar = new JProgressBar(JProgressBar.VERTICAL,0,100);

panel.add(progressBar);
panel.add(Box.createRigidArea(new Dimension(0, 20)));

tugma = new JButton("Boshlash");

progressBarHolatiniOzgartirish = new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
int qiymat = progressBar.getValue();
if (qiymat > 99) {
taymer.stop();
tugma.setEnabled(false);
return;
}
progressBar.setValue(++qiymat);
}
};

taymer = new Timer(10, progressBarHolatiniOzgartirish);

tugma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
if (taymer.isRunning()) {
taymer.stop();
tugma.setText("Boshlash");

} else {
taymer.start();
tugma.setText("To'htatish");
}
}
});

panel.add(tugma);
add(panel);

pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
ProgressBargaMisol misol = new ProgressBargaMisol();
misol.setVisible(true);
}
});
}
}

************************************************************************************
Misolda progressBar va tugma ko’rinadi. Tugmani chertish orqali jarayonni to’htatish va yurgizish mumkin.

progressBar = new JProgressBar();

Bu yerda biz ProgressBar yasadik, konstruktoriga parametr ko’rsatilmasa, qiymatlari minimum 0, maksimum 100 ni tashkil etadi, hamda ProgressBar  vertikal ko’rinadi.
progressBar ni boshqa konstruktorlari ham bor bo’lib ular:

JProgressBar(int yo'nalish);

JProgressBar.VERTICAL/JProgressBar.HORIZONTAL konstantlarini argumentga uzatib, uning yo’nalishini o’zimiz belgilashimiz mumkin.

JProgressBar(int min, int max);

bu konstruktorda birinchi argumentga minimum, ikkinchisiga maksimum qiymatni uzatsak bo’ladi.

JProgressBar(int orient, int min, int max)

Bunisida yo’nalish, minimum va maksimum qiymatlarini o’zimiz tayinlaymiz.

tugma = new JButton("Boshlash");
Boshlash deb sarlavhalangan tugma yasaldi.
progressBarHolatiniOzgartirish = new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
int qiymat = progressBar.getValue();
if (qiymat > 99) {
taymer.stop();
tugma.setEnabled(false);
return;
}
progressBar.setValue(++qiymat);
}
};

ActionListener progressBarHolatiniOzgartirish deb nomlangan ActionListener yasaldi. Bu hodisa tunglovchish progressBar qiymatini bittadan orttirib boradi. Agar progressBar qiymati 99 dan katta bo’lib ketsa, taymerni to’xtatib, tugmani muzlatib qo’yadi.

taymer = new Timer(10, progressBarHolatiniOzgartirish);

Taymer yasadik. buning konstruktordagi birinchi qiymat vaqt chegarasini (millisekundlarda), ikkinchisi ActionListener ni ifodalaydi. Bu ob’ekt ko’rsatilgan vaqtda ko’rsatilgan ‘ActionListener’ ga murojat qilib, unda aytilgan ishlarni bajaradi. 1000 milisekund 1 soniyaligini hisobga olsak, bizning holatimizga har soniyaning 100 dan 1 ulushida progressBar qiymatiga 1 qo’shilib boryapti.

tugma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent hodisa) {
if (taymer.isRunning()) {
taymer.stop();
tugma.setText("Boshlash");

} else if (!"To'htatish".equals(tugma.getText())) {
taymer.start();
tugma.setText("To'htatish");
}
}
});

‘tugma’ ga ActionListener qo’shdik. Ya’ni tugma bosilganda nima ish bajarilishiga kod yozib chiqdik. Bu yerda Timer dan olingan ob’ektning isRunning() metodi orqali uning ishlayotgan yoki yo’qligini aniqlab, ishlayotgan bo’lsa to’xtatib tugma sarlavhasini “Boshlash”ga o’zgartirdik, ishlamayotgan bo’lsa ishlatdib tugma sarlavhasini  “To’xtatish” ga o’zgartirdik.

createRigidArea bo’sh joy tashlashi, panelga qanday qilib joylashuv boshqaruvi tayinlash va komponent qo’shish to’g’risida avval ham gaplashganmiz

pack() ;

bu metod oyna o’lchamini imkon qadar kichiklashtiradi.

setResizable(false) ;

bu ifoda oyna (JFrame ob’ekti) o’lchamini o’zgarmas holga keltiradi.

(JToggleButton) JTOGGLEBUTTON
***********************************************************

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class ToggleButtongaMisol extends JDialog
implements ActionListener {

private JToggleButton qizilTugma;
private JToggleButton yashilTugma;
private JToggleButton kokTugma;
private JPanel ekranimiz;

public ToggleButtongaMisol() {
initFM();
}

private void initFM() {
JPanel asosiyPanel = new JPanel();
asosiyPanel.setLayout(new BoxLayout(asosiyPanel, BoxLayout.X_AXIS));
asosiyPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

JPanel chapTomon = new JPanel();
chapTomon.setLayout(new BoxLayout(chapTomon, BoxLayout.Y_AXIS));

qizilTugma = new JToggleButton("Qizil");
qizilTugma.addActionListener(this);

yashilTugma = new JToggleButton("Yashil");
yashilTugma.addActionListener(this);

kokTugma = new JToggleButton("Ko'k");
kokTugma.addActionListener(this);

kokTugma.setMaximumSize(yashilTugma.getMaximumSize());
qizilTugma.setMaximumSize(yashilTugma.getMaximumSize());

chapTomon.add(qizilTugma);
chapTomon.add(Box.createRigidArea(new Dimension(25, 7)));
chapTomon.add(yashilTugma);
chapTomon.add(Box.createRigidArea(new Dimension(25, 7)));
chapTomon.add(kokTugma);

asosiyPanel.add(chapTomon);
asosiyPanel.add(Box.createRigidArea(new Dimension(20, 0)));

ekranimiz = new JPanel();
ekranimiz.setPreferredSize(new Dimension(110, 110));
ekranimiz.setBorder(LineBorder.createGrayLineBorder());
ekranimiz.setBackground(Color.black);

asosiyPanel.add(ekranimiz);
add(asosiyPanel);

setTitle("'JToggleButton'ga misol");
pack();
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}

public void actionPerformed(ActionEvent hodisa) {
Color rang = ekranimiz.getBackground();
int qizil = rang.getRed();
int yashil = rang.getGreen();
int kok = rang.getBlue();

if (hodisa.getActionCommand().equals("Qizil")) {
if (qizil == 0) {
qizil = 255;
} else {
qizil = 0;
}
}

if (hodisa.getActionCommand().equals("Yashil")) {
if (yashil == 0) {
yashil = 255;
} else {
yashil = 0;
}
}

if (hodisa.getActionCommand().equals("Ko'k")) {
if (kok == 0) {
kok = 255;
} else {
kok = 0;
}
}

Color ornatilganRang = new Color(qizil, yashil, kok);
ekranimiz.setBackground(ornatilganRang);
}

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

***********************************************************
Bu misoldagi uchta tugma va uchta panel bor. Asosiy panel boshqalari joylashuvini tashkillashtirishga hizmati qiladi. Chap panelga uchta tugma qo’shilgach, uning o’zi asosiy panelga o’rnatilgan. So’ngra esa uning yoniga qora fonli ‘ekran’ paneli ham joylashtirilgan. Tugmalarga, Qizil, Yashil, Ko’k deb yozilgan. Ularni bosib qo’yish orqali ko’rsatilgan rang ‘ekran’ panelga qo’shiladi. Ko’tarib qo’yish orqali esa u rang paneldan olib tashlanadi.

qizilTugma = new JToggleButton("Qizil");
qizilTugma.addActionListener(this);

Bu yerda ToggleButton yasab unga ActionListener tayinladik.

kokTugma.setMaximumSize(yashilTugma.getMaximumSize());
qizilTugma.setMaximumSize(yashilTugma.getMaximumSize());

barcha tugmalar o’lchamini bir-hil holatga keltirdik.

Color rang = ekranimiz.getBackground();
int qizil = rang.getRed();
int yashil = rang.getGreen();
int kok = rang.getBlue();

actionPerformed() metodida biz ‘ekranimiz’ paneli orqa fonidagi (RGB) qizil, yashil, va ko’k tarkiblarni aniqlab ularni o’zgaruvchilarga berib olyapmiz.

if (hodisa.getActionCommand().equals("Qizil")) {
if (qizil == 0) {
qizil = 255;
} else {
qizil = 0;
}
}

Qaysi tugma bosilganligini tekshirib, unga ko’ra RGB qiymatni o’zgartirdik.

Color ornatilganRang = new Color(qizil, yashil, kok);
ekranimiz.setBackground(ornatilganRang);

Bu yerda yangi rang yasalib, ‘ekranimiz’ fonining rangiga o’sha rang o’rnatilgan. (Ya’ni tugmalar holatidan kelgan rang).

Bu qismda biz Java Swing ning asosiy komponentlari bilan tanishdik.

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

O'xshash maqolalar: