Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

Java Program using JRadioButton with ActionListener in Java Swing

 


GUI
package Trial;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

class Demo extends JFrame implements ActionListener {

	JButton b1, exitbtn;
	JRadioButton r1, r2;

	Demo() {

		setTitle("Hancie e-Learning Studio");
		setLayout(null);
		setSize(600, 400);
		setLocationRelativeTo(null);

		r1 = new JRadioButton("Male");
		r1.setBounds(30, 20, 90, 40);
		add(r1);

		r2 = new JRadioButton("Female");
		r2.setBounds(130, 20, 90, 40);
		add(r2);

		ButtonGroup bg = new ButtonGroup();
		bg.add(r1);
		bg.add(r2);

		b1 = new JButton("Check");
		b1.setBounds(30, 130, 100, 40);
		add(b1);
		b1.addActionListener(this);

		exitbtn = new JButton("Exit");
		exitbtn.setBounds(150, 130, 100, 40);
		add(exitbtn);
		exitbtn.addActionListener(this);

		setVisible(true);
	}

	public void actionPerformed(ActionEvent ev) {
		if (ev.getSource() == (b1)) {
			if (r1.isSelected()) {
				JOptionPane.showMessageDialog(this, "Male is Selected");
			}

			if (r2.isSelected()) {
				JOptionPane.showMessageDialog(this, "Female is Selected");
			}
		}

		if (ev.getSource() == exitbtn) {
			System.exit(0);
		}

	}

	public static void main(String args[]) {
		new Demo();
	}
}
Output
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.