Java Swing's JTabbedPane implementation tab tab

Source: Internet
Author: User

This article will show you the Simple tab-label effect implementation

A component that allows a user to switch between a set of components by clicking a tab with a given title and/or icon.

addTab insertTab Add a tab/component to an object by using the and method TabbedPane . Tabs are represented by the index corresponding to the add location, where the index of the first tab is 0, and the index of the last tab is the number of tabs minus 1.

Example:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >import java.awt.gridlayout;import java.awt.event.keyevent;import Java.net.url;import Javax.swing.ImageIcon; Import Javax.swing.jframe;import Javax.swing.jpanel;import Javax.swing.jtabbedpane;import Javax.swing.swingutilities;public class Jtabbedpanedemo extends JPanel {private JTabbedPane JTabbedPane = new Jtabbedpan E ();//The component that holds the tab private string[] tabnames = {"Option 1", "option 2"};imageicon icon = Createimageicon ("images/middle.gif");p ublic J Tabbedpanedemo () {layoutcomponents ();} private void Layoutcomponents () {int i = 0;//the first label Jpaneljpanel Jpanelfirst = new JPanel ();//Jtabbedpane.addtab (tabname S[i++],icon,creatcomponent () (), "first");//Add to Page Jtabbedpane.addtab (tabnames[i++], icon, Jpanelfirst, "primary");// Join the first page jtabbedpane.setmnemonicat (0, keyevent.vk_0);//Set the shortcut key for the first position to 0//under the second tab jpaneljpanel Jpanelsecond = new JPanel () ; Jtabbedpane.addtab (tabnames[i++], icon, Jpanelsecond, "second");//Join First page Jtabbedpane.setmnemonicat (1, KeyeveNt. VK_1);//Set shortcut key to 1setLayout (new GridLayout (1, 1)); Add (JTabbedpane);} Private ImageIcon Createimageicon (String path) {URL url = JTabbedPaneDemo.class.getResource (path); if (URL = = null) {Syste M.out.println ("The image" + path + "is not exist!"); return null;} return new ImageIcon (URL);} /** * @param args */public static void main (string[] args) {Swingutilities.invokelater (new Runnable () {public void run () { Jframe.setdefaultlookandfeeldecorated (TRUE);//sets the build appearance to Java appearance JFrame frame = new JFrame (); frame.setlayout (null); Frame.setcontentpane (New Jtabbedpanedemo ()); Frame.setsize (+); frame.setvisible (true);//New Tabcomponentsdemo (). Runtest ();}});}} </span>
Results:


Java Swing's JTabbedPane implementation tab tab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.