yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java GUI Lesson 6 | Menus


3m read
·Nov 3, 2024

Hey, this is Mack has 101. I'm Jake. Welcome to your sixth Java tutorial, and this is what I'll be talking about: menus.

So, like these kinds of things, like a file menu. It's just gonna have three colors: red, blue, and yellow. When you click one, it turns everything yellow. When you click one, it turns the background to either blue or red. So I'm gonna make this extend JFrame. The first thing we have to make is a JMenuBar, which is like this bar that has all our menus in it. I'll call that bar, and yeah, just call the menu "menus."

We're only gonna have one menu, and that's gonna be like a menu. Then, all these are things in the menu, so private JRadioButtonMenuItem, and I'll call that red. I know that's a long name, but it's a menu item, and a radio button means only one can be selected at a time because we only want one color to be selected at a time. We're only changing it to like red, blue, or yellow. So that's what the radio part means; it means that only one can be selected.

I'm just gonna copy and paste this and make blue and green. Alright, so now I'm gonna make the constructor. In our constructor, I'm just gonna set bar equal to new JMenuBar. Then, I'll pass in the menu equals new JMenu, and I'll pass in colors equals new JMenu.

I'm just gonna copy and paste this little phrase here because it's a lot to write or type, and I'll call it red and green. I was gonna make it yellow, but why not make it green? Doesn't really matter. Then setLayout; there's no need to use absolute positioning. We only have one thing in here, that is the menu bar. SetSize: I'll make it 300 by 250, and I'll set the title as "Colors."

Alright, so now what I want to do is bar.add(menu) so that I've added the menu to the menu bar. Menu.add(red), so that's red; that item is added to that menu. Menu.add(blue), and then menu.add(green). Now, in order to make it so you can only select one at once, we make what's called a ButtonGroup. So that means you can only select one.

I want to create a button group: ButtonGroup group equals new ButtonGroup. All I have to do is group.add(red), group.add(blue), and group.add(green). So I did all that. Let me just see if I missed anything. I have to actually add the menu bar, or else nothing here is gonna show up.

Alright, so now I'm gonna implement ActionListener. Okay, so then I'm gonna have red.addActionListener. This is gonna wait for our event to happen, and then when it happens, it's gonna change the color of the background based on the event. I talked about event handling in a previous tutorial, at least with the ActionListener, and now we have to implement that method: public void actionPerformed(ActionEvent E).

In here, I'm just gonna say if (e.getSource() == red), setBackgroundColor(Color.RED), which means if the location of the event is red, as in, if red is selected, make the background color red, and I'll do the same for blue and green. Color.BLUE in all caps just means blue.

So, alright, so there we go. I'm just gonna run this, and you'll see your colors. When I click red, you know the background's red. I click blue, it's blue. That's like green; it's green. As you can see, only one can be selected at a time because I grouped them together like that.

So, are you changing the color with this colors found menu? When you click it, it exits. So basically, I'll just review really quickly what I did, which is I made a menu bar so I can store menus. I made a menu to put in there and I put items in there which are red, blue, and green. I made this group so that only one can be selected at a time, and I added to all three of those items the ActionListener so that it can see if red is selected. It knows to set the background color red.

So, yeah, thank you for watching Mack heads 101. Subscribe, and goodbye!

More Articles

View All
How I Plan a Productive Week in Under 10 Minutes with Amplenote
Hey everyone, in this video I want to show you my system of planning a productive day and week using Android. [Music] So in my YouTube channel, I talked about many note-taking apps like Obsidian, Remnant, and Rome. But in this video, I want to talk about…
Lo-Fi Khan Beats to Study/Relax to
Oh, [Music] [Music] [Music] [Music] w [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Laughter] [Music] [Music] [Music] [Music] [Music] y [Music] [Applause] [Music] [Music] [Laughter] [Music] [Laughter] [Music] [Applause] [Music] [Applaus…
AP US history long essay example 2 | US History | Khan Academy
So we’re talking about the long essay section on the AP U.S. History exam. In the first video on this essay, we talked about kind of general strategy for how to approach the essay. You’ve got 35 minutes to write it, so I recommend you spend five to ten mi…
How a broken, screwed-up life can be beautiful (Kintsugi)
Imagine having a beautiful vase decorating your living room. And it’s not just a vase; it’s a genuine nineteenth-century, hand-painted piece of porcelain created in the Satsuma province in Japan. One day, your neighbor’s dog sneaks into your garden, walks…
Parallel resistors (part 3) | Circuit analysis | Electrical engineering | Khan Academy
In this video, we’re going to talk even some more about parallel resistors. Parallel resistors are resistors that are connected end to end and share the same nodes. Here’s R1 and R2; they share the same nodes, that one and that one, and that means they sh…
Hinduism Introduction: Core ideas of Brahman, Atman, Samsara and Moksha | History | Khan Academy
We’re now going to talk about Hinduism, which is one of the largest religions on Earth, practiced by over a billion people. It’s interesting for several reasons. First, it is considered to be one of the oldest religions that is still practiced. Some histo…