// You might want to add frame.setVisible(true) to be the very last line. Sometimes with Mac, the components won't appear until you resize the window import java.awt.Color; import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; public class Main { public static void main(String[] args) { // JLabel = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("dude.png"); Border border = BorderFactory.createLineBorder(Color.green,3);
JLabel label = new JLabel(); //create a label label.setText("bro, do you even code?"); //set text of label label.setIcon(image); label.setHorizontalTextPosition(JLabel.CENTER); //set text LEFT,CENTER, RIGHT of imageicon label.setVerticalTextPosition(JLabel.TOP); //set text TOP,CENTER, BOTTOM of imageicon label.setForeground(new Color(0x00FF00)); //set font color of text label.setFont(new Font("MV Boli",Font.PLAIN,100)); //set font of text label.setIconTextGap(-25); //set gap of text to image label.setBackground(Color.black); //set background color label.setOpaque(true); //display background color //label.setBorder(border); //sets border of label (not image+text) label.setVerticalAlignment(JLabel.CENTER); //set vertical position of icon+text within label label.setHorizontalAlignment(JLabel.CENTER); //set horizontal position of icon+text within label //label.setBounds(100, 100, 250, 250); //set x,y position within frame as well as dimensions
@@jhanzaibhumayun5782 bro can u help me pls by telling what u exactly did? Im also using intellij and facing the problem, tried different ways, but imageicon is not showing up, text is coming but imageicon never shows 😭, pls help bro, what to do
@@shanihalder2755 I just added frame.setVisible(true) to the last line in the code. Don't really know exactly what type of problem you are having. Would help if you posted your code.
@@jhanzaibhumayun5782 problem solved bro after multiple tries. It solved after i moved the image to the project folder, earlier i was putting the image in the source code folder. Thanks for help❤️
Knowing that you made a video about labels actually exited me to learn about labels and I thought about it all day today and yesterday until I finally got a chance to watch it tonight. Gotta say, it's my favorite tutorial i've watched
This lesson has much more information than usual . I have made some references for each code and would like to share with you guys . Hope this will make it more clear to you . package testing; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; import javax.swing.border.Border; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Font; public class Main { public static void main(String[] args) {
// create an image for label . ImageIcon image = new ImageIcon("src/烈日戰士.png") ;
// create boundary lines for label and set their color and size . Border border = BorderFactory.createLineBorder(new Color(0x000000) , 5) ;
// Setup the label
// create a label . JLabel label = new JLabel() ; label.setBackground(new Color(0xABF095)) ;
// It is used after setting the background color . // true : paint every pixel within the bounds of label . label.setOpaque(true) ;
// Apply the boundary lines to the label . label.setBorder(border) ;
// set the position and dimensions of label bounds within the frame . // Before using the code below , add to set a layout for label first . label.setBounds(0 , 0 , 500 , 500) ; // (x , y , width , height)
// Text of label
// Another style : label.setText("Nice to meet you !");
// set "type of font" , font style , font size . // ("MV Boli" , "Helvetica" , "Futura" , ……) , (plain, bold, italic) label.setFont(new Font("MV Boli" , Font.PLAIN , 20)) ;
// set the font color . label.setForeground(new Color(0x4878F5)) ;
// Position of components in label
// add the image into the label . label.setIcon(image) ;
// set the position of label text within the label bounds . label.setHorizontalTextPosition(JLabel.CENTER) ; // Left or CENTER or RIGHT label.setVerticalTextPosition(JLabel.TOP) ; // TOP or CENTER or BOTTOM
// set the position of label image within the label bounds . label.setHorizontalAlignment(JLabel.CENTER) ; label.setVerticalAlignment(JLabel.CENTER) ;
// set the distance between the text block and the image . // negative and positive are both available . label.setIconTextGap(0) ;
// Setup the frame .
// we need a frame to hold components(e.g. label) . JFrame frame = new JFrame() ; frame.setVisible(true) ;
// exit when you click [X] on the top-right corner . // "JFrame.HIDE_ON_CLOSE" : hide but still running in the background thread . //"JFrame.DO_NOTHING_ON_CLOSE" : disable the [X] button . frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; frame.setSize(500 , 500) ; // still manually resizable .
// Add component to the frame .
frame.add(label) ;
// set a layout for the label to set bounds . frame.setLayout(null) ;
// By using the code below , system will auto-resize the frame in order to accommodate all the components you add to the frame , no matter if size of components change . // use to add all your components to your frame first , disable and before you use the code below . // frame.pack() ; } }
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
If the Image Icon is not showing up you can try: 'ImageIcon *name* = new ImageIcon(getClass().getResource("*FilePath*"));' instead of 'ImageIcon *name * = new ImageIcon(*FilePath*);'
bro u can help me. my error : Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
Makes me really appreciate JavaScript with css. This would have taken me like 8 lines of css to accomplish this. Great to have this option in Java thiugh
Thank yo brother I understand now what is a Label is ow to create a label and what is a frame and how to create a Frame. Millions Thanks and god bless you❤
Copy the image from the explorer and then in Eclipse, select the package and hit ctr + v to paste it. Alternatively you can copy the absolute path of the image if you don't want to paste it in the project folder.
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ; ImageIcon icon = new ImageIcon (filename.png): Image image = icon.getImage(); //transform the icon to an image. Image newimage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH): icon=new ImageIcon(newimage);// this transforms the image back to an icon credit: Martin Wachira(Comments)
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ; ImageIcon icon = new ImageIcon (filename.png): Image image = icon.getImage(); //transform the icon to an image. Image newimage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH): icon=new ImageIcon(newimage);// this transforms the image back to an icon credit: Martin Wachira(Comments)
My gui window pop up does not have the Java Logo on the left top? How to set up so that this Java Logo should appear on the left top? Instead of the Java Logo on top left, all I see just: X, -, and + signs on the left. Is there a way to move those to the right top so that we can set up the Java Logo on the left top. Thanks for helping...
idk why but my code wont load any image some of the var names are diffrent then the video but it just wont load and idk why package Main; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class mayn { public static void main(String[] args) { ImageIcon bg = new ImageIcon("computationalemulatorr calc2.png");
JLabel textz = new JLabel(); textz.setText("GOD HELP ME PLEASE, GIVE ME STRENGTH"); textz.setIcon(bg); JFrame window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(32*48,32*24);
Thanks for the tutorial. But I have a question. Why is it when I run the program the GUI's Appearance doesn't update, it's just empty/blank white window. It just gets updated or fixed whenever I just resize the window's size or hit maximize or minimize button. I mean it's not a big deal, but just annoying since I have to do that whenever I need to run the code.
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ; ImageIcon icon = new ImageIcon (filename.png): Image image = icon.getImage() //transform the icon to an image. Image newimage = image.getScaledInstance(width, height, Image SCALE_SMOOTH): icon = new ImageIcon (newimage);// this transforms the image back to an icon.
Hey bro, I copy and pasted all your code into my VScode editor and swapped the bro.png reference with a different image in my project folder-- but the image didn't show on the screen. Do you know an easy fix to this? It's really been bothering me.
// You might want to add frame.setVisible(true) to be the very last line. Sometimes with Mac, the components won't appear until you resize the window
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class Main {
public static void main(String[] args) {
// JLabel = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("dude.png");
Border border = BorderFactory.createLineBorder(Color.green,3);
JLabel label = new JLabel(); //create a label
label.setText("bro, do you even code?"); //set text of label
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER); //set text LEFT,CENTER, RIGHT of imageicon
label.setVerticalTextPosition(JLabel.TOP); //set text TOP,CENTER, BOTTOM of imageicon
label.setForeground(new Color(0x00FF00)); //set font color of text
label.setFont(new Font("MV Boli",Font.PLAIN,100)); //set font of text
label.setIconTextGap(-25); //set gap of text to image
label.setBackground(Color.black); //set background color
label.setOpaque(true); //display background color
//label.setBorder(border); //sets border of label (not image+text)
label.setVerticalAlignment(JLabel.CENTER); //set vertical position of icon+text within label
label.setHorizontalAlignment(JLabel.CENTER); //set horizontal position of icon+text within label
//label.setBounds(100, 100, 250, 250); //set x,y position within frame as well as dimensions
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500,500);
//frame.setLayout(null);
frame.setVisible(true);
frame.add(label);
frame.pack();
}
}
Thanks for the tip! My labels were not showing up until I resized the window. This explains why. Using IntelliJ IDEA.
@@jhanzaibhumayun5782 bro can u help me pls by telling what u exactly did? Im also using intellij and facing the problem, tried different ways, but imageicon is not showing up, text is coming but imageicon never shows 😭, pls help bro, what to do
@@shanihalder2755 I just added frame.setVisible(true) to the last line in the code. Don't really know exactly what type of problem you are having. Would help if you posted your code.
in the previous lesson .setopaque wasn't used change color of the frame but is it used here?
@@jhanzaibhumayun5782 problem solved bro after multiple tries. It solved after i moved the image to the project folder, earlier i was putting the image in the source code folder. Thanks for help❤️
Knowing that you made a video about labels actually exited me to learn about labels and I thought about it all day today and yesterday until I finally got a chance to watch it tonight. Gotta say, it's my favorite tutorial i've watched
This lesson has much more information than usual .
I have made some references for each code and would like to share with you guys .
Hope this will make it more clear to you .
package testing;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Font;
public class Main {
public static void main(String[] args) {
// create an image for label .
ImageIcon image = new ImageIcon("src/烈日戰士.png") ;
// create boundary lines for label and set their color and size .
Border border = BorderFactory.createLineBorder(new Color(0x000000) , 5) ;
// Setup the label
// create a label .
JLabel label = new JLabel() ;
label.setBackground(new Color(0xABF095)) ;
// It is used after setting the background color .
// true : paint every pixel within the bounds of label .
label.setOpaque(true) ;
// Apply the boundary lines to the label .
label.setBorder(border) ;
// set the position and dimensions of label bounds within the frame .
// Before using the code below , add to set a layout for label first .
label.setBounds(0 , 0 , 500 , 500) ; // (x , y , width , height)
// Text of label
// Another style :
label.setText("Nice to meet you !");
// set "type of font" , font style , font size .
// ("MV Boli" , "Helvetica" , "Futura" , ……) , (plain, bold, italic)
label.setFont(new Font("MV Boli" , Font.PLAIN , 20)) ;
// set the font color .
label.setForeground(new Color(0x4878F5)) ;
// Position of components in label
// add the image into the label .
label.setIcon(image) ;
// set the position of label text within the label bounds .
label.setHorizontalTextPosition(JLabel.CENTER) ; // Left or CENTER or RIGHT
label.setVerticalTextPosition(JLabel.TOP) ; // TOP or CENTER or BOTTOM
// set the position of label image within the label bounds .
label.setHorizontalAlignment(JLabel.CENTER) ;
label.setVerticalAlignment(JLabel.CENTER) ;
// set the distance between the text block and the image .
// negative and positive are both available .
label.setIconTextGap(0) ;
// Setup the frame .
// we need a frame to hold components(e.g. label) .
JFrame frame = new JFrame() ;
frame.setVisible(true) ;
// exit when you click [X] on the top-right corner .
// "JFrame.HIDE_ON_CLOSE" : hide but still running in the background thread .
//"JFrame.DO_NOTHING_ON_CLOSE" : disable the [X] button .
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setSize(500 , 500) ; // still manually resizable .
// Add component to the frame .
frame.add(label) ;
// set a layout for the label to set bounds .
frame.setLayout(null) ;
// By using the code below , system will auto-resize the frame in order to accommodate all the components you add to the frame , no matter if size of components change .
// use to add all your components to your frame first , disable and before you use the code below .
// frame.pack() ;
}
}
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
This was soo difficult to understand at school but somehow you make it sooo easy. Thank you! 🙏🏼
Thank you very much Bro. Your tutorials are fantastic. Your so underrated man...
From Java17 onwards use "LEADING" instead of "TOP" to avoid exception
great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much
Awesome! I love the broke down videos - learn Java in smaller sections
you're the best teacher , Thanks for this vdo
bro your tutorials are so much helpfull its well detailed for a beginner like me .
If the Image Icon is not showing up you can try:
'ImageIcon *name* = new ImageIcon(getClass().getResource("*FilePath*"));'
instead of
'ImageIcon *name * = new ImageIcon(*FilePath*);'
bro u can help me. my error : Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
@@thaison3684 looks like something that you code is trying to reference is null. Try seeing if you are referencing/declaring correctly.
Bless you, I was going crazy!! That did the trick! 🤯
@@Tamflakes No problem, glad I could help!
Bro it is showing " non static method "getclass()" tell me without using method how to bring images??
Makes me really appreciate JavaScript with css. This would have taken me like 8 lines of css to accomplish this. Great to have this option in Java thiugh
Thank yo brother I understand now what is a Label is ow to create a label and what is a frame and how to create a Frame. Millions Thanks and god bless you❤
bro how do you add another text so that there is a text above and below the pic?
Much needed thing in RUclips thanks bro
Wow. Awesome.
thank you sir
I need this Chanel, because java is complicated
Please Make Tuturial For JAVAFX
Guys, my favicon isn't showing on my JFrame. I am using netbeans on mac OS. How can I make it work?
Thanks, Bro! ☕ You're awesome!
how can i add only the image and move it around the frame?
Sir please tell us in short how to add an image to the project folder? Thank you sir
Copy the image from the explorer and then in Eclipse, select the package and hit ctr + v to paste it. Alternatively you can copy the absolute path of the image if you don't want to paste it in the project folder.
I use to think and java and javascript was the same when i first strarted coding hahah
I did too lol. I guess Javascript was named after Java because the name was so popular at the time, albeit Javascript has nothing to do with Java
@@BroCodez ohhh i learned js beforr java becuz of it
Yeah I'm also confused when I'm started Java. I bought javascript course in Udemy instead of Java course .
Me tooo.....bro explanation 👌..take a bow
@Bro Code I am learning more from you than I am my professors. Thanks Bro~~
How can I resize the image in a label. I palced a random image it only a part of it appeared in the label
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ;
ImageIcon icon = new ImageIcon (filename.png):
Image image = icon.getImage(); //transform the icon to an image.
Image newimage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH):
icon=new ImageIcon(newimage);// this transforms the image back to an icon
credit: Martin Wachira(Comments)
Thank you very much
thank you very much bro, you're more helpfulI than university lectures
very helpful as always thank you !!
Thank you bro! Great content and explainations.
thank u bro
Hey Bro!!! Thanks so much for the tutorial!!!!
you're welcome! Thanks for watching!
Thank You !
nice
I Coded on my VS but its not getting any effect in Frame even though I added label to Frame? Anyone know why that is happening?
Very good video keep it up!
thanks for watching Josh!
How to set an image as your the logo for the app you have programmed?
Thank you brother!
we're gettin higher bro!!
ImageIcon doesn't work. Give some suggestion
how can i make the image smaller on my screen?
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ;
ImageIcon icon = new ImageIcon (filename.png):
Image image = icon.getImage(); //transform the icon to an image.
Image newimage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH):
icon=new ImageIcon(newimage);// this transforms the image back to an icon
credit: Martin Wachira(Comments)
The font style doesn't work. I need help
Thanks Bro
I'm keen on your videos bro.
thank you for watching Mohamad
My gui window pop up does not have the Java Logo on the left top? How to set up so that this Java Logo should appear on the left top? Instead of the Java Logo on top left, all I see just: X, -, and + signs on the left. Is there a way to move those to the right top so that we can set up the Java Logo on the left top. Thanks for helping...
are you referring to the default java logo?
thank you bro myhero
Thank you so much sir.
How do you add a jpeg file in intellij idea? Help please
Very very good
thanks for watching Ahmed
Thanks You so so so so so so so much..
idk why but my code wont load any image some of the var names are diffrent then the video but it just wont load and idk why
package Main;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class mayn {
public static void main(String[] args) {
ImageIcon bg = new ImageIcon("computationalemulatorr calc2.png");
JLabel textz = new JLabel();
textz.setText("GOD HELP ME PLEASE, GIVE ME STRENGTH");
textz.setIcon(bg);
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(32*48,32*24);
window.add(textz);
window.setVisible(true);
}
}
Sams problem bro why I can't use label.add(image)?? You got solution
@@zohebansari6429 any solution ?
thanks bro
PLz bro how to set an image as your logo
Thanks for the tutorial. But I have a question. Why is it when I run the program the GUI's Appearance doesn't update, it's just empty/blank white window. It just gets updated or fixed whenever I just resize the window's size or hit maximize or minimize button. I mean it's not a big deal, but just annoying since I have to do that whenever I need to run the code.
try putting the frame.setVisible(true); at the end of the code
@@soumelee5661 I already found the solution on the error! 😅 But thank you!
where i can find fonts types for label
how can I add second label?
Ly 2 bro
My Images aren't visible pls help🙏
Thanks!!
Sir, is it possible to create array of JLabels....?
Yeah
Jlabel[] labelArray = new Jlabel[whatever size you want]
Label of love
Thankyou Bro😍
perfect
thx bro
is there a code to resize the image icon?
To resize the image icon, you'll have to translate the icon to an image, resize the image then translate the the image back to an icon. Here's the code ;
ImageIcon icon = new ImageIcon (filename.png):
Image image = icon.getImage() //transform the icon to an image.
Image newimage = image.getScaledInstance(width, height, Image SCALE_SMOOTH):
icon = new ImageIcon (newimage);// this transforms the image back to an icon.
@@martinwachira4429 Thanks a lot brother. It worked!!
Best
thanks for watching MD. Sazib
Isnt doing all this in Java , a little complicated. Can be done with less effort with CSS and Front end Javascript ...
Whats your opinion ?
a little late, but this is a java course man, why would he do it in CSS or JavaScript in a Java course. Doesnt matter if its complicated.
GG
Thanks
you are the goat! T ^ T :DDD
import java.awt.Font;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class Main{
public static void main(String[ ]args){
ImageIcon image = new ImageIcon("flower.png");
Border border = BorderFactory.createLineBorder(Color.blue,3);
JLabel label = new JLabel();
label.setText("Coding is fun!");
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.RIGHT);
label.setVerticalTextPosition(JLabel.CENTER);
label.setForeground(Color.blue);
label.setFont(new Font("Italic",Font.ITALIC,18));
label.setIconTextGap(-20);
label.setBackground(Color.yellow);
label.setOpaque(true);
label.setBorder(border);
label.setVerticalAlignment(JLabel.BOTTOM);
label.setHorizontalAlignment(JLabel.LEFT);
//label.setBounds(50,50,125,125);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(380,380);
//frame.setLayout(null);
frame.add(label);
frame.setVisible(true);
frame.pack();
}
}
Hey bro, I copy and pasted all your code into my VScode editor and swapped the bro.png reference with a different image in my project folder-- but the image didn't show on the screen. Do you know an easy fix to this? It's really been bothering me.
Ah, I got it to work. Thanks for the vids man- they are really the only resource out there to learn this kind of stuff
I'm having the same problem..could you please help me out ??
@@dipikakanth8402 I fixed the issue by putting my image in the project folder-- not the library folder or the source code folder, but the main folder.
@@jacokyle0160 Thank you
for the algorithm
Bro you make me Pro
Bro, please add code to comment!
I code but not in binary numbers
that is ok, I don't either
Java has Labels...
it also has goto 👹
ok
yo its me fellow bro
where's the code tho
2. comment
"Video 50"
Bro,do u even code?
You = God
thanks, but I'm just bro
comment
50th. Thank you, ma Bro Sensei
random comment
Seems like you have cough, everything alright??
bruv it's his voice
My voicebox was wornout. Two videos/day might be too much lol
.
a buh abuh dat's all fokes
Best
thanks bro
Thanks
comment