You are on page 1of 13

Java Programming : Sample GUI

Objects

Dr. May Phyo Thu


Lecturer

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Objectives

• To define a subclass of JFrame to implement a customized frame window.


• To write event-driven programs using Java's delegation-based event model.
• To write GUI application programs using GUI objects from the javax.swing
package.

Faculty of Computer Science


Graphical User Interface

• In Java, GUI-based programs are implemented by using classes from the


javax.swing and java.awt packages.

AWT Swing

Components are platform-dependent. Components are platform-independent.

Components are heavyweight. Components are lightweight.

less components more powerful components

doesn't follow MVC follows MVC

Faculty of Computer Science


Sample GUI Objects

JLabel JTextField

JCheckBox

JFrame

JButton

Faculty of Computer Science


Creating a Frame

• javax.swing.JFrame class is a type of container which inherits the java.awt.Frame


class.
• Top-level window, with border and a title bar.
• JFrame class has various methods which can be used to customize it.
• It works like the main window.
• Two ways to create a frame:
 By creating the object of JFrame class (association)
 By extending JFrame class (inheritance)

Faculty of Computer Science


Creating a Frame (Cont’d)

• import javax.swing.*;
• public class JFrameSubclass extends JFrame {
• public JFrameSubclass() {
• setTitle("First GUI Program");
• setSize(300, 200);
• setLocation(150, 250);
• setVisible(true);
• setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
• }

Faculty of Computer Science


Creating a Frame (Cont’d)

• public static void main(String[] args) {


• new JFrameSubclass();
•}
•}

Faculty of Computer Science


ContentPane

• In Java Swing, the layer that is used to hold objects is called content pane.
• GUI objects such as buttons, labels, textboxes are added to the content pane layer
of the container.
• getContentPane() method retrieves the content pane layer so that you can add
an object to it.

This gray area is the


content pane of this
frame.

Faculty of Computer Science


Changing the Background Color

• We can change the background color of a content pane.


• Container contentPane = getContentPane();
• contentPane.setBackground(Color.CYAN);

Faculty of Computer Science


JPanel

•  Swing’s lightweight container which is used to group a set of components


together. 
• JPanel is a pretty simple component which does not have a GUI (except visual
border).
• Use add(Componet) method to add components like JLabel, JTextField, JButton,
etc for the layout managers: FlowLayout, BorderLayout, GridLayout etc.
• JPanel panel=new Jpanel(new FlowLayout());
• JButton button=new Jbutton(“Click”);
• panel.add(button);

Faculty of Computer Science


JOptionPane

• JOptionPane class is used to provide standard dialog boxes such as message


dialog box, confirm dialog box and input dialog box.
• These dialog boxes are used to display information or get input from the user.  
• JOptionPane.showMessageDialog(f,"Hello, How are you?"); 

Faculty of Computer Science


Summary

• GUI-based programs are implemented by using classes from the javax.swing


packages.
• Learn about JFrame, and contentPane.
• Learn about JOptionPane and JPanel.

Faculty of Computer Science


Next Lecture

• Layout Manager
• FlowLayout
• BorderLayout
• GridLayout
• Absolute Positioning

Faculty of Computer Science

You might also like