Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 11 Object-oriented programming: Graphical user interface Jin Sa.

Similar presentations


Presentation on theme: "Unit 11 Object-oriented programming: Graphical user interface Jin Sa."— Presentation transcript:

1 Unit 11 Object-oriented programming: Graphical user interface Jin Sa

2 Objectives GUI components – To create user interfaces using frames, panels, and some simple UI components Layout managers – To understand the role of layout managers – To use the FlowLayout, GridLayout, and BorderLayout Graphics – To use the Color class – To use the paintComponent method – To use the Graphics class

3 GUI components

4 AWT and Swing Java provides many predefined classes for building GUI applications. These classes are provided in two packages called: Abstract Windows Toolkit (AWT) and Swing. In Java, to write a GUI program, we derive new classes from those provided in AWT or Swing.

5 The overall structure of AWT and Swing Graphics – Drawing, fonts, colour etc. Components – buttons, text fields, menus and scroll bars etc Layout managers – Arrangement of GUI components Event handlers – Event such as clicking button and corresponding actions Image manipulation.

6 Creating a frame import javax.swing.*; public class MyFrame1 extends JFrame{ MyFrame1(String title) { setTitle(title); setSize(400,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame1 frame = new MyFrame1("MyFrame"); }

7 Student activity 11.1 Implement and run the above example.

8 Adding components to a frame Container contentPane = getContentPane(); //obtain the content pane of the frame JButton ok = new JButton("first_ok_button"); //create a button object container.add(ok);

9 Student activity 11.2 Read the page on “Adding components to a frame” Add a button to the frame.

10 Effect of Using ContentPane 1 public class MyFrame_color extends JFrame{ MyFrame_color(String title) { setTitle(title); setSize(400,300); setBackground(Color.BLUE);//sets the frame colour??? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame_color frame = new MyFrame_color("MyFrame"); }

11 Effect of Using ContentPane 2 public class MyFrame_color extends JFrame{ MyFrame_color(String title) { setTitle(title); setSize(400,300); getContentPane().setBackground(Color.BLUE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame_color frame = new MyFrame_color("MyFrame"); }

12 Some GUI Components Component is the superclass of Container – Container is the superclass of JComponent JComponent is the superclass of AbstractButton – AbstractButton is the superclass of JButton JComponent is the superclass of JTextComponent – JTextComponent is the superclass of JTextField – JTextComponent is the superclass of JTextArea JComponent is the superclass of JLabel

13 The Component class provides methods such as setting and getting background colours. The Container class provides methods for adding component to the container; setting layout manager; and painting the component.

14 JButton Mostly used constructors. – JButton(String text) – E.g. – JButton ok = new JButton("first_ok_button"); JButton also inherits all the methods in its super classes such as setBackgroundColor

15 JTextField A JTextField: user input area. Commonly used constructors and methods are: – JTextField(int columns) – Creates an empty text field with the specified number of columns. – JTextField(String text) – Creates a text field initialized with the specified text. Mostly used methods: – getText() – Returns the string from the text field. – setText(String text) – Puts the given string in the text field.

16 JLabel A JLabel is a display area for a short text. Constructors and methods include the following: – JLabel() – JLabel(String text) – setText(String text) – getText()

17 Container containers can contain a number of components, e.g. frames and panels, ContentPane We can use the add method to put components in a container: Container contentPane = getContentPane(); JButton ok = new JButton("first_ok_button"); contentPane.add(ok);

18 JPanel JPanel is a subclass of Jcomponent Used as container or sub-containers for grouping user interface components Often programmers use a JPanel instead of the Content Pane when adding components to a frame as JPanel is a subclass of JComponent; it provides more methods than the ContentPane

19 Student activity 11.4 Read the page on Container. Use JPanel. Add a JButton to the JPanel instead of to the ContentPane.

20 Layout Manger Controls how the GUI components within the container can be arranged Java provides a number of predefined layout managers. – FlowLayout – GridLayout – BorderLayout Containers has a method – setLayout(LayoutManager)

21 FlowLayout FlowLayout is the simplest layout manager. The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started.

22 FlowLayout: Fragment of code public class ShowFlowLayout extends JFrame { public ShowFlowLayout() { Container container = getContentPane(); container.setLayout(new FlowLayout()); for (int i = 1; i <= 10; i++) container.add(new JButton("Component " + i)); } public static void main(String[] args) { ShowFlowLayout frame = new ShowFlowLayout(FlowLayout.LEFT); …… }

23 Output from the program

24 Student activity 11.5

25 GridLayout The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on.

26 GridLayout: Fragment of code public class ShowGridLayout extends JFrame { public ShowGridLayout() { Container container = getContentPane(); container.setLayout(new GridLayout(3,2)); container.add(new JLabel("First Name")); container.add(new JTextField(8)); container.add(new JLabel("MI")); container.add(new JTextField(1)); container.add(new JLabel("Last Name")); container.add(new JTextField(8)); } …… }

27 Output from the program

28 Student activity 11.6 Implement the GridLayout program

29 BorderLayout Divides the container into five areas: East, South, West, North, and Center. To add a component to a particular area, we use the add(Component, constraint) method, where constraint is – BorderLayout.EAST, – BorderLayout.SOUTH, – BorderLayout.WEST, – BorderLayout.NORTH, – BorderLayout.CENTER

30 BorderLayout: code fragment public class ShowBorderLayout extends JFrame { public ShowBorderLayout() { Container container = getContentPane(); container.add(new JButton("East"), BorderLayout.EAST); container.add(new JButton("South"), BorderLayout.SOUTH); container.add(new JButton("West"), BorderLayout.WEST); container.add(new JButton("North"), BorderLayout.NORTH); container.add(new JButton("Center"), BorderLayout.CENTER); } ……

31 Output from program

32 An integrated case study

33 The Color class You can set colours for GUI components by using the java.awt.Color class. MyFrame(String title) { … … Container container = getContentPane(); JButton ok = new JButton("first_ok_button"); container.add(ok); ok.setBackground(Color.red); container.setBackground(Color.green); … … }

34 Drawing in Swing usually create a new class that extends JPanel and override the paintComponent () method The Graphics object g is created automatically by the Java Virtual Machine for every visible GUI component.

35 Methods in the Graphics object drawLine(50,50,100,60), draws a line from (50,50) to (100,60) drawRect(20,20,100,50), draws a rectangle with (20,20) as the top left corner; 100 is the length and 50 is the width. fillRect(20,20,100,50), draws a rectangle as above but filled with the current colour. drawOval(30,30,50,50), draws an oval bounded by an invisible rectangle whose top left corner is at (30,30); length is 50 and width is 50. drawString(“Hello”, 50,50), draws a string starting from the position (50,50). setColor(Color.red), sets of colour of the graphics object to be red.

36 paintComponenet public class MyCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(10,10,100,50); g.drawRect(10,80,100,50); }

37 Darwing: code fragment 1 public class MyExample extends JFrame { MyCanvas canvas = new MyCanvas(); JLabel heading = new JLabel("My drawing"); public MyExample() { Container container = getContentPane(); container.add(heading,BorderLayout.NORTH); container.add(canvas,BorderLayout.CENTER); }

38 Darwing: code fragment 2 public static void main(String[] args) { MyExample myDrawing = new MyExample(); myDrawing.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); myDrawing.setSize(300,300); myDrawing.setVisible(true); }

39 Student activity 11.8 (homework)

40 Summary How to create various GUI components, in particular, JFrame, JPanel, JButton, JTextfield, JLabel. How to use layout managers for containers, in particular, FLowLayout, GridLayout and BorderLayout. How to use the graphics object to draw graphics, in particular how to use the paintComponent method.


Download ppt "Unit 11 Object-oriented programming: Graphical user interface Jin Sa."

Similar presentations


Ads by Google