1. Principle
The principle is very simple: it is just a JLabel and JPanel. Jlabel displays the title text and pictures indicating whether the control is currently in an expanded or collapsed state; while JPanel mainly has one function - the container that carries the control. JLabel controls whether the JPanel is displayed by responding to a mouse event. This will achieve the effect of folding or expanding.
I won't say much below, let's take a look at the detailed sample code
2. Code
public class JShrinkablePanel extends JPanel {private JLabellabel;private Stringtitle ="";private JPanelcontentPanel =null;private boolean isExpanded =true;private JListlist =new JList();private IconiconExpand =null;private IconiconCollapse =null;public JShrinkablePanel(String title, JPanel contentPanel) {super();this.title = title;this.contentPanel = contentPanel;initComponents();initComponentsStatus();initLayout();initResources();unRegisterEvents();registerEvents();}private void initComponents() {this.label =new JLabel();}private void initComponentsStatus() {this.label.setHorizontalAlignment(JLabel.LEFT);this.label.setVerticalAlignment(JLabel.CENTER);this.label.setVerticalTextPosition(JLabel.CENTER);this.label.setBackground(this.list.getSelectionBackground());this.iconExpand =new ImageIcon("src/Resources/Expand.png");this.iconCollapse =new ImageIcon("src/Resources/Collapse.png");}private void initLayout() {this.setLayout(new GridBagLayout());this.add(this.label,new GridBagConstraints(0,0,1,1,1,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));this.add(this.contentPanel,new GridBagConstraints(0,1,1,1,1,0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));}private void initResources() {this.label.setIcon(this.iconExpand);this.label.setText(this.title);}private void unRegisterEvents() {this.label.removeMouseListener(this.mouseListener);}private void registerEvents() {this.label.addMouseListener(this.mouseListener);}private MouseListenermouseListener =new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {isExpanded = !isExpanded;panelVisible();}@Overridepublic void mouseEntered(MouseEvent e) {label.setOpaque(true);label.repaint();}@Overridepublic void mouseExited(MouseEvent e) {label.setOpaque(false);label.repaint();}};private void panelVisible() {this.contentPanel.setVisible(this.isExpanded);this.label.setIcon(this.isExpanded ?this.iconExpand : this.iconCollapse);}public static void main(String[] args) {JFrame jf =new JFrame("JShrinkablePanel");jf.setBounds(400,200,400,300);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel=new JPanel();panel.add(new JButton("Just for show"));panel.setBorder(BorderFactory.createTitledBorder("Border"));JShrinkablePanel scrollPane=new JShrinkablePanel("TestJShrinkablePanel",panel);jf.add(scrollPane);jf.setVisible(true);}}3. Effect
panel Expand the mouse on the title Label
panel expands the mouse not on the title Label
panel fold mouse on title Label
panel folding mouse is not on the title Label
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.