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

Java GUI Lesson 1 | Making a window


2m read
·Nov 3, 2024

Hello, this is Jake. This is going to be your first Java GUI tutorial. For those of you who don't know what a GUI is, it's GUI, which stands for graphical user interface. It's like Windows, and you can interact using the graphics and stuff like that, as opposed to the command line, which is what I was teaching before, where you enter commands and it does something based on your input and stuff like that.

So the first thing I have to do is import something. We have to import Java X dot swing, and this is all our windows, buttons, and things like that. So in this tutorial, I'm just going to teach you how to make the window.

In order to do this, we make a J frame, which is a frame. It's a window. That's one kind. F equals new JFrame, empty parameters. So what this is, we just created our window, but obviously we can't just run it and have our window because there are a few things we have to do.

First, we have to set its size and things like that. So F dot set visible, which is very important. This takes a boolean, so it's set to true. Alright, so F dot set visible true. F dot set size, and this takes two parameters. It takes two doubles, which are the width and the height.

I'll make the width 350 and the height 200. So it takes width and height to click the width and height of it. F dot set, this is important, default close operation and JFrame dot exit in all caps, underscore on close. What this does is it's just a really fancy way of saying it closes when we exit out. When we press the X button, it closes. Without this, it would not close when we exit out.

Right, so now we have F dot set title, and we can make a title. This is not mandatory, but it's nice. I'll just call it "Window." Alright, and we can also set a background color, but for that, we have to import java.awt, and that has our colors.

So F dot set background and color with a capital C dot and then your color in all caps, like RED in all caps. It has to be in all caps. Alright, so there we go. I’m going to run this, and I'll create a window.

So I prefer to it. First of all, I created a window, set it visible, set the size, made it so you can exit out, set a title, and set a background. So voila, here it is! We can move it around, resize it, and we can exit out.

If I don't want them to resize it, F dot set resizable, and that takes a boolean, false. It can be true or false. So F dot set resizable false, and I cannot resize it. It's set to true by default though.

So this here is not necessary; it's not necessary to do resizable true because it sets it to true by default. So there you go! And that's making a basic window.

I'll see you next time, and I'll be talking about J labels next time. So see ya!

More Articles

View All
Office Hours with Adora Cheung
Alright, hey everyone! Today we have Dore Chun. She’s a YC partner and co-founder of Homejoy. Thank you! How’s it going? Good, how are you? Good! Alright, we’re gonna do some office hour questions from the internet, so let’s go! Cool! So first question…
How to motivate and engage your kids in learning while at home
Hey everyone, welcome to our webinar! My name is Lauren Kwan, and I’m on the Khan Academy team. Today, I am joined by my co-worker, Dan Tu, and our special guest, Connor Corey. Connor is an expert teacher, a parent, and a Khan Academy ambassador, which me…
Seasons | The Earth-sun-moon system | Middle school Earth and space science | Khan Academy
In this video, we’re gonna think about why we have seasons on Earth like summer and fall and winter and spring. Now, one theory that some folks might have is maybe it’s due to the distance between the Earth and the Sun. We know that the Earth orbits the S…
What Actually Expands In An Expanding Universe?
A portion of this video was sponsored by Salesforce. More about Salesforce at the end of the show. The first piece of evidence that showed our universe is expanding came in the light from distant galaxies. If you look at the spectrum of the sun, you see t…
Michael Burry Is Predicting an Even Bigger Crash.
As you guys probably saw from my video a few weeks ago, Michael Burry, the man that famously predicted the ‘08 housing bubble, is currently predicting another very large recession and stock market crash in 2022 on the back of all the inflation we’re curre…
Watch: Putting a Camera on a Whale Shark | Expedition Raw
I’m out here putting Critter cams on whale sharks and hope to better understand their behavior along the reef. We spot a shark; it was coming up to the boat and actually very curious. I didn’t really realize where the shark was. As soon as I jumped in the…