Java radio buttons 🔘
Vložit
- čas přidán 23. 08. 2020
- Java radio button JRadioButton GUI swing tutorial for beginners
#Java #radio #JRadioButton #button #GUI #swing #tutorial #beginners
// ***********************************************************************
public class Main{
public static void main(String[] args) {
// JRadioButton = One or more buttons in a grouping in which only 1 may be selected per group
new MyFrame();
}
}
// ***********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener{
JRadioButton pizzaButton;
JRadioButton hamburgerButton;
JRadioButton hotdogButton;
ImageIcon pizzaIcon;
ImageIcon hamburgerIcon;
ImageIcon hotdogIcon;
MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
//pizzaIcon = new ImageIcon("pizza.png");
//hamburgerIcon = new ImageIcon("hamburger.png");
//hotdogIcon = new ImageIcon("hotdog.png");
pizzaButton = new JRadioButton("pizza");
hamburgerButton = new JRadioButton("hamburger");
hotdogButton = new JRadioButton("hotdog");
ButtonGroup group = new ButtonGroup();
group.add(pizzaButton);
group.add(hamburgerButton);
group.add(hotdogButton);
pizzaButton.addActionListener(this);
hamburgerButton.addActionListener(this);
hotdogButton.addActionListener(this);
//pizzaButton.setIcon(pizzaIcon);
//hamburgerButton.setIcon(hamburgerIcon);
//hotdogButton.setIcon(hotdogIcon);
this.add(pizzaButton);
this.add(hamburgerButton);
this.add(hotdogButton);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==pizzaButton) {
System.out.println("You ordered pizza!");
}
else if(e.getSource()==hamburgerButton) {
System.out.println("You ordered a hamburger!");
}
else if(e.getSource()==hotdogButton) {
System.out.println("You ordered a hotdog!");
}
}
} - Věda a technologie
// ***********************************************************************
public class Main{
public static void main(String[] args) {
// JRadioButton = One or more buttons in a grouping in which only 1 may be selected per group
new MyFrame();
}
}
// ***********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener{
JRadioButton pizzaButton;
JRadioButton hamburgerButton;
JRadioButton hotdogButton;
//ImageIcon pizzaIcon;
//ImageIcon hamburgerIcon;
//ImageIcon hotdogIcon;
MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
//pizzaIcon = new ImageIcon("pizza.png");
//hamburgerIcon = new ImageIcon("hamburger.png");
//hotdogIcon = new ImageIcon("hotdog.png");
pizzaButton = new JRadioButton("pizza");
hamburgerButton = new JRadioButton("hamburger");
hotdogButton = new JRadioButton("hotdog");
ButtonGroup group = new ButtonGroup();
group.add(pizzaButton);
group.add(hamburgerButton);
group.add(hotdogButton);
pizzaButton.addActionListener(this);
hamburgerButton.addActionListener(this);
hotdogButton.addActionListener(this);
//pizzaButton.setIcon(pizzaIcon);
//hamburgerButton.setIcon(hamburgerIcon);
//hotdogButton.setIcon(hotdogIcon);
this.add(pizzaButton);
this.add(hamburgerButton);
this.add(hotdogButton);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==pizzaButton) {
System.out.println("You ordered pizza!");
}
else if(e.getSource()==hamburgerButton) {
System.out.println("You ordered a hamburger!");
}
else if(e.getSource()==hotdogButton) {
System.out.println("You ordered a hotdog!");
}
}
}
Thanks bro! I feel these videos are the type where they get recommend to everyone in like 10 years from now.
Bro, I've been going through your Java GUI series. Your explanations are easy to understand and I appreciate the creative tips. It's just enough to launch your imagination in implementation rather than resulting in dated looking GUIs. Thank you
It's nice to have such an amazing people that share their knowledge. Thanks a lot!
OMG, I am so grateful I found your channel! Thank you!
Cool and clear Tutorial!
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
So far, this course is amazing
You're a savior man. Never stop making videos
thank you for watching Rick! I don't plan on stopping 👍
good job, Bro! it's really simple to understand your tutorials.
Thanks bro! this course is easy to learn.
thanks bro. man this channel helps me so much
Okay! Cool, it helps me/ Thanks
Nice video
another great vid
thank you for the content
its just funny how I haven't had hiccups since I started this series, almost 8 hours in
thank you!
Liked commemted subscribed
I was sitting here hopeless thinking I’m failing my exam today this man just saved me and my cousin
very good video
you are a legend bro
Thank you bro
Thanks, Bro
nice
i ordered them all
🔥🙌
thanks bro
Thanks
Oh my god “bro code” I get it
What about resizing the radio buttons, I know you can resize the text with setfont but if you do so, the radio buttons would stay the same size originally
Bro I'm having a problem with icons.
Can you please tell me from where do you download icon.
My downloaded icon sizes are too large so they don't fit with my text
I am watching
im trynna wherein sum text just only gonna pop up in the console if you click the submit button(JButton) after you choose an item in a JRadioButton form. is it possible? i dont seem to know.
I have problem set RadioIcon. Would it be possible to share your image icon in here. So I can test if it's related to image size issue.
BRO, I add the images just as you did, I made them small enough (40x40) and they for some reason just don't show... this is driving me crazy!!
Can I use all types of images in java like jpg, jpeg, png, etc?
These 3 are supported ones, yeah (I think you already figured it out, so sorry for the late response).
Hi have one question suppose u have to enter some field after clicking radio btn exa:if u click female radio btn u enter name but now u change ur mind and select other btn now the value entered in the female btn i. e., name is not cleared which causes form to not submit so how to clear radio btn field values once u select other btn please please try to clear my question
Hoping positive response🙂
it wont work to me ; and how i wright the same code but all in the main !!
i really hope u answer me soon bc i have an exem in that and i can't wright my project conversion programme
Bro how to indent the button?
1.9K ...🐌 2K
pls help my activity
Using JFrame and 3 RadioButton.Create a java application that will allow user to draw and choose the color they want to use.
😥
im first year student
can i get a source code ?
youtube algorithm comment
Hi ;(} parent ; don't laugh at me:(). I c u ll why the hate child where I am you were, where you are I will be. (Hint) we all bleed red. Do not let knowledge consume u pray for crk
Practicing...
public class Main{
public static void main(String[] args){
new MyFrame();
}
}
**********************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener
{
JRadioButton funkButton;
JRadioButton soulButton;
JRadioButton reggaeButton;
ImageIcon funkIcon;
ImageIcon soulIcon;
ImageIcon reggaeIcon;
MyFrame ()
{
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setLayout (new FlowLayout ());
funkIcon = new ImageIcon("funk.png");
soulIcon = new ImageIcon("soul.png");
reggaeIcon = new ImageIcon("reggae.png");
funkButton = new JRadioButton("funk");
soulButton = new JRadioButton("soul");
reggaeButton = new JRadioButton("reggae");
ButtonGroup group = new ButtonGroup();
group.add(funkButton);
group.add(soulButton);
group.add(reggaeButton);
funkButton.addActionListener(this);
soulButton.addActionListener(this);
reggaeButton.addActionListener(this);
funkButton.setIcon(funkIcon);
soulButton.setIcon(soulIcon);
reggaeButton.setIcon(reggaeIcon);
this.add(funkButton);
this.add(soulButton);
this.add(reggaeButton);
this.pack ();
this.setVisible (true);
}
@Override
public void actionPerformed (ActionEvent e){
if(e.getSource()==funkButton){
System.out.println("Play me some funk music.");
}
else if(e.getSource()==soulButton){
System.out.println("Play me some soul music.");
}
else if (e.getSource()==reggaeButton){
System.out.println("Play me some reggae music.");
}
}
}
it wont work to me
and how i wright the same code but all in the main !!
i really hope u answer me soon bc i have an exem in that and i can't wright my project conversion programme
in the main function you cannot use "this" for the JFrame. You need to create a new JFrame ("JFrame myFrame"), ("myFrame=new JFrame"). Then instead of "this", you write "myFrame".