Bir radyo düğmesi oluşturmak için JRadioButton sınıfını kullanıyoruz. Radyo düğmesi, birden fazla seçenek arasından bir seçeneği seçmek için kullanılır. Formların doldurulmasında, çevrimiçi objektif formlarda ve bilgi yarışmasında kullanılır.
Aynı anda yalnızca bir radyo düğmesi seçebilmemiz için bir ButtonGroup’a radyo düğmeleri ekleriz. Bir ButtonGroup oluşturmak ve bir gruba radyo düğmesi eklemek için “ButtonGroup” sınıfını kullanıyoruz.
Kullanılan Nesneler:
JRadioButton(): Metin içermeyen, seçilmemiş bir RadioButton oluşturur.
Örnek:
1 2 3 | JRadioButton j1 = new JRadioButton() |
JButton(String s) : Belirli bir metinle bir JButton oluşturur.
Örnek:
1 2 3 | JButton b1 = new JButton("Button") |
JLabel(String s) : Belirli bir metinle bir JLabel oluşturur.
Örnek:
1 2 3 | JLabel L = new JLabel("Label 1") |
ButtonGroup() : JRadioButton ekleyebileceğimiz bir grup oluşturmak için kullanın. Bir ButtonGroup içinde yalnızca bir JRadioButton seçebiliriz.
Radyo düğmelerini birlikte Gruplandırma Adımları.
ButtonGroup() Metodunu kullanarak bir ButtonGroup örneği oluşturun.
1 2 3 | ButtonGroup G = new ButtonGroup() |
Şimdi add() Metodu yardımıyla G Grubuna butonları ekleyin.
Örnek:
1 2 3 4 5 6 | ButtonGroup G = new ButtonGroup(); G.add(Button1); G.add(Button2); |
isSelected() : bir JRadioButton seçilirse true veya false bir Boolean değeri döndürür, aksi takdirde false döndürür.
Örnek:
1 2 3 | JRadioButton.isSelected() |
Set(…) ve Get(…) Metotları
- Set ve Get, harici sınıflardan doğrudan erişen üye değişkenleri değiştirmek için kullanılır.
- Sınıf üyesi değişkenlere doğrudan erişmek yerine, bu değişkenlere erişmek için get yöntemlerini tanımlar ve bunları değiştirmek için set yöntemini ayarlarsınız.
Kodlar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class JavaOrnekleri extends JFrame { JRadioButton jRadioButton1; JRadioButton jRadioButton2; JButton jButton; ButtonGroup G1; JLabel L1; JFrame frame; public JavaOrnekleri() { //Örnek Frame Oluşturma frame = new JFrame(); frame.setTitle("Pencere"); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setBounds(200,200,400,400); //"JRadioButton" nesnesi oluşturma. jRadioButton1 = new JRadioButton(); // "JRadioButton" nesnesi oluşturma. jRadioButton2 = new JRadioButton(); //"JButton" nesnesi oluşturma. jButton = new JButton("Tıkla"); //"ButtonGroup" nesnesi olutşruma. G1 = new ButtonGroup(); //" JLabel" nesnesi oluşturma. L1 = new JLabel("Nitelik"); jRadioButton1.setText("İyi"); jRadioButton2.setText("Orta"); jRadioButton1.setBounds(120, 30, 120, 50); jRadioButton2.setBounds(250, 30, 80, 50); jButton.setBounds(125, 90, 80, 30); L1.setBounds(20, 30, 150, 50); frame.add(jRadioButton1); frame.add(jRadioButton2); frame.add(jButton); frame.add(L1); G1.add(jRadioButton1); G1.add(jRadioButton2); } } |

JRadioButton ile ActionListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class JavaOrnekleri extends JFrame { JRadioButton jRadioButton1; JRadioButton jRadioButton2; JButton jButton; ButtonGroup G1; JLabel L1; JFrame frame; public JavaOrnekleri() { //Örnek Frame Oluşturma frame = new JFrame(); frame.setTitle("Pencere"); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setBounds(200,200,400,400); //"JRadioButton" nesnesi oluşturma. jRadioButton1 = new JRadioButton(); // "JRadioButton" nesnesi oluşturma. jRadioButton2 = new JRadioButton(); //"JButton" nesnesi oluşturma. jButton = new JButton("Tıkla"); //"ButtonGroup" nesnesi olutşruma. G1 = new ButtonGroup(); //" JLabel" nesnesi oluşturma. L1 = new JLabel("Nitelik"); jRadioButton1.setText("İyi"); jRadioButton2.setText("Orta"); jRadioButton1.setBounds(120, 30, 120, 50); jRadioButton2.setBounds(250, 30, 80, 50); jButton.setBounds(125, 90, 80, 30); L1.setBounds(20, 30, 150, 50); frame.add(jRadioButton1); frame.add(jRadioButton2); frame.add(jButton); frame.add(L1); G1.add(jRadioButton1); G1.add(jRadioButton2); //JButton Listener Oluşturma. jButton.addActionListener(new ActionListener() { // Anonim Sınıf public void actionPerformed(ActionEvent e) { // Override Method // String değişken. String nitelik = " "; // if jRadioButton2 durumu. if (jRadioButton1.isSelected()) { nitelik = "İyi"; } else if (jRadioButton2.isSelected()) { nitelik = "Orta"; } else { nitelik = "Seçim Yok"; } // MessageDialog gösterimi JOptionPane.showMessageDialog(JavaOrnekleri.this, nitelik); } }); } } |

Program 3 Basit bir radyo düğmesi grubu (resimli) oluşturmak ve bunlara Itemlistener eklemek için program kodu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import javax.swing.*; import java.awt.event.*; public class JavaOrnekleri extends JFrame{ JRadioButton jRadioButton1; JRadioButton jRadioButton2; ButtonGroup G1; JLabel L1; JFrame frame; public JavaOrnekleri() { //Örnek Frame Oluşturma frame = new JFrame(); frame.setTitle("Pencere"); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setBounds(200,200,400,400); //"JRadioButton" nesnesi oluşturma. jRadioButton1 = new JRadioButton(); jRadioButton1.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); L1.setText(aButton.getText()); } }); // "JRadioButton" nesnesi oluşturma. jRadioButton2 = new JRadioButton(); jRadioButton2.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent event) { AbstractButton aButton = (AbstractButton) event.getSource(); L1.setText(aButton.getText()); } }); //"ButtonGroup" nesnesi olutşruma. G1 = new ButtonGroup(); //" JLabel" nesnesi oluşturma. L1 = new JLabel("Seçim:"); jRadioButton1.setText("İyi"); jRadioButton2.setText("Orta"); jRadioButton1.setBounds(120, 30, 120, 50); jRadioButton2.setBounds(250, 30, 80, 50); L1.setBounds(20, 30, 150, 50); frame.add(jRadioButton1); frame.add(jRadioButton2); frame.add(L1); G1.add(jRadioButton1); G1.add(jRadioButton2); } } |
Main.java
1 2 3 4 5 6 7 | public class Main{ public static void main(String[] args) { new JavaOrnekleri(); } } |
Yorum yap