Dr. Hikmat A. M. AbdelJaber
JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame. JFrame is a resizable, movable window with title bar and close button. Usually it contains JPanels. The entire application is usually a JFrame. JPanel is a region internal to a JFrame or another JPanel. Used for grouping components together. Optionally bounded by a visible border. Lives inside some enclosing Container.
Description. A JPanel is an empty area that can be used either to: 1. Layout other components, including other panels. 2. Draw graphics on. Use a JPanel to layout other components Declare and create the JPanel: Example: JPanel p = new JPanel();
Set the layout and other characteristics Example: p.setlayout(new BorderLayout()); p.setpreferredsize(new Dimension(200, 100)); p.setbackground(color.blue); You may want to set these characteristics in the constructor of a new class that extends JPanel. The default layout for a JPanel is FlowLayout, but it is a good idea to explicitly set it to avoid any problems with future changes in the default.
Add components Use add(...) to add components to a panel. Example: p.add(plusbutton, BorderLayout.NORTH); p.add(minusbutton, BorderLayout.SOUTH);
Constructors The most common JPanel constructor has no parameters. This creates a panel with the default layout (FlowLayout). Call setlayout() to change the layout. JPanel p = new JPanel(); p.setlayout(new FlowLayout()); You can optionally specify the layout in the Jpanel constructor call. JPanel p = new JPanel(new FlowLayout()); However, this doesn't work with BoxLayout, so calling setlayout() is still the preferred style of many programmers
Common Methods p.setlayout(layoutmanager layout); p.setpreferredsize(new Dimension(int width, int height)); p.setbackground(color c); p.add(some component); w = p.getwidth(); // Used for graphics, not components h = p.getheight(); // Used for graphics, not components
Example JPanel p = new JPanel(); p.setlayout(new GridLayout(4,3)); // 4 rows & 3 columns JButton bx = new JButton("Top left"); p.add(bx); // bx goes into first grid element.... // add more elements to the panel. Show the program: Step_by_Step_Package_Ch3 Adapter class: provides a default implementation (with an empty method body) of each method in the interface. Ex: WindowAdapter, KeyAdapter, MouseAdapter, etc.
All layout managers implement the interface LayoutManager by an object as a parameter to the method setlayout( ). Three ways to arrange components in a GUI: 1. Absolute positioning: Set container s layout to null Reposition all components (by resize event which can be processed by ComponentListener). 2. Visual programming in an IDE. 3. Layout managers: FlowLayout : default for JPanel BorderLayout : default for JFrame GridLayout GridBagLayout
It adds the components from left to right, and the order can be specified by using the index in add method. Ex. add(textfield, 2); The components are CENTER aligned by default. Use setalignment( FlowLayout.LEFT ).CENTER.RIGHT The LayoutManager interface method layoutcontainer(getcontentpane()) is used to rearrange the components on the JFrame based on the adjusted layout.
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; First Program FlowLayoutFrame.java public class FlowLayoutFrame extends JFrame private JButton leftjbutton = new JButton("Left"); private JButton centerjbutton = new JButton("Center"); private JButton rightjbutton = new JButton("Right"); private FlowLayout layout; public FlowLayoutFrame() super("flowlayout Demo"); layout = new FlowLayout(); // default alignment = CENTER setlayout(layout); add(leftjbutton); add(centerjbutton); add(rightjbutton);
leftjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.left); layout.layoutcontainer(getcontentpane()); ); centerjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.center); layout.layoutcontainer(getcontentpane()); ); rightjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.right); layout.layoutcontainer(getcontentpane()); );
import javax.swing.jframe; Second Program FlowLayoutTest.java public class FlowLayoutTest public static void main(string[] args) FlowLayoutFrame flowlayoutframe = new FlowLayoutFrame(); flowlayoutframe.setdefaultcloseoperation(jframe.exit_on_close); flowlayoutframe.setsize(300, 100); flowlayoutframe.setvisible(true);
Output
The default for JFrame. Arranges the components into 5 regions: NORTH, SOUTH, EAST, WEST, CENTER Show the program new BorderLayout( hgap, vgap); Horizontal gap Vertical gap By default, the component is added to CENTER. Ex. Add(button1); add(button1,borderlayout.center);
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class BorderLayoutFrame extends JFrame implements ActionListener private JButton[] buttons; private final String[] names = "Hide North","Hide South","Hide East", "Hide West","Hide Center"; private BorderLayout layout; public BorderLayoutFrame() super("borderlayout Demo"); layout = new BorderLayout(5,5); // hgap & vgap setlayout(layout); buttons = new JButton[names.length]; for( int count=0; count<names.length; count++) buttons[count] = new JButton(names[count]); buttons[count].addactionlistener(this); First Program BorderLayoutFrame.java
add(buttons[0], BorderLayout.NORTH); add(buttons[1], BorderLayout.SOUTH); add(buttons[2], BorderLayout.EAST); add(buttons[3], BorderLayout.WEST); add(buttons[4], BorderLayout.CENTER); public void actionperformed(actionevent e) for( JButton button: buttons) if(e.getsource()==button) button.setvisible(false); else button.setvisible(true); layout.layoutcontainer(getcontentpane());
import javax.swing.jframe; Second Program BorderLayoutTest.java public class BorderLayoutTest public static void main(string[] args) BorderLayoutFrame borderlayoutframe = new BorderLayoutFrame(); borderlayoutframe.setdefaultcloseoperation(jframe.exit_on_close); borderlayoutframe.setsize(500, 300); borderlayoutframe.setlocation(400, 200); borderlayoutframe.setvisible(true);
Output
Every component have the same width and height. Components are added row after row, from left to right cell. Show the program
import javax.swing.*; import javax.swing.*; public class GridLayoutFrame extends JFrame private final JButton button = new JButton("OK"); private final JLabel label = new JLabel("Label"); private final JTextField textfield = new JTextField("Text Field"); private final JCheckBox checkbox = new JCheckBox("chekBox"); private final JComboBox combobox = new JComboBox(); private final JList list = new JList(); private final GridLayout gridlayout; public GridLayoutFrame() super("gridlayout Demo"); gridlayout = new GridLayout(3,2,15,15);// 3 rows,2 columns,15 hgap,15 vgap setlayout(gridlayout);
add(button); add(label); add(textfield); add(checkbox); add(combobox); add(list); public static void main(string[] args) GridLayoutFrame gridlayout = new GridLayoutFrame(); gridlayout.setdefaultcloseoperation(jframe.exit_on_close); gridlayout.setsize(300, 200); gridlayout.setlocation(400, 200); gridlayout.setvisible(true);
Output
Modify the application by changing the Java icon/image located in left of the title bar to a lock icon/image. Do this task for both frames: password and ABC Bank.