Slayder elementi berilgan minimal va maksimal qiymatlar orasidan qiymat tanlash imkonini
Ushbu oyna quyidagi dastur yordamida yaratilgan:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderTest
{
JLabel qiymat;
JSlider slider;
public static void main(String[] args)
{
SliderTest st = new SliderTest();
st.go();
}
public void go()
{
JFrame frame = new JFrame("Slider Test");
JPanel panel = new JPanel();
slider = new JSlider(0, 100, 50);
slider.addChangeListener(new sliderChange());
JLabel label = new JLabel("Slayderning joriy qiymati: ");
qiymat = new JLabel("" + slider.getValue());
panel.add(slider);
panel.add(label);
panel.add(qiymat);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,200);
frame.setVisible(true);
}
public class sliderChange implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
qiymat.setText("" + slider.getValue());
}
}
}
Slayder yurgichi xolati o’zgartirilganda
ChangeEvent
xodisasi xosil qilinadi va ushbu
xodisani
ChangeListener
interfeysini bajarib qabul qilish va amal bajarish mumkin. Slayderni
joriy qiymatini olish uchun
JSlider
klassini
getValue()
metodidan foydalanish mumkin.
Spinner elementi qiymat tanlash uchun ishlatilib, Slayder elementidan farqli o’laroq minimal
va maksimal qiymatlari bilan chegaralanmagan. Element yonboshida joylashgan yuqoriga va
pastga qaragan strelkalar bilan Spinner qiymaini oshirish yoki kamaytirish mumkin. Spinner
elementi
JSpinner
klassi yordamida yaratiladi. Quyidagi misolda Spinner joylashgan oyna
keltirilgan:
JSpinner
klassi yordamida yaratilgan spinner elementi
Ushbu oyna quyidagi dastur yordamida yaratilgan:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SpinnerTest{
JLabel qiymat;
JSpinner spinner;
public static void main(String[] args)
{
SpinnerTest st = new SpinnerTest();
st.go();
}
public void go()
{
JFrame frame = new JFrame("Spinner Test");
JPanel panel = new JPanel();
spinner = new JSpinner();
spinner.addChangeListener(new spinnerChange());
JLabel label = new JLabel("Spinnerning joriy qiymati: ");
qiymat = new JLabel("" + spinner.getValue());
panel.add(spinner);
panel.add(label);
panel.add(qiymat);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300,200);
frame.setVisible(true);
}
public class spinnerChange
implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
qiymat.setText("" + spinner.getValue());
}
}
}
Spinner elementi belgilangan elementlar orasidan tanlash imkonini xam beradi. Buning
uchun Spinner konstruktoriga ma’lumotlar modelini uzatish mumkin. Ma’lumotlar modeli
massiv yordamida yaratiladi. Masalan:
String[] qiymatlar = {"sariq","qizil","oq","qora","zangor"};
SpinnerListModel model = new SpinnerListModel(qiymatlar);
JSpinner spinner = new JSpinner(model);
Do'stlaringiz bilan baham: