[JavaSwing] JLabel
Content
Create simple JLabel
Put color, background color for JLabel
JLabel is often used to display text or image to create guidelines, instructions on the user interface.
In the picture above using 4 JLabel user to correctly enter the required information.
Example 1: Create simple JLabel
package nguyenvanquan7826.JLabel; import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class MyJLabel extends JFrame { public MyJLabel() { // create frame setLayout(new GridLayout(1, 3, 5, 5)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create image Icon icon = new ImageIcon(getClass().getResource("7826.png")); // create three JLabel JLabel lb1 = new JLabel("label text only"); JLabel lb2 = new JLabel(icon); JLabel lb3 = new JLabel("icon and text", icon, JLabel.CENTER); lb3.setVerticalTextPosition(JLabel.BOTTOM); lb3.setHorizontalTextPosition(JLabel.CENTER); // add three label to frame add(lb1); add(lb2); add(lb3); // display frame pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MyJLabel(); } }
The results have been 3 JLabel as follows (Note 7826.png file is placed in the same package as shown)
In this example, we note the following command:
– setLayout(new GridLayout(1, 3, 5, 5)); Orders placed GridLayout for JFrame with 1 row, 3 post, columns and rows are separated by 5px. Layout Temporary understand the layout of objects for JFrame, if no layout, the object will be overlap.
– Icon icon = new ImageIcon(); Read command file photo imaging for JLabel.
– Followed by 3 commands 3 JLabel form. JLabel all 6 form initialization:
+/ JLabel(): Create a JLabel instance with no image and an empty string
+/ JLabel(Icon image): Create an instance of JLabel assign a picture
+/ JLabel(Icon image, int horizontalAlignment): Create an instance of JLabel specify an image and horizontal alignment
+/ JLabel(String text): Create an instance of the specified text JLabel
+/ JLabel(String text, Icon icon, int horizontalAlignment): Create an instance of the specified text JLabel, image và horizontal alignment
+/ JLabel(String text, int horizontalAlignment): Create an instance of the specified text JLabel, và horizontal alignment.
In LB3 we have 2 jaw setVerticalTextPosition and setHorizontalTextPosition to set the position of the text vertically (BOTTOM) and horizontally (CENTER). For the same LB1 JLabel containing only text if desired alignment (left, right, between, …) we use the method setHorizontalAlignment(int alignment).
Example 2: Put color, background color for JLabel
package nguyenvanquan7826.JLabel; import java.awt.Color; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class MyJlabelWithColor extends JFrame { public MyJlabelWithColor() { setLayout(new GridLayout(1, 2, 5, 5)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); // create JLabel have text color red and background color green JLabel lb; lb = createJLabel("JLabel 1", Color.red, Color.green); add(lb); // create JLabel have text color blue and background color yellow lb = createJLabel("JLabel 2", Color.blue, Color.yellow); add(lb); // display JFrame setLocationRelativeTo(null); setVisible(true); } private JLabel createJLabel(String text, Color textColor, Color backgroundColor) { JLabel lb = new JLabel(text); // set align lb.setHorizontalAlignment(JLabel.CENTER); // set color lb.setForeground(textColor); // set background color lb.setOpaque(true); lb.setBackground(backgroundColor); return lb; } public static void main(String[] args) { new MyJlabelWithColor(); } }
In this example we create a function createJLabel returns 1 JLabel with the arguments passed to turn the text, its color and color brackground. It is noteworthy that in order to accommodate the background, we need to put Opaque JLabel is true of (The default is false).
Refer to: class JLabel
I can make the background transparent label not it, ie if you insert a picture behind the label, to label override, but so will see a different color scheme, can only word that does not float floating frame label does not?
By default, the back ground of JLanel is then placed over. But if you want to set the background of a photo JLabel, you can do the following:
Among them are the images you put in the corresponding package or taken from somewhere (but should only true path). Also you can do it this way with other objects such as JFrame, JPanel, …
Ad co the viet chu thick bang tieng Viet dk ko?
Her write captions in English but translated from Vietnamese style out that. Easy to stop your service. Familiarize yourself with the caption and name in English turns offline, That would be useful.
Can you tell me how I file level(7826.png) no.
File that is just the image alone, Your photos are also used where.
I want to ask how to put files in the package as shown above 7826.png)
You just copy and paste it to stop.
suppose you have 1 JLabel on 1 JFrame, and I want to draw a line on the make ntn JLabel Bro
Then the leftovers JLable and rewrite function OnDraw
e type input code netbeans done it this a military error ạ:
invalid method declaration; return type required
setLayout(new GridLayout(1, 3, 5, 5);
illegal start of type
What is this error z a?
He is also not clear why more…
for ( int i = 0; in < 4; ++in) {
for ( int j = 0; j < 3; ++j) {
Image imgTemp = createImage(new FilteredImageSource(img.getSource(),
new CropImageFilter(j*width/3, i*height/3, (width/3)+1, height/3+1)));
vt.add(imgTemp);
}
}
for(int i = 0; in < 12; i )
{
check[in] = false;
}
checkwin = new int[4][3];
for(int i = 0; in < 4; i ){
for( int j = 0; j 3){
}
bt.setIcon(new ImageIcon(vt.get(index)));
checkwin[in][j] =index ;
check[index] = true;
jp.add(bt);
}
}
I asked him for a bit irrelevant to the article a bit is not it? I use the random function and crop the image into array however you want 1 cell matrix in a fixed image retention 1 Location is how ạ? My random function seems faulty he can point out solutions to help you not sir?
I do not see your content anywhere random. If you want 1 Photo fixed cells with random enemies do not give in that box.
bt = new JButton();
bt.addActionListener(this);
Random random = new Random();
int index = Math.abs(Random.nextInt()%11);// random from 0 to 10
while(check[index]){
index = Math.abs(Random.nextInt()%11);
System.out.println(index+” “);
}
if(in>3){
}
bt.setIcon(new ImageIcon(vt.get(index)));
checkwin[in][j] =index ;
check[index] = true;
jp.add(bt);
hi sir you put your labor shortage.
So this is how you make it wrong
labor is not wrong sir. but I do the puzzle game. random, the umbrella when you finally will be disturbed and if done playing, the water will be killed. Should you wish to graft is finally fixed it at the bottom and the other puzzle pieces shuffling alone sir
He asked me about the function:
Icon icon = new ImageIcon(getClass().getResource(“7826.png”));
// create three JLabel
JLabel lb2 = new JLabel(icon);
And
label = new JLabel() {
public void paintComponent(Graphics g) {
g.drawImage(img.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
==> I think that we also offer the same result, whether you want to use is not in any way that he ??.
Yeah you
let me ask how to write the word over the image so he
You look in here, please try. http://stackoverflow.com/questions/2736320/write-text-onto-image-in-java
I asked some function for font size adjustment ko sir ?
you try setTextSize view.
apparently not content that my ad e want to adjust the font size for JLabel without sir
He explained very detailed exercises sir. Thank you very much sir E feelings.
How do you want to ask for random background color of JLabel?
VDU itself 5 seconds to change color 1 sir times.
Random color, use color codes its random function. Still 5 seconds 1 once they try theard vs Hander view.
cho e hỏi là mình có thể chọn 1 vùng bất kỳ trên ảnh khi nó được load trên jlabel được không ạ .như kiểu là mình kéo thả chuột để chọn 1 hình chữ nhật bất kì trên đó và xử lí mỗi vùng đó thôi ý .
Cái này mình chưa thử 🙂 Bạn tìm thêm trên google xem nhé.