Chekbox elementi va u xosil qilgan xodisa asosida amal bajarish
Bold yozuviga ega Chekbox elementi tanlanganda “Iltimos, matn xususiyatini tanlang”
matni qalin yozuvga o’zgaradi:
Chekbox elementi va u xosil qilgan xodisa asosida matn ko’rinishini qalinroq qilish
Bold va Italic yozuvlariga ega Checkbox elementlari tanlanganda matn qalin va kursiv
yozuvga o’zgaradi:
Chekbox elementi va u xosil qilgan xodisa asosida matn ko’rinishini qalinroq va kursiv qilish
Checkbox elementi birdaniga bir nechta variantlarni tanlash imkonini berish uchun
ishlatiladi. Bir nechta variantlardan faqat bittasini tanlash uchun Radio tugmalar ishlatiladi. Bir
variant tanlanganda boshqa tanlangan variantlar belgilanishi bekor qilinadi. Buning uchun Radio
tugmalar bir guruhda bo’lishi kerak. Radio tugmalar
JRadioButton
klassi yordamida yaratiladi.
Radio tugmalar guruhi
ButtonGroup
klassi yordamida xosil qilinadi. Quyidagi oynada Radio
tugmalar ko’rsatilgan:
JRadioButton
klassi yordamida yaratilgan radio tugmalar
Ushbu oyna quyidagi dastur yordamida yaratilgan:
import java.awt.*;
import javax.swing.*;
public class RadioTanlash
{
public static void main(String[] args)
{
RadioTanlash rt = new RadioTanlash();
rt.go();
}
public void go()
{
JFrame frame = new JFrame("Radio Tanlash");
JPanel panel = new JPanel( );
ButtonGroup guruh = new ButtonGroup();
JRadioButton rb1 = new JRadioButton("Kichik");
guruh.add(rb1);
JRadioButton rb2 = new JRadioButton("Oddiy");
guruh.add(rb2);
JRadioButton rb3 = new JRadioButton("Katta");
guruh.add(rb3);
JLabel label = new JLabel("Iltimos, matn xususiyatini belgilang");
label.setFont(new Font("Arial", Font.PLAIN, 14));
panel.add(rb1);
panel.add(rb2);
panel.add(rb3);
panel.add(label);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,200);
frame.setVisible(true);
}
}
Radio button element foydalanuvchi tomonidan tanlanganda
ActionEvent
xodisasi xosil
qilinadi va
ActionListener
interfeysi yordamida ushbu xodisani qabul qilib biron bir amal
bajarib olish mumkin. Masalan, quyidagi dastur tegishli Radio tugmasi tanlanganda oynadagi
matn o’lchamini o’zgartirib beradi:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadioTanlash
{
JRadioButton rb1;
JRadioButton rb2;
JRadioButton rb3;
JLabel label;
public static void main(String[] args)
{
RadioTanlash rt = new RadioTanlash();
rt.go();
}
public void go()
{
JFrame frame = new JFrame("Radio Tanlash");
JPanel panel = new JPanel( );
ActionListener Listener = new Listener();
ButtonGroup guruh = new ButtonGroup();
rb1 = new JRadioButton("Kichik");
rb1.addActionListener(Listener);
guruh.add(rb1);
rb2 = new JRadioButton("Oddiy");
rb2.addActionListener(Listener);
guruh.add(rb2);
rb3 = new JRadioButton("Katta");
rb3.addActionListener(Listener);
guruh.add(rb3);
label = new JLabel("Iltimos, matn xususiyatini belgilang");
label.setFont(new Font("Arial", Font.PLAIN, 14));
panel.add(rb1);
panel.add(rb2);
panel.add(rb3);
panel.add(label);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,200);
frame.setVisible(true);
}
public class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(rb1.isSelected()) label.setFont(new Font("Arial", Font.PLAIN, 12));
if(rb2.isSelected()) label.setFont(new Font("Arial", Font.PLAIN, 14));
if(rb3.isSelected()) label.setFont(new Font("Arial", Font.PLAIN, 16));
}
}
}
Ushbu dastur quyidagi oynani ekrangachiqarib beradi:
Radio tugmalar va u xosil qilgan xodisa asosida amal bajarish
Kichik va Katta yozuvlariga ega Radio tugmalari tanlanganda matn o’lchami o’zgarishi
quyidagi oynalarda ko’rsatilgan:
Radio tugma tanlanganda xosil qilingan xodisa asosida matn ko’rinishini o’zgartirish
Guruhda bo’lgan Radio tugmalarning bir vaqtning o’zida faqatgina bittasi tanlangan bo’lishi
mumkin.
Do'stlaringiz bilan baham: |