Unit 3 GUI Concepts
Unit 3 GUI Concepts
NICOLAS
NEUST
1. To be able to understand:
1. Swing and awt Components
2. To be able to use:
1. JFrame class
2. JLabel class
3. Layout manager(namely FlowLayout)
4. JTextFields, JButtons and tooltips to add into a
JFrame
What is Graphical User
Interface(GUI)?
• Commonly pronounced as gooey.
example:
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Sample code for JFrame
Sample output of JFrame
JLabel
• Predefined class by the swing library.
• A component that appears in JFrame as a text and
can’t be edited.
• To create new instance:
format: JLabel identifierForLabel = new
JLabel(“display text");
format:
identifierForFrame.add(nameOfLabel);
Example : frame1.add(label1);
Modifying JLabel
• Modifying Jlabel can be done by Font method
• Font method is from awt package.
format:
Font identifierForFont = new Font(“fontType”, Font.EMPHASIS,
fontsize:int);
code:
Font font1 = new Font(“Times New Roman”, Font.BOLD, 40);
Modifying JLabel
Step 3: Lastly apply the Font method to chosen
Jlabel using setFont
format: identifierForLabel.setFont(nameOfFont);
code: label1.setFont(font1);
code:
JTextField textField1 = new JTextField(3);
JTextField
• adding JTextField into JFrame
format:
identifierForFrame.add(identifierForJTextField);
code:
frame1.add(textField1);
• note: data that will be stored on JTextField is Character,String on
default. So if you want to calculate numbers you have to use parse()
method.
JButton
Buttons are components that is used to trigger an
action or make a selection when the
user clicks it.
JButton is a component of swing class that lets the
developer create a button.