Java JLayeredPane
Java JLayeredPane is used to add the third dimension for positioning or dividing component into several layers.
How to Declare Java JLayeredPane
[class JLayeredPane extends JComponent implements Accessible
]
Java JLayeredPane constructors
JLayeredPane-used to create a new JLayeredPane.
Java JLayeredPane Methods
- int getIndexOf(Component c)-used to return certain component index that is selected.
- int getLayer(Component c)-this method is used to return attribute layer.
- int getPosition(Component c)-return component position in it layer.
Java JLayeredPane Example
[
import java.awt.Color; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.JButton; public class JavaJlayeredPaneExample extends JFrame { public JLayeredPane pane; JavaJlayeredPaneExample() { super("JavaJlayeredPaneExample"); setSize(400,400); pane=getLayeredPane(); JButton button1=new JButton("Button 1"); button1.setBackground(Color.red); button1.setBounds(10,10, 200, 30); pane.add(button1,new Integer(1)); JButton button2=new JButton("Button 2"); button2.setBackground(Color.BLUE); button2.setBounds(30, 60, 200, 30); pane.add(button2,new Integer(2)); JButton button3=new JButton("Button 3"); button3.setBounds(50, 120, 200, 30); button3.setBackground(Color.CYAN); pane.add(button3,new Integer(3)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public static void main(String args[]) { new JavaJlayeredPaneExample().setVisible(true); } }
]
Java JLayeredPane with ActionListener Example
[
import java.awt.Color; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JColorChooser; public class JavaJlayeredPaneExample extends JFrame implements ActionListener{ public JLayeredPane pane; public JButton button1,button2,button3; JavaJlayeredPaneExample() { super("JavaJlayeredPaneExample"); setSize(400,400); pane=getLayeredPane(); button1=new JButton("Button 1"); button1.setBackground(Color.red); button1.setBounds(10,10, 200, 30); button1.addActionListener(this); pane.add(button1,new Integer(1)); button2=new JButton("Button 2"); button2.setBackground(Color.BLUE); button2.addActionListener(this); button2.setBounds(30, 60, 200, 30); pane.add(button2,new Integer(2)); button3=new JButton("Button 3"); button3.setBounds(50, 120, 200, 30); button3.addActionListener(this); button3.setBackground(Color.CYAN); pane.add(button3,new Integer(3)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void actionPerformed(ActionEvent event) { if(event.getSource()==button1) { Color c=JColorChooser.showDialog(this,"Select Color", Color.yellow); button1.setBackground(c); } else if(event.getSource()==button2) { Color c=JColorChooser.showDialog(this,"Select Color", Color.yellow); button2.setBackground(c); } else if(event.getSource()==button3) { Color c=JColorChooser.showDialog(this,"Select Color", Color.yellow); button3.setBackground(c); } } public static void main(String args[]) { new JavaJlayeredPaneExample().setVisible(true); } }
]