Java如何實現(xiàn)可折疊Panel方法示例
1.原理
原理非常簡單:就是一個JLabel和JPanel。Jlabel顯示標題文字以及標明控件當前是處于展開還是折疊狀態(tài)的圖片;而JPanel主要就一個作用——承載控件的容器。JLabel通過響應鼠標事件來控制JPanel是否顯示。這樣就可以達到折疊或展開的效果。
下面話不多說了,來一起看看詳細的示例代碼
2.代碼
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() {
@Override
public void mouseClicked(MouseEvent e) {
isExpanded = !isExpanded;
panelVisible();
}
@Override
public void mouseEntered(MouseEvent e) {
label.setOpaque(true);
label.repaint();
}
@Override
public 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.效果

panel展開鼠標在標題Label上

panel展開鼠標沒在標題Label上

panel折疊鼠標在標題Label上

panel折疊鼠標沒在標題Label上
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
springBoot集成redis(jedis)的實現(xiàn)示例
Redis是我們Java開發(fā)中,使用頻次非常高的一個nosql數(shù)據(jù)庫,本文主要介紹了springBoot集成redis(jedis)的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Windows10 Java環(huán)境變量配置過程圖解
這篇文章主要介紹了Windows10 Java環(huán)境變量配置過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07
Idea調(diào)用WebService的關鍵步驟和注意事項
這篇文章主要介紹了如何在Idea中調(diào)用WebService,包括理解WebService的基本概念、獲取WSDL文件、閱讀和理解WSDL文件、選擇對接測試工具或方式、發(fā)送請求和接收響應、處理響應結果以及錯誤處理,需要的朋友可以參考下2025-01-01
Springboot+Shiro+Jwt實現(xiàn)權限控制的項目實踐
如今的互聯(lián)網(wǎng)已經(jīng)成為前后端分離的時代,所以本文在使用SpringBoot整合Shiro框架的時候會聯(lián)合JWT一起搭配使用,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Java語言----三種循環(huán)語句的區(qū)別介紹
下面小編就為大家?guī)硪黄狫ava語言----三種循環(huán)語句的區(qū)別介紹。小編舉得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07

