Thanks
Thank you user for ur javax swing Library tutorial.......
public class Hello_World { // TODO Auto-generated method stub } }
import javax.swing.JFrame;
and then class can extend JFrame class import javax.swing.JFrame;
Now it looks like this code:
import javax.swing.JLabel;
Traditionally, all GUI elements are initialized in the constructor. We will act in the same way.
To create an instance of JLabel, write the following code:
The constructor of JLabel class takes a string as an argument. This string will be displayed on the label. To find more information about JLabel class, read this
setForeground
method of the JLabel object helloLabel.setForeground(Color.RED);
The parameter of this method is the color of the text. To use color constants, please, add import java.awt.Color;
import.
By default, text in the label is aligned to the left part. We will change the alignment to the center using setHorizontalAlignment
method:
helloLabel.setHorizontalAlignment(SwingConstants.CENTER);
SwingConstants class it's a set collection of different constants used in Java. CENTER
constant is used to align text to the center of component.
Now we will change the font of the text, displayed on label. We wil call setFont
method to change the font.
It will look like this helloLabel.setFont(new Font("Arial", Font.PLAIN, 36));
where "Arial" is font's name, "Font.PLAIN" is font's style(It can be also Font.Bold and Font.Italic) and 36 - size of font. We have finished the process of initialization. Now, the constructor looks like this:
Hello_World(){ JLabel helloLabel; add(helloLabel); }
add
method. in the line 7.
Hello_World HW = new Hello_World();
SetSize
method:
HW.setSize(320, 240);
After this, we make the frame visible:
HW.setVisible(true);
The full project's code is:
import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; Hello_World(){ JLabel helloLabel; add(helloLabel); } // TODO Auto-generated method stub Hello_World HW = new Hello_World(); HW.setSize(320, 240); HW.setVisible(true); } }