JFrame & JLabel. By Iqtidar Ali

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "JFrame & JLabel. By Iqtidar Ali"

Transcription

1 JFrame & JLabel By Iqtidar Ali

2 JFrame & its Features JFrame is a window with border, title and buttons. The component added to frame are referred to as its contents & are managed by the content pane. To add a component to JFrame, we must use its contentpane and will add it to content Pane. When JFrame is set visible, an event dispatching thread is started. It can be moved, resized.

3 JFrame Location By default, JFrame is displayed in upper-left corner of screen. To display frame at specified location, You can use the setlocation(x,y) method in JFrame class. This method places the upper-left corner of a frame at location(x,y).

4 Steps for creating a JFrame Window Construct an object of the JFrame. Set the size of the Jframe. Set the title of the Jframe to appear in title bar. Set the default close operation. When the user clicks close button, the program stops running. Make the Jframe visible.

5 import java.awt.*; import javax.swing.*; public class test extends JFrame{ public test(){ settitle("my Window"); setlayout(new FlowLayout()); setsize(300, 200 ); // Set the frame size setdefaultcloseoperation(jframe.exit_on_close); setvisible( true); // Display the frame public static void main(string[] args) test obj=new test();

6 LABEL To include a control in a window, you must add it to the window To do this, you must first create an instance of the desired control Then add it to a window by calling add() The syntax is Label compobj=new label() add(compobj); Here, compobj is an instance of the control that you want to add.

7 Label The easiest control to use is a label A label is an object of type Label, and it contains a string, which it displays Labels are passive controls that do not support any interaction with the user Label defines the following constructors: -Label( ) :- create empty label Label(String):- create label with given string Label(String, int):- Label with string & alignment

8 import java.awt.*; import javax.swing.*; public class test extends JFrame{ public test(){ settitle("my Window"); Label lb=new Label("Hello window"); add(lb); setlayout(new FlowLayout()); setsize(300, 200 ); // Set the frame size setdefaultcloseoperation(jframe.exit_on_close); setvisible( true); // Display the frame public static void main(string[] args) test obj=new test();

9 import java.awt.*; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingconstants; public class LabelFrame extends JFrame private JLabel label1; public LabelFrame() super("testing Label"); setlayout(new FlowLayout()); label1=new JLabel("Label with text"); label1.settooltiptext("this is Label1"); add(label1); public static void main(string[] args) LabelFrame labelframe=new LabelFrame(); labelframe.setdefaultcloseoperation(jframe.exit_on_close); labelframe.setsize(260,180); labelframe.setvisible(true); }

10 import java.awt.*; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingconstants; public class LabelFrame extends JFrame private JLabel label2; public LabelFrame() super("testing Label"); setlayout(new FlowLayout()); Icon bug=new ImageIcon(getClass().getResource("bug1.png")); label2=new JLabel("Label with text and icon", bug, SwingConstants.LEFT); label2.settooltiptext("this is Label2"); add(label2); public static void main(string[] args) LabelFrame labelframe=new LabelFrame(); labelframe.setdefaultcloseoperation(jframe.exit_on_close); labelframe.setsize(260,180); labelframe.setvisible(true); }

11 import java.awt.*; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingconstants; public class LabelFrame extends JFrame { private JLabel label3; public LabelFrame() { super("testing Label"); setlayout(new FlowLayout()); Icon bug=new ImageIcon(getClass().getResource("bug1.png")); label3=new JLabel(); label3.settext("label with icon and text at the bottom"); label3.seticon(bug); label3.sethorizontaltextposition(swingconstants.center); label3.settooltiptext("this is label3"); add(label3); } public static void main(string[] args) { LabelFrame labelframe=new LabelFrame(); labelframe.setdefaultcloseoperation(jframe.exit_on_close); labelframe.setsize(260,180); labelframe.setvisible(true); } }

12 THE END