0:01 hey what's going on everybody it's your
0:03 bro here hope you're doing well
0:05 this is my new and improved java swing
0:07 tutorial series and in this video i'm
0:09 going to teach you guys how we can
0:10 create a
0:12 simple jframe that we can later add
0:13 components to
0:19 if you find this video helpful please
0:21 remember to like
0:23 comment and subscribe your support will
0:25 help keep this channel running
0:28 okay everybody so let's create a jframe
0:31 so a jframe is a gui window
0:33 to add components to we're not going to
0:35 be adding any components in this video
0:36 but in the next video we're going to cover
0:36 cover
0:39 labels so to begin let's create an
0:41 instance of a jframe
0:44 so jframe let's call this frame equals
0:48 new j frame so this
0:51 creates a frame for us an instance
0:54 of a frame however we'll need an import
0:55 so we'll add that at the top
0:59 import java x dot swing dot j frame
1:01 so if we were to run this it's actually
1:02 not visible
1:05 so we need to set the visibility to true
1:07 so we'll add that the end
1:10 frame dot set visible
1:14 and we're going to set this to true
1:17 so this will make frame
1:20 visible so we can actually see it now
1:24 however it is very small so
1:26 let's increase the size so we can do
1:27 that by saying
1:30 the name of the frame dot set
1:34 size so we can pass in a dimension
1:37 or a width and a height and this is in integers
1:38 integers
1:40 so let's say this frame will be 420 by
1:43 420 because 420 is a funny number
1:47 so this sets the
1:50 x dimension and
1:55 y dimension of our frame
1:58 and this should be visible now here's
1:59 our jframe
2:01 let's set some other things let's change
2:04 the title so we can do that by saying
2:07 frame dot set title and we can set this to
2:08 to
2:11 a string i'll call this
2:15 j frame title goes
2:19 here and this will display at the top
2:21 so set this to whatever you want your
2:24 title for your window to be
2:27 so this sets title of
2:30 frame now pay attention to this
2:33 so when we hit this x button in the
2:35 corner it's going to actually
2:37 hide our frame but not actually close
2:39 out of the program
2:41 so if you want to close out of the
2:42 program and exit
2:44 we're going to change a setting we're
2:46 going to use
2:49 frame dot set default close
2:51 operation and we're going to set this to jframe
2:52 jframe
2:55 dot exit on close
2:57 so when we hit that x button it's going
2:59 to exit out of
3:03 application and by default it's normally
3:05 hide on close so it appears that we
3:07 killed the application but it's still
3:09 running in the background
3:13 otherwise there is do nothing on close
3:15 and you can actually prevent somebody
3:17 from hitting the x button to close out
3:19 of the frame which is actually kind of
3:20 annoying but
3:21 that's how you can do that but let's set
3:24 this back to exit on close because we
3:26 want to exit out of the application
3:29 when we hit that x button now another
3:30 thing that we can do
3:34 is that we can resize our window
3:36 so we can actually prevent that so we're
3:37 going to say
3:41 frame dot set
3:45 resizable false
3:48 what this will do is prevent
3:52 frame from being resized
3:57 so we can no longer resize this even if
3:58 we tried
4:00 we can no longer make this full screen
4:02 either with the maximize button
4:04 so here's something else for you guys
4:05 how do we change the icon
4:08 for the top left of our frame window
4:10 normally it's the
4:12 java logo but we can actually change
4:13 that to something that suits our program better
4:14 better
4:16 so i'm actually going to use the logo
4:17 for my channel
4:20 and change the icon from the java logo
4:24 to my logo so if you want feel free to
4:25 pause the video and download an image
4:27 that you might want to use
4:29 here's my logo i'm going to change the
4:31 icon of my frame
4:33 to this logo instead of the default java logo
4:34 logo
4:37 so what i'm going to do is copy this go
4:38 to my project folder
4:42 and then paste it so then we're going to
4:42 create an
4:46 image icon out of this so image
4:48 then icon let's call this image just image
4:49 image
4:53 equals new image icon
4:55 and then we're going to list either the
4:57 file path or the file name
5:00 since this is within my project folder i
5:01 only have to list
5:04 the file name so this file name is logo.png
5:11 logo.png and then we're going to need
5:12 another import
5:15 so java x dot swing dot image icon at
5:16 the top
5:20 so this will create an image
5:23 icon and now what we're going to do
5:27 is set the image icon of the frame
5:30 so we'll say frame dot
5:33 set icon image
5:35 and within the parentheses of this
5:37 method here we're going to say image
5:41 dot get image
5:45 so this will change icon
5:48 of frame so then
5:52 if we tried this our image is now
5:54 at the top left of our frame in place of
5:57 the java logo that was there by default
6:00 pretty cool huh now let's change the
6:02 background color of our frame
6:05 so what we're going to do is that we're
6:06 going to say
6:10 frame dot get
6:13 content pain
6:14 we're going to have to do a little bit
6:17 of method chaining followed by
6:21 dot set background
6:23 and then you can place a color within
6:26 here so there's a few basic colors
6:33 and then we're going to need an import
6:35 as well so make sure to include this at
6:37 the top
6:39 java.awt.color so this will
6:44 change color of background
6:47 so now this window should be green
6:49 however you might not like some of the
6:51 default colors you can create a custom
6:52 color too
6:56 so within the set background method
6:59 we're going to say new
7:02 color and we have a few options we can place
7:03 place
7:06 some rgb values here or a hexadecimal
7:07 color value
7:10 so let's begin with some rgb values so all
7:10 all
7:15 zeros this is black
7:17 so the background is now black and this
7:19 is on the range from 0 to
7:22 255 with
7:25 255 for all three values is
7:28 just white so the first value is the
7:35 and you can see it's as bright red as
7:37 you can make it really
7:42 the second value is green so that's 255
7:44 and remember this is on a range of 0 to 255
7:46 255
7:54 so you can adjust the amount of red
7:56 green and blue that you want
7:58 so i'm just going to make up some random
8:00 numbers 123
8:03 50 250.
8:06 i have no idea what color this is so oh
8:07 i actually
8:09 really like that that's like a twitch purple
8:10 purple
8:12 so to say i think i'm keeping that
8:14 actually i'm going to remember this number
8:14 number
8:18 123 50 250. all right you can also place a
8:18 a
8:22 hexadecimal value here so you say
8:25 0x and then it's a
8:28 series of six values
8:32 so all zeroes would be black otherwise
8:33 all f's
8:35 would be white and if you don't believe
8:38 me i'll prove you wrong
8:40 so you can always go to google and look
8:42 up hexadecimal
8:43 color values and you can pick a color
8:45 that you want so
8:49 1 2 3 4 5 6 would be
8:52 this shade of blue which is actually
8:54 pretty sweet uh
8:56 otherwise you can really pick any color
8:57 you want and put that here
8:58 and the last thing that i got for you
9:01 guys today in this video is that
9:03 there are two i would say different ways
9:05 in which you can create a frame the
9:07 first is just to create an instance
9:09 of a frame and give it a name and then
9:10 you can change
9:12 all of the members and properties of
9:14 this frame by just saying the name of
9:15 the frame
9:17 dot and then whatever change you want to
9:18 make the other way
9:21 is that you can use a jframe as a parent class
9:22 class
9:24 to a child class so i'll show you how to
9:26 do that because i do this quite often in
9:28 this tutorial series
9:30 so what we're going to do is actually
9:32 create another class that's going to be
9:32 a child
9:35 class of jframe so go to your source folder
9:36 folder
9:39 then go to file new class and i'll call this
9:40 this
9:44 my frame so my frame
9:48 extends j frame this is the subclass
9:50 or child class and jframe is the parent class
9:51 class
9:54 also known as the super class then we're
9:54 going to need
9:57 this import at the top as well so the
9:59 other way in which we can create a jframe
10:00 jframe
10:04 i'm actually going to copy this
10:06 turn all of this into a comment because
10:09 we won't really need it right now
10:10 and then i'm going to create a
10:13 constructor for my frame
10:15 so this constructor is going to be
10:17 called when we create an instance
10:20 of my frame and i'm going to paste
10:22 everything within here however we're
10:23 going to replace
10:27 the name of our frame and take out that
10:28 line as well
10:31 so instead of saying frame dot set title
10:33 we're actually going to replace the word
10:35 frame with the word
10:37 this so an easy way that you can make
10:39 these changes is go to
10:42 edit find replace
10:45 and we're going to replace frame
10:54 so replace the word frame or whatever
10:55 else you called your frame
10:58 at this point with this and we're going
10:59 to create an
11:02 instance of the my frame class
11:05 so we'll do that here so we're going to say
11:07 say
11:10 my frame instead of jframe and let's
11:11 call this
11:15 my frame equals new my
11:18 frame and this will do the exact same thing
11:18 thing
11:23 as all of this code where it creates a
11:25 basically a duplicate of the frame we had
11:26 had
11:29 so you don't necessarily need to create a
11:30 a
11:32 name for this instance if you don't plan on
11:33 on
11:35 using this name for anything so one
11:37 thing that we can also do
11:39 is take out this portion of the code if
11:42 we don't really plan on using the name
11:44 or a name for your my frame you can just
11:47 say new my frame
11:49 and this works as well however if you
11:51 need to make some changes within
11:54 this class to your my frame you might
11:56 have to actually create a name for
11:59 it all right so that's the basics of
12:01 frames in java if you'd like a copy of
12:03 all this code i'll post all of this in
12:04 the comments down below
12:06 but yeah that's how you can create a
12:09 frame in java
12:12 hey you yeah i'm talking to you if you
12:13 learn something new
12:16 then you can help me help you in three
12:17 easy steps
12:19 by smashing that like button drop a
12:20 comment down below
12:22 and subscribe if you'd like to become a
12:24 fellow bro [Music]