Getting GUI and Swing,
Head First Java Edition ii Chapter xii & xiii,
*You may learn about GUI and Swing in these two chapters. Let’s get started on the chapter.
GUI
- How to make a GUI ?
- Make a Jframe.(A JFrame is the object that represents a window on the screen. It’s where you put all the interface things like buttons, checkboxes, text fields, and so on)
JFrame frame = new JFrame();
2. Make a widget (button, text field, etc.) like this
JButton button = new JButton(“click me”);
3.Add the widget to the frame.(you cannot add the frame directly .You should add things to window pane.
frame.getContentPane().add(button);
4.Display it (give it a size and make it visible)
frame.setSize(300,300);
frame.setVisible(true);
*There are a ton of Swing components you can add using javax.swing package.You need to import that package.
GUI is likes,

*When we pressed “click me’’ button. But nothing happens .What you do?
*Then you need to do two things.
- A method to be called when the user clicks.
- A way to know when to trigger that method.
Getting a button’s ActionEvent,
*First, the ActionListener Interface must be implemented. After that, you can implement the actionPerformed() method from the actionListener Interface by registering with the button. The code demonstrates that,

Graphics,
*There are three different ways to arrange items on your graphical user interface. There are some,
- put widgets on a frame. It means you can Add buttons, menus, radio buttons to the frame.
frame.getContentPane().add(myButton);
2.Draw 2D graphics on a widget.You can Use a graphic object to paint on a widget.
graphics.fillOval(70,70,100,100);
3. put a JPEG on a widget .You can use own image for that.
graphics.drawImage(myPic,10,10,this);
*method. Consider the paintComponent() method as a means for the system to say, “Hey widget, it’s time to paint yourself.” If you want to draw a circle, you can use the paintComponent() method to do it. When the drawing panel in the frame is visible.
Things to do in paintComponent()
- Display a JPEG
- Paint a randomly-colored circle on a black background
The Graphic parameter’s object reference is an instance of the Graphics class.
public void paintComponent(Graphics g) { }
The object referenced by the ‘g’ parameter is actually a Graphics2D class instance.
- To cast a Graphics2D reference from a Graphics2D object like this
Graphics2D g2d = (Graphics2D) g;
- Methods you can call on a Graphics2D reference.Some of them are below
fill3DRect() ,draw3DRect() ,rotate() ,scale() ,shear() ,transform(), setRenderingHints().
Inner classes,
*Like thus, the inner class is enclosed within the curly braces of the outer class,

*The inner class can use the outer class’s private variables and methods as though they were defined in the inner class. As an example,

How to make an instance of an inner class,
*If you create an inner class from code in an outer class, the inner object will ‘bond’ with the instance of the outer class.

How to use Swing,
*Layout manager objects govern the size and location of widgets in a Java GUI. In this context, the swing components are referred to as widgets. JComponent. Swing. Almost everything you can put in a graphical user interface is built on javax. Swing components can hold other Swing components since they are nested.
*There are two types of names for components. They’re interactive elements, and they’re,
background components- JFrame and Jpanel.
Interactive components-Button, text field.
All component manager controls the components that are contained.
Layout Managers,
There are three layout managers.They are broder ,flow and box.
- Border Layout,
- Only one background component per region is divided by a BorderLayout manager, such as NORTH, SOUTH, EAST, WEST, and CENTER. Let’s look at an example of a button being added to the eastern region.You can use this for other region changing BorderLayout.EAST .

- Components laid out by this manager usually don’t get to have their preferred size.
- default layout for a frame
2. Flow layout,
- If a panel is added to the frame ,the Border Layout manager will be in control of the panel by default, and the Flow Layout manager will be in control of the components within it.
- components added left to right, wrapping to a new line when needed
- FlowLayout is the default layout manager for a panel.
3.Box layout,
- components will be lad top to bottom ,one per line.
- each component gets to have its own size, and the components are placed in the order in which they’re added.

References,
[1]Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021)