Java labels 👨‍💻

preview_player
Показать описание
Java GUI labels JLabel swing tutorial for beginners

#Java #label #JLabel #swing #tutorial #beginners
Рекомендации по теме
Комментарии
Автор

// 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);
//set text LEFT, CENTER, RIGHT of imageicon
//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
//set background color
label.setOpaque(true); //display background color
//label.setBorder(border); //sets border of label (not image+text)
//set vertical position of icon+text within label
//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.setSize(500, 500);
//frame.setLayout(null);
frame.setVisible(true);
frame.add(label);
frame.pack();
}
}

BroCodez
Автор

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 =, 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 <frame.setLayout(null) ;> to set a layout for label first .
label.setBounds(0, 0, 500, 500) ; // (x, y, width, height)

// Text of label

// Another style : <JLabel label = new JLabel("Nice to meet you !") ;>
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 .
; // Left or CENTER or RIGHT
; // TOP or CENTER or BOTTOM

// set the position of label image within the label bounds .
;
;

// 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 .
: disable the [X] button .
;
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 <frame.add()> to add all your components to your frame first, disable <frame.setLayout(null)> and <label.setBounds()> before you use the code below .
// frame.pack() ;
}
}

maxwong
Автор

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

caidenhiles
Автор

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

pavelkvasnicka
Автор

Thank you very much Bro. Your tutorials are fantastic. Your so underrated man...

augustocera
Автор

This was soo difficult to understand at school but somehow you make it sooo easy. Thank you! 🙏🏼

ADJOLU
Автор

If the Image Icon is not showing up you can try:

'ImageIcon *name* = new

instead of
'ImageIcon *name * = new ImageIcon(*FilePath*);'

Iischeese
Автор

Awesome! I love the broke down videos - learn Java in smaller sections

UnderArea
Автор

bro your tutorials are so much helpfull its well detailed for a beginner like me .

mbekezelisibanda
Автор

great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much

dianamilenaarchilacordoba
Автор

you're the best teacher, Thanks for this vdo

somonorhong
Автор

From Java17 onwards use "LEADING" instead of "TOP" to avoid exception

swapnil
Автор

Much needed thing in YouTube thanks bro

RockRahul
Автор

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❤

kamalkhashoggi
Автор

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

Ryan-zvyl
Автор

@Bro Code I am learning more from you than I am my professors. Thanks Bro~~

beksaylor
Автор

I use to think and java and javascript was the same when i first strarted coding hahah

noisyguest
Автор

thank you very much bro, you're more helpfulI than university lectures

WajdAlhuzaym
Автор

I need this Chanel, because java is complicated

Benchmus
Автор

Thank you bro! Great content and explainations.

robinshere
welcome to shbcf.ru