Packages and GUI
Packages and GUI
Day 3 - morning
Packages
2. Use directly
Example:
java.awt.Color
User defined packages
• Example
– import <PackageName>;
– import <PackageName>.<ClassName>;
NetBeans package folders will be created inside src
folder.
How to import
• Example
public interface MyInterface{
void PrintName();
}
Lambda Expression
• Syntax
– () -> { body of the abstract method}
– (parameters) -> { body of the abstract method}
– Three parts:
• () - input parameters
• -> arrow token
• {} - abstract method definition
Regular code vs. lambda expression
public interface MyInterface { public interface MyInterface {
public void PrintHello(); public void PrintHello();
} }
public static void main(String[] args) { public static void main(String[] args) {
obj.PrintHello("Randima");
}
}
Lambda with two parameters
public interface MyInterface {
public int AddTwoNumber(int a,int b);
}
public class LambdaExampleOne {
import javax.swing.*;
• Border Layout
– Arrange the components to five regions in the frame
• North
• South
• West
• East
• Center
– Each region will have only one component.
Layouts
• Flow Layout
– This is the default layout arrangment
– Laying the components from left to right within the JPanel
– However, three alignments are available
• Leading
• Trailing
• Center (default)
Layouts
• Grid Layout
– Components are placed in a grid layout.
– A cell of the grid holds only one component
– Layout adds the components from left to right and top to bottom.
• Gridbag Layout
– This is just like the Gridbag layout
– Except, components can expand to multiple columns and rows
– Columns can have different widths.
Swing Component Classes
Create a component and add it to the frame
//add a button
JButton addButton = new JButton("Add");
JButton clearButton = new JButton("Clear");
Component that generate Contain methods to handle All the Events in Java and
the event. the events which will be event classes associated
informed via event with them.
A component can have notifications.
multiple events Two important methods:
This is from javax.awt.event - getSource()
Example package. - toString()
- Button
- Menu
- Radio button
Partners in Event handling (examples)
Event Listener Listener Method
Action Event ActionListener void actionPerformed(ActionEvent e)
Eg: button press, list item selected
Key Event KeyListener void keyPressed(KeyEvent e)
Eg: input is received from keyboard void keyReleased(KeyEvent e)
void keyTyped(KeyEvent e)
Mouse Event MouseListener void mouseClicked(MouseEvent e)
Eg: mouse is dragged, clicked, void mouseEntered(MouseEvent e)
moved, etc void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
Item Event ItemListener void itemStateChanged(ItemEvent e)
Eg: check-box or list item is clicked
Text Event TextListener void textValueChanged(TextEvent e)
Eg: text area or text field is changed
Implement Event handling in Java
new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(ButtonFrame.this, "You
have clicked the button.");
}
}
);
Display a message
Radio button listener
Radio button listener