JLayeredPane Continuing on Swing Components A layered pane is a Swing container that provides a third dimension for positioning components depth or Z order. The class for the layered pane is JLayeredPane. The higher the Z order, closer the component is to the top position within the container. The relationship between components at the same depth is determined by their positions within the depth. Depth Constants in JLayeredPane Depth Constants in JLayeredPane
depth = 2 change the depth of the ICE logo import java.awt.event.*; import java.awt.image.*; import java.applet.applet; import javax.imageio.*; import java.io.*; depth = 4 depth = 5 initial depth = 5 public class TestLayeredPaneApplet extends JApplet{ JLabel icelabel,label; Icon iceicon; JLayeredPane layeredpane; MyHandler handler = new MyHandler(); public void init(){ setmouseicongui(); setlayeredpanegui(); public void setmouseicongui(){ iceicon = new ImageIcon("ICE.png"); icelabel = new JLabel(iceIcon); setlayout(null); icelabel.setbounds(200,200, iceicon.geticonwidth(), iceicon.geticonheight()); layeredpane = getlayeredpane(); layeredpane.addmousemotionlistener(handler); layeredpane.add(icelabel,new Integer(5)); public void setlayeredpanegui(){ JPanel panel6 = new JPanel(); panel6.setbounds(120,120,200,200); panel6.setbackground(color.magenta); layeredpane.add(panel6,new Integer(6)); JPanel panel2 = new JPanel(); panel2.setbounds(10,10,200,200); panel2.setbackground(color.yellow); layeredpane.add(panel2,new Integer(2)); JPanel panel4 = new JPanel(); panel4.setbounds(60,60,200,200); panel4.setbackground(color.cyan); layeredpane.add(panel4,new Integer(4));
label = new JLabel("Current Depth for ICE = +layeredpane.getlayer(icelabel)); label.setbounds(2,2,150,20); layeredpane.add(label,new Integer(100)); JButton incbutton = new JButton("+"); incbutton.setbounds(155,2,50,50); incbutton.addactionlistener(handler); layeredpane.add(incbutton,new Integer(100)); JButton decbutton = new JButton("-"); decbutton.setbounds(210,2,50,50); decbutton.addactionlistener(handler); layeredpane.add(decbutton,new Integer(100)); class MyHandler implements MouseMotionListener, ActionListener{ public void mousemoved(mouseevent e){ icelabel.setlocation(e.getx()-iceicon.geticonwidth()/2, e.gety()-iceicon.geticonheight()/2); public void mousedragged(mouseevent e){ icelabel.setlocation(e.getx()-iceicon.geticonwidth()/2, e.gety()-iceicon.geticonheight()/2); Menus public void actionperformed(actionevent e){ JButton src = (JButton)e.getSource(); if(src.gettext().equals("+")){ layeredpane.setlayer(icelabel, layeredpane.getlayer(icelabel)+1,0); else{ layeredpane.setlayer(icelabel, layeredpane.getlayer(icelabel)-1,0); label.settext("current Depth for ICE = "+ layeredpane.getlayer(icelabel)); Menus are not placed with the other components in the UI but usually along the top of a window A menu bar contains one or more menus. Swing classes related to menus include (but not limited to) JMenuBar, JMenu, JMenuItem
JMenu Example: Using Menus JMenuBar JButton import java.awt.event.*; public class TestMenuBarApplet extends JApplet{ JMenuBar menubar; JMenu editmenu,aboutmenu; JMenuItem action1, action2, action3, action4; JTextArea text = new JTextArea(); JButton button1, button2; MyButtonHandler handler; JTextArea JMenuItem public void init(){ menubar = new JMenuBar(); editmenu = new JMenu("Edit"); menubar.add(editmenu); action1 = new JMenuItem("Action 1"); editmenu.add(action1); action2 = new JMenuItem("Action 2"); editmenu.add(action2); editmenu.addseparator(); action3 = new JMenuItem("Clear"); editmenu.add(action3); aboutmenu = new JMenu("About"); menubar.add(aboutmenu); action4 = new JMenuItem("Credit"); aboutmenu.add(action4); setjmenubar(menubar); button1 = new JButton("+"); button2 = new JButton("-"); menubar.add(button1); menubar.add(button2); handler = new MyButtonHandler(); setlayout(new BorderLayout()); add(text,borderlayout.center); class MyButtonHandler implements ActionListener{ public MyButtonHandler(){ action1.addactionlistener(this); : button2.addactionlistener(this); public void actionperformed(actionevent e){ AbstractButton src = (AbstractButton)e.getSource(); if(src.gettext().equals("clear")){ text.settext(""); else if(src.gettext().equals("+")){ text.setfont(text.getfont().derivefont( text.getfont().getsize2d()+5f)); else if(src.gettext().equals("-")){ text.setfont(text.getfont().derivefont( text.getfont().getsize2d()-5f)); else{ text.append(src.gettext()+" is selected.\n");
A GUI application starts with a frame. A frame is a top-level window with a border, a title bar, and control buttons. In the Swing framework, frames are provided by the JFrame class. Swing Applications frame control menu frame title bar frame control buttons Using JFrame in Swing Applications Observe the following code. The program creates and show an instance of JFrame with a white JPanel. public class SimpleFrame{ public static void main(string[] args){ JFrame mainframe = new JFrame("A simple JFrame example"); mainframe.setdefaultcloseoperation(jframe.exit_on_close); JPanel panel = new JPanel(); panel.setpreferredsize(new Dimension(400,400)); mainframe.add(panel); mainframe.pack(); mainframe.setvisible(true); Using JFrame in Swing Applications Using JFrame in Swing Applications mainframe.setdefaultcloseoperation(jframe.exit_on_close); panel.setpreferredsize(new Dimension(400,400)); This specifies what happens when mainframe is closed. The EXIT_ON_CLOSE operation exits the program when the user closes the frame. This is good for this program since the program has only one frame. setpreferredsize() set the preferred dimension of a JComponent mainframe.pack(); pack() sizes the frame so that all its contents are at or above their preferred sizes. Using pack() is preferable to setsize() or setbounds(), since it still leaves the layout manager in charge.
Dialogs A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Using JOptionPane, different kinds of dialogs can be simply created and customized. import java.awt.event.*; public class DialogTest extends JFrame{ JButton button1, button2, button3; public DialogTest(String s){ super(s); JPanel panel = new JPanel(); panel.setlayout(new GridLayout(3,1,5,5)); panel.setpreferredsize(new Dimension(200,200)); button1 = new JButton("1) Default Title and Icon"); button2 = new JButton("2) Custom Title, Warning Icon"); button3 = new JButton("3) Show Option Dialog"); panel.add(button1); panel.add(button2); panel.add(button3); MyButtonHandler handler = new MyButtonHandler(); add(panel); public static void main(string[] args){ DialogTest frame = new DialogTest("Various Dialogs"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); class MyButtonHandler implements ActionListener{ public MyButtonHandler(){ button1.addactionlistener(this); button2.addactionlistener(this); button3.addactionlistener(this); : public void actionperformed(actionevent e){ System.out.println("Action"); JButton src = (JButton)e.getSource(); switch(src.gettext().charat(0)){ case '1': JOptionPane.showMessageDialog(DialogTest.this, "Please go read the textbook."); break; case '2': JOptionPane.showMessageDialog(DialogTest.this, "You MUST read the textbook.","error", JOptionPane.ERROR_MESSAGE); break;
case '3': Object[] options = {"Okey!","No, thanks","cancel"; int n = JOptionPane.showOptionDialog(DialogTest.this, "Would you go reading?","a Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null, options,options[2]); System.out.println(n); break;