Swings
Swings
Swings
Swing components are light weight components as they are written in java. Swing components are built on top of AWT. Swing components are consistent across all platforms they look and work alike in all platforms. Swing components are made available in javax.swing package.
swing components
Buttons
Common buttons Radio buttons Check buttons
Swing components
Combo boxes Lists Menus
Swing components
Spinners Sliders Textfields
Swing classes
Components you use to build a GUI app, are instances of classes contained in the javax.swing package:
JButton JTextField JRadioButton JCheckBox JComboBox JLabel etc
JFrame object
Container object
JTextField:
JTextField() JTextField(int cols) JTextField(String s, int cols) JTextField(String s)
and again
JList:
JList() JList(Vector v) Example:
String[] data = {"one", "two", "three", "four"}; JList dataList = new JList(data); dataList.add(five);
and again
JRadioButton:
JRadioButton(String s, Icon i, boolean state)
Example:
JRadioButton rb1= new JRadioButton(one); JRadioButton rb2= new JRadioButton(two); ButtonGroup bg= new ButtonGroup(); bg.add(rb1); bg.add(rb2);
Layout Managers
You use layout managers to design your GUIs. There are several managers like:
FlowLayout BorderLayout GridLayout CardLayout GridBagLayout
FlowLayout
Default layout Components laid out from the top-left corner, from left to right and top to bottom like a text.
BorderLayout
Places components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area
GridLayout
Simply makes a bunch of components equal in size and displays them in the requested number of rows and columns
CardLayout
lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays
GridBagLayout
is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell.
Layout Managers
Putting it into code:
Setting the manager:
Container myCont = getContentPane(); myCont.setLayout(new FlowLayout());
Adding Components:
myCont.add(aComponent, BorderLayout.WEST);