import javax.swing.*;
public class RasmliTugma{
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Icon icon = new ImageIcon("search.png");
JButton button = new JButton("OK", icon);
panel.add(button);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
}
Ushbu dastur quyidagi oynani ekranga chiqarib beradi:
JButton
klassi yordamida yaratilgan rasmli tugma
Java dasturlash tilida tugma bosilganda
ActionEvent
xodisasi xosil qilinadi. Ushbu
xodisani
qabul qilish uchun dastur xodisalarni nazorat qiluvchi
ActionListener
interfeysini
bajarishi va
uning
actionPerformed()
metodini qayta yaratishi kerak bo’ladi.
actionPerformed()
metodi
ichiga tugma bosilganda bajarilishi kerak bo’ladigan amallar kiritiladi. Masalan:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public
class TugmaAmali
{
JPanel panel;
Color backgroundColor = Color.YELLOW;
}
}
}
Yuqoridagi dastur tugma bosilganda oyna rangi sariq rangdan qizil rangga o’zgaradi.
Tugma xosil qilgan xodisa asosida amal bajarish
Dostları ilə paylaş: