Upload
pradip-kharbuja
View
550
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Slides
Citation preview
Topic 5 : More Complex User InterfacesDDOOCP
Swing Components (Classes)
› javax.swing
1. JOptionPane
2. JFrame
3. JButton
4. JTextField
5. Jlabel
6. JPasswordField
7. JTextArea
8. JRadioButton
9. JCheckBox
10.JComboBox
11.JPanel
12.JScrollBar
13.JScrollPane
14.JSlider
JOptionPane.showMessageDialog
JOptionPane.showMessageDialog
› Possible values for messageType are -1, 0, 1, 2, 3
Message Type arguments of showMessageDialog
1. JOptionPane.PLAIN_MESSAGE (no icon will be used) => -1
2. JOptionPane.ERROR_MESSAGE => 0
3. JOptionPane.INFORMATION_MESSAGE => 1
4. JOptionPane.WARNING_MESSAGE => 2
5. JOptionPane.QUESTION_MESSAGE => 3
JOptionPane.showMessageDialog
JOptionPane.showInputDialog
JOptionPane.showInputDialog
› WAP to take 2 inputs from user and display the sum.
JOptionPane.showConfirmDialog
JFrame› Constructors
– JFrame( )
– JFrame(String str)
› Methods
– Container getContentPane( )
– void setDefaultCloseOperation(int value)
– void remove(Component cm)
– void setLayout(LayoutManager manager)
› Other inherited Methods– setSize( )
– setVisible( )
– setTitle( )
– setBackground( )
– add( )
JFrame
JButton
› Constructors– JButton( )
– JButton(String str)
› Methods
– void setText(String str)
– void setEnabled(boolean b)
– String getText( )
– void addActionListener(ActionListener al)
JButton
Click event in JButton
JTextField
› Constructors JTextField( )
JTextField(String str)
› Methods void addActionListener(ActionListener al)
void removeActionListener(ActionListener al)
void setFont( )
• Inherited Methods
void setText(String t)
String getText( )
void setBackground(Color bg)
void setForeground(Color fg)
void setEditable(boolean b)
void copy( )
void cut( )
void paste( )
JLabel
› Constructors JLabel( )
JLabel(str)
› Methods String getText( )
void setText(String str)
• Inherited Methods
void setVisible(boolean visible)
void setEnabled(boolean enabled)
void setBounds(int x, int y, int width, int height)
Develop the following application.
JTextArea
› Constructors JTextArea( )
JTextArea(String str)
› Methods void append(String text)
void insert(String text, int position)
void setFont(Font f)
• Inherited Methods
String getText( )
void setText(String text)
void setVisible(boolean visible)
void setEnabled(boolean enabled)
TextAreaDemo
JPasswordField
› Constructors JPasswordField( )
JPasswordField(String str)
› Methods void copy( )
void cut( )
String getText( )
char[ ] getPassword( )
String getText( ) method is Deprecated.
• Inerited Methods
void setVisible(boolean visible)
void setEnabled(boolean enabled)
Login Logic
Login Form
Login Form
Login Form
JPanel
› Constructors JPanel( )
JPanel (LayoutManager layout)
› Inherited Methods
void setVisible(boolean visible)
void setEnabled(boolean enabled)
void setBackground(Color bg)
JCheckBox
› Constructors JCheckBox( )
JCheckBox(String text)
JCheckBox(String text, boolean selected)
› Inherited Methods
void addActionListener(ActionListener l)
void setText(String text)
void setVisible(boolean visible)
boolean isSelected()
void setSelected(boolean b)
JRadioButton
› Constructors JRadioButton( )
JRadioButton(String text)
JRadioButton(String text, boolean selected)
› Inherited Methods
void addActionListener(ActionListener l)
void setText(String text)
void setVisible(boolean visible)
boolean isSelected()
void setSelected(boolean b)
JRadioButtonDemo
JComboBox
› Constructors JComboBox( )
JComboBox(Object [ ] items)
› Methods void addActionListener(ActionListener l)
void addItem(Object item)
void removeAllItems()
void removeItem(Object item)
void removeItemAt(int index)
void setSelectedItem(Object anObject)
void setSelectedIndex(int anIndex)
Object getSelectedItem()
int getSelectedIndex()
JComboBoxDemo
JComboBoxDemo
JComboBoxDemo
JSlider› Constructors
JSlider( )
JSlider(int orientation)
JSlider.HORIZONTAL => 0
JSlider.VERTICAL => 1
JSlider(int min, int max)
JSlider(int min, int max, int value)
JSlider(int orientation, int min, int max, intvalue)
› Methods
void addChangeListener (ChangeListener l)
int getValue( )
void setValue(int n)
void setPaintLabels(boolean b)
void setMajorTickSpacing(int n)
Slider Demo
JSlider Important Code
Color Chooser
MeroSlider.java
ColorChooser.java
ColorChooser.java
JScrollPane
› ConstructorJScrollPane( )
JScrollPane(Component view)
JScrollPane(Component view, int vertical, int horizontal)
› Methodsvoid setLayout(LayoutManager layout)
Demo First
JScrollPane Example
Multiplication Table of 5
Layout Managers
1. GridLayout
2. NullLayout or Absolute Layout
3. FlowLayout
4. GridBagLayout
5. BorderLayout
6. BoxLayout
GridLayout
› Constructors GridLayout()
GridLayout(int rows, int columns)
GridLayout(int rows, int columns, int hgap, int vgap)
› Methods– void setColumns(int cols)
– void setRows(int rows)
– void setHgap(int hgap)
– void setVgap(int vgap)
GridLayout
FlowLayout
› Constructors FlowLayout()
FlowLayout(int align)
FlowLayout.LEFT => 0
FlowLayout.CENTER => 1
FlowLayout.RIGHT => 2
FlowLayout(int align, int hgap, int vgap)
› Methods– void setAlignment(int align)
– void setHgap(int hgap)
– void setVgap(int vgap)
Fonts
› public Font(String name, int style, int size)
S.N. Modifier and Type Field
1 static final int BOLD
2 static final int ITALIC
3 String name
4 Int size
Events
Event Type Listener Method
ActionEvent ActionListener void actionPerformed(ActionEvent e)
ChangeEvent ChangeListener void stateChanged(ChangeEvent e)
MouseEvent MouseListener void mouseClicked(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
Events
Event Type Listener Method
KeyEvent KeyListener void keyPressed(KeyEvent e)
void keyReleased(KeyEvent e)
void keyTyped(KeyEvent e)
FocusEvent FocusListener void focusGained(FocusEvent e)
void focusLost(FocusEvent e)
References
› http://www.java2s.com/Tutorial/Java/0240__Swing/UsingJOptionPanetoDisplayaMessage.htm
› http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
› http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html
› http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
› http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html