JLabel

전영덕·2023년 2월 12일
0

Java Swing

목록 보기
2/2
package learningSwingByYoutube;

import java.awt.Color;
import java.awt.Font;
import java.awt.Label;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;

public class Main02 {

	public static void main(String[] args) {

		// Jlabel = a GUI display area for a string of text, an image, or both

		ImageIcon image2 = new ImageIcon("image1.jpg"); 		//2. create a image
		Border border = BorderFactory.createLineBorder(Color.green);//11. set border
		
		
		JLabel label = new JLabel();							//0.create a label
		label.setText("Bro, do you even code?"); 				// 1. set text label. we can make those (0. and 1.) with 1 line by
																// 	JLabel label = new JLabel("Bro, do you even code?");
		label.setIcon(image2);									//3. set a image
		label.setHorizontalTextPosition(JLabel.CENTER);			//4. set text LEFT, CENTER, RIGHT of imageicon
		label.setVerticalTextPosition(JLabel.TOP); 				//5. set text TOP, Center, or BOTTOM of imageicon
		label.setForeground(new Color(200,100,100));			//6. set font color of text
		label.setFont(new Font("MV Boli", Font.PLAIN,50)); 		//7. set font of text
		label.setIconTextGap(100); 								//8. set gap of text to image
		label.setBackground(Color.black); 						//9. set background color
		label.setOpaque(true); 									//10. display background color
		label.setBorder(border);								//11. 
		label.setVerticalAlignment(JLabel.CENTER);				//12.set vertical position of icon+text within label
		label.setHorizontalAlignment(JLabel.CENTER); 			//13.set horizontal position of icon+text within label
		//label.setBounds(100, 100, 250, 250);					//15. set x,y position within frame as well as dimensions
		
		JFrame frame = new JFrame();
		frame.setTitle("Jframe title goes here");
		frame.setSize(420, 420);
		//frame.setLayout(null);								//14.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.add(label);
		frame.pack(); 											//16.get rid of 14and 15. Next this code. You should check the turn. Label ahead then pack method.
		
	}

}

0개의 댓글