jtabbedpane in java
The JTabbedPane
is a Swing container that allows the user to switch between a group of components by clicking on tabs with their titles. Each tab is associated with a different component, and the content of the tab is displayed when the tab is selected.
Here's an example of how to use JTabbedPane
:
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class TabbedPaneExample { public static void main(String[] args) { JFrame frame = new JFrame("Tabbed Pane Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); JTabbedPane tabbedPane = new JTabbedPane(); JPanel panel1 = new JPanel(); panel1.add(new JLabel("This is panel 1")); tabbedPane.addTab("Panel 1", panel1); JPanel panel2 = new JPanel(); panel2.add(new JLabel("This is panel 2")); tabbedPane.addTab("Panel 2", panel2); JPanel panel3 = new JPanel(); panel3.add(new JLabel("This is panel 3")); tabbedPane.addTab("Panel 3", panel3); frame.getContentPane().add(tabbedPane); frame.setVisible(true); } }
In this example, we create a JFrame
and a JTabbedPane
. We then create three JPanel
s, each with a JLabel
containing some text. We add the panels to the tabbed pane using the addTab()
method, which takes two parameters: the title of the tab and the component to be displayed in the tab. Finally, we add the tabbed pane to the content pane of the frame and make the frame visible.
When you run this example, you'll see a window with three tabs. Clicking on each tab displays the content associated with that tab.