Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of.

Similar presentations


Presentation on theme: "GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of."— Presentation transcript:

1 GUI in Java

2 Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of familiarity User interacts with GUI component via mouse, keyboard, etc.

3 GUI in Java Java.awt –Abstract Window ToolKit –Was used with java1.1 Java.Swing –Uses some of the classes of java.awt package –Part of Java Foundation Classes (JFC)

4 Creating a Window Window class in java.awt –Provides no border and title bar. JFrame class in java.swing –Provides title, border, and other facilities for a window.

5

6 Subclasses of Component Class

7

8 Window Pane Window panes are container objects that represent an area of a window. They come in several different types. Defined in javax.swing package

9 Component Class Attributes –Position ( x and y with respect to the container component) –Name –Size (width and height) –Foreground color, background color –Font –Cursor –Enabled or not ( normal or grayed out) –Visible or not

10 Contd… Methods –setName(String) –isVisible(), isEnabled() (Boolean) –setVisible(), setEnabled()

11 Size and Position of a Component Position is represented by Point Object of java.awt.Point –point representing a location in (x, y) coordinate space, specified in integer precision. Size by java.awt.Dimension –It has width and Height public members. Size and position together by java.awt.Rectangle

12 Methods for Size and Position With Dimension Class –Dimension getSize() –Dimension getSize(Dimension dim) With Point Class –Point getLocation() –Point getLocation(Point p) With Rectangle Class –Rectangle getBounds() –Rectangle getBounds(Rectangle rect)

13 Methods for Size and Position

14

15 Changing Size and Position of a Component Void setBounds( int x, int y, int width, int Height) Void setBounds( Rectangle rect) Void setSize(Dimension d) setLocation( int x, int y) setLocation( Point p)

16 Creating Window:Example import javax.swing.JFrame; public class TryWindow { // The window object static JFrame aWindow = new JFrame(“This is the Window Title”); public static void main(String[] args) { int windowWidth = 400; // Window width in pixels int windowHeight = 150; // Window height in pixels aWindow.setBounds(50, 100, // Set position windowWidth, windowHeight); // and size aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aWindow.setVisible(true); // Display the window }

17 Creating Window: Example OUTPUT

18

19

20 Changing Size and Position of a Component

21 Variation in Size of a Component

22 ToolKit() Method of Component class getToolKit() returns an object of type ToolKit. It gives information about the environment in which application is running. E.g screen size in pixels

23 Example:NewWindow import javax.swing.JFrame; import java.awt.Toolkit; import java.awt.Dimension; public class TryWindow2 { // The window object static JFrame aWindow = new JFrame(“This is the Window Title”); public static void main(String[] args) { Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position wndSize.width/2, wndSize.height/2); // Size aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aWindow.setVisible(true); // Display the window }

24 Visual Characteristics of Components void setBackground(Color aColor) Color getBackground() void setForeground(Color bColor) Color getForeground() void setCursor(Cursor aCursor) void setFont(Font aFont) Font getFont()

25 Methods Description

26 Defining Color A screen color is represented by an object of class Color. You define a color value as a combination of the three primary colors: red, green, and blue. They are usually expressed in that sequence, and are often referred to as RGB values.

27 Defining Color 1.if you want the window in the previous example to have a pink background, you could add the statement: aWindow.setBackground(Color.PINK); 2.When you have created a Color object, you can brighten or darken the color it represents by calling its brighter() or darker() methods by a predefined factor: thisColor.brighter(); // Brighten the color thatColor.darker(); // Darken the color 3.To compare two Color objects you can use the equals() method. For example, to compare two colors objects colorA and colorB, you could write: if(colorA.equals(colorB)) { // Do something... }

28 Creating Cursors An object of the java.awt.Cursor class encapsulates a bitmap representation of the mouse cursor. Cursor myCursor = new Cursor(Cursor.TEXT_CURSOR);

29 Creating Cursors

30 Colors & Cursors import javax.swing.JFrame; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.Color; import java.awt.Cursor; public class TryWindow4 { // The window object static JFrame aWindow = new JFrame(“This is the Window Title”); public static void main(String[] args) { Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position wndSize.width/2, wndSize.height/2); // Size aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); aWindow.getContentPane().setBackground(Color.PINK); aWindow.setVisible(true); // Display the window}

31 OUTPUT

32 Selecting Fonts Font myFont = new Font(“Serif”, Font.ITALIC, 12); Font myFont = new Font(“Serif”, Font.ITALIC + Font.BOLD, 12); GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font[] fonts = e.getAllFonts();

33 Example: FontInfo

34 Swing Components

35 SwingComponents:Buttons

36 Creating a Swing Button & RadioButton & Label Container content = aWindow.getContentPane(); JButton myButton=new JButton(“MyButton”); content.add(myButton); JRadioButton myButton=new JRadioButton(“my radio button”); content.add(myButton); JLabel Label = new JLabel("hiiiiiiiiiii"); content.add(Label);

37 Menus

38 JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu fileMenu = new JMenu(“File”); menuBar.add(fileMenu); JMenuItem openMenu = new JMenuItem(“Open”); JCheckboxMenuItem circleItem = new JCheckboxMenuItem(“Circle”, true); JRadioButtonMenuItem item = new JRadioButtonMenuItem(“Curve”, true); fileMenu.add(openMenu); Creating MenuBar, Adding Menu & MenuItems

39 Text Components

40 JPanel The JPanel class defines something like a physical panel that you can use as a container to group a set of components. JList Component JTable Component

41 Lay Out Managers A layout manager determines the way that components are arranged in a container. Layout manager for a container determines the position and size of all the components in the container. The classes that define layout managers all implement the LayoutManager interface

42 Flow Layout The flow layout manager places components in a row, and when the row is full, it automatically spills components onto the next row. Default position is centered. Container content = aWindow.getContentPane(); FlowLayout flow = new FlowLayout(); // Create a layout manager content.setLayout(flow); // Set the container layout mgr

43 Layout Manager Heuristics Left to right, Top to bottom c n s ew FlowLayoutGridLayout BorderLayout none, programmer sets x,y,w,h null One at a time CardLayout GridBagLayout JButton

44 FlowLayout It simply lays out components from left to right, starting new rows if necessary

45 GridLayout GridLayouts simply make a bunch of Components have equal size, displaying them in the requested number of rows and columns

46

47 LAB TASK 1 Create a window Specify its bounds Specify its default close operation Specify its background color Specify its Cursor style Specify its layout Add 2 components 1.Button 2.RadioButton

48 LAB TASK 2 Create a window Specify its bounds Specify its default close operation Specify its layout Add 6 buttons through for loop. Add 1 Label to it

49 LAB TASK 3 Create a window Specify its bounds Specify its default close operation Add Menu Bar Add 2 menus 1.File 2.Elements Add 4 menu Items to File menu New, Open, Close, Save Add 4 radio button menu Items to Elements menu Line, Rectangle, Circle, Curve

50 LAB TASK 4

51 Creating and Showing Frames Example //1. Create the frame. JFrame frame = new JFrame("FrameDemo"); //2. Optional: What happens when the frame closes? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //3. Create components and put them in the frame. //...create emptyLabel... frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //4. Size the frame. frame.pack(); //5. Show it. frame.setVisible(true);

52 Creating and Showing Frames Example import java.awt.*; import javax.swing.*; public class H{ private static void createAndShowGUI() { JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel("hiiiiiiiiiii"); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel,BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { createAndShowGUI(); } }


Download ppt "GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of."

Similar presentations


Ads by Google