Java CS tab Click to Toggle tab Implementation Panel

Source: Internet
Author: User
Tags int size

The code achieves the following effect

The main interface class is as follows

Import java.awt.Component;
Import java.awt.Dimension;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.InputEvent;
Import java.awt.event.KeyEvent;

Import Javax.swing.JCheckBoxMenuItem;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JPanel;
Import Javax.swing.JTabbedPane;
Import Javax.swing.KeyStroke;
Import javax.swing.SwingUtilities;

Import Test. Buttontabcomponent;

public class Tabcomponentsdemo extends jframe{

Private JTabbedPane pane = new JTabbedPane ();
Private JMenuItem Scrolltabitem;
Private JMenuItem Componenttabitem;
Private final int numtab = 5;

Public Tabcomponentsdemo (String title) {
Set Frame title Name
Super (title);
Set shutdown mode
Setdefaultcloseoperation (Jframe.exit_on_close);
Create a menu bar
Initmenu ();
Adding Tabpane to a frame
Add (pane);

}

Initializes the TabPanel related configuration and is used for resize calls
public void Runtest () {
Set the label with the Close button
Componenttabitem.setselected (TRUE);

Set label
for (int i = 0; i < Numtab; i++)
{
Label Signature
String name = "tab" +I;
Create a label
Pane.add (name,createcontent (name));
Initialize the text and button on the label
Inittabcomponent (i);
}


To set the size of a frame
SetSize (New Dimension (500,500));
Put the frame in the center of the screen
Setlocationrelativeto (NULL);
Show frame
SetVisible (TRUE);


}

Create a Label Content section
Private Component createcontent (String name) {
Create a panel, and set the layout to a sub-block
JPanel panel = new JPanel (new GridLayout);
Create a label
JLabel label = new JLabel (name);
Set the text format of a label
Label.sethorizontalalignment (Jlabel.center);
Put a label in a panel
Panel.add (label);
return panel;
}

Initialize the label header with the Close button
private void inittabcomponent (int i) {
Use this function to initialize the head of the label
Pane.settabcomponentat (i, New Buttontabcomponent (pane));
}

Create a menu bar
private void Initmenu () {
Create a menu bar
JMenuBar MB = new JMenuBar ();

Create overlapping tab options
Scrolltabitem = new JCheckBoxMenuItem ("overlapping tab");
Set shortcut keys
Scrolltabitem.setaccelerator (Keystroke.getkeystroke (keyevent.vk_s,inputevent.ctrl_mask));
Setting Up Listener Events
Scrolltabitem.addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent arg0) {
if (pane.gettablayoutpolicy () = = Jtabbedpane.scroll_tab_layout)
Pane.settablayoutpolicy (jtabbedpane.wrap_tab_layout);
Else
Pane.settablayoutpolicy (jtabbedpane.scroll_tab_layout);
}
});

Set a menu for a tab that can be closed
Componenttabitem = new JCheckBoxMenuItem ("Set tab that can be closed");
Componenttabitem.setaccelerator (Keystroke.getkeystroke (keyevent.vk_c,inputevent.ctrl_mask));
Componenttabitem.addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent e) {
for (int i = 0; i < Numtab; i++)
{
if (componenttabitem.isselected ())
Inittabcomponent (i);
Else
Pane.settabcomponentat (i, NULL);
}
}
});

Set Reset Label
JMenuItem ResetItem = new JMenuItem ("reset");
Resetitem.setaccelerator (Keystroke.getkeystroke (keyevent.vk_r,inputevent.ctrl_mask));
Resetitem.addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent e) {
Runtest ();
}
});

Create a Menu
JMenu menu = new JMenu ("option");
Add Menu item
Menu.add (Componenttabitem);
Menu.add (Scrolltabitem);
Menu.add (ResetItem);

Add Menu
Mb.add (menu);
Add a menu bar (note that a frame can have only one menu bar, so use set)
Setjmenubar (MB);

}

public static void Main (string[] args) {
Swingutilities.invokelater (New Runnable () {

@Override
public void Run () {
New Tabcomponentsdemo ("tab test can be closed"). Runtest ();
}
});
}


}

Another class that will be used is as follows:

Package test;


Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import java.awt.Component;
Import java.awt.Dimension;
Import Java.awt.FlowLayout;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;

Import Javax.swing.AbstractButton;
Import Javax.swing.BorderFactory;
Import Javax.swing.JButton;
Import Javax.swing.JLabel;
Import Javax.swing.JPanel;
Import Javax.swing.JTabbedPane;
Import Javax.swing.plaf.basic.BasicButtonUI;

public class Buttontabcomponent extends jpanel{
Private final JTabbedPane pane;

Public buttontabcomponent (final JTabbedPane pane) {
Set the label header to horizontal, left-to-right, and the component's spacing is 0 (Landscape and portrait)
Super (New FlowLayout (flowlayout.left, 0, 0));

if (pane = = null)
throw new NullPointerException ("pane can not is null");
This.pane = pane;

Don't draw the panel's boundaries
Setopaque (FALSE);

Create the text content of the label header
JLabel label = new JLabel () {
Override the method, return the name of the pane specified position, here a bit around, you can understand the understanding
Why not set the contents of the label directly, but instead modify the label content by overriding the function
Probably want business logic to try not to scatter on the outside
@Override
Public String GetText () {
You can get the position of the current Panel in tab
int i = pane.indexoftabcomponent (buttontabcomponent.this);
if (i! =-1)
Get the name of the current panel (actually the name of the tab)
return pane.gettitleat (i);
return null;
}
};

Label.setborder (Borderfactory.createemptyborder (0,0,0,5));
Add (label);
Create a Close button (that is, the difference button)
JButton button = new Tabbutton ();
Add (button);
SetBorder (Borderfactory.createemptyborder (2,0,0,0));
}

Private class Tabbutton extends JButton implements actionlistener{
Public Tabbutton () {
Set the size of the key
final int size = 17;
Setpreferredsize (New Dimension (size,size));
Set the key information
Settooltiptext ("close window");
Set the key to draw the same as the normal button
Setui (New Basicbuttonui ());
Do not fill the button, that is, the key is transparent
Setcontentareafilled (FALSE);
button does not get focus
Setfocusable (FALSE);
Set the border of a key as a carving style
SetBorder (Borderfactory.createetchedborder ());
The system does not automatically draw the key boundary (this border is drawn after the mouse is placed)
Setborderpainted (FALSE);

Add a Keystroke click event
addActionListener (Tabbutton.this);
Add mouse events (mainly mouseover and mouse exit)
Addmouselistener (MouseListener);

}

Overriding the draw function of a button
@Override
protected void Paintcomponent (Graphics g) {
Super.paintcomponent (g);

Create a graphics2d because you need to draw a difference on the button
graphics2d g2 = (graphics2d) g.create ();

Set the brush with a width of 2
G2.setstroke (New Basicstroke (2));
Set Brush color
G2.setcolor (Color.Black);
The brush is purple when you move the mouse over the button
if (Getmodel (). Isrollover ())
G2.setcolor (Color.pink);
Draw Poor
int delta = 6;
G2.drawline (Delta, Delta, GetWidth ()-Delta-1, GetHeight ()-delta-1);
G2.drawline (GetWidth ()-Delta-1, Delta, Delta, GetHeight ()-delta-1);
Releasing brush resources
G2.dispose ();
}

@Override
public void actionperformed (ActionEvent arg0) {
When the button is clicked, close the current tab
int i = pane.indexoftabcomponent (buttontabcomponent.this);
if (i! =-1)
Pane.remove (i);
}

Private final MouseListener MouseListener = new Mouseadapter () {
@Override
public void mouseentered (MouseEvent event) {
Mouse over button to draw key border
Component C = event.getcomponent ();
if (c instanceof AbstractButton)
((AbstractButton) c). Setborderpainted (True);
}

@Override
public void mouseexited (MouseEvent event) {
Mouse out button, do not draw key border
Component C = event.getcomponent ();
if (c instanceof AbstractButton)
((AbstractButton) c). Setborderpainted (false);
}

};

}

}

Java CS tab Click to Toggle tab Implementation Panel

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.