这里一些自定义 JPanel 用于使用静态输入值进行扩展和收缩。
Java timer 用于延迟和加快面板显示时间。
public class SlidingPanel extends JPanel {
private int paneHeight;
private int paneWidth;
private int comHeight;
private int comWidth;
private Timer everyspeedmillisec;
/**custom class for slide up and down actions for JPanel
*
*/
public SlidingPanel(LayoutManager layout,Dimension panedim) {
super(layout);
setPreferredSize(panedim);
paneHeight = (int)panedim.getHeight();;
paneWidth = (int) panedim.getWidth();
}
/**function for expanding Jpanel
*
*/
public void expand_sft_det(int speed ,JPanel slidingcom)
{
//Add Jpanel for sliding
this.add(slidingcom);
//获取面板的高度和宽度,即我们想要显示的幻灯片高度。
comHeight=(int)slidingcom.getMinimumSize().getHeight();
comWidth=(int) slidingcom.getMinimumSize().getWidth();
//用一些静态值初始化计时器 - 中等速度)
everyspeedmillisec = new Timer(30, new ActionListener() {
private int count_timer=0;
public void actionPerformed(ActionEvent e) {
count_timer++;
if( paneHeight < comHeight){
setPreferredSize(new Dimension(paneWidth, paneHeight));
paneHeight+=20;
repaint();
revalidate();
}
else
everyspeedmillisec.stop() ;
}
});
everyspeedmillisec.start();
}
/** 缩小 Jpanel 的函数 * */
public void shrink_sft_det(int speed ,JPanel slidingcom) {
comHeight = (int)slidingcom.getMinimumSize().getHeight();
comWidth = (int)slidingcom.getMinimumSize().getWidth();
//滑动到顶部的高度
paneHeight=0;
everyspeedmillisec = new Timer(30, new ActionListener() {
private int count_timer=0;
public void actionPerformed(ActionEvent e) {
count_timer++;
if( paneHeight < comHeight){
setPreferredSize(new Dimension(comWidth,comHeight));
comHeight-=20;
repaint();
revalidate();
}
else
everyspeedmillisec.stop() ;
}
});
everyspeedmillisec.start();
}
//声明类并使用收缩onclick事件
public class SoftPanel extends JPanel implements ActionListener
, MouseListener {
JPanel MoredetPane;
JPanel SlidedetPane;
private JLabel SoftDOCLabel;
private JLabel SoftLocation;
private JLabel SoftUpdates;
private boolean mr_det_flag =true;
MoredetPane = new SlidingPanel(new GridLayout(1,1,5,5),new Dimension(300,1));
MoredetPane.setOpaque(false);
//Add some components
SlidedetPane = new JPanel(new GridLayout(3,2,50,25));
SlidedetPane.setPreferredSize(new Dimension(300,200));
SlidedetPane.setOpaque(false);
SoftDOCLabel = new JLabel("Software Date") ;
SoftLocation = new JLabel("Software Location") ;
SoftUpdates = new JLabel("Software Updates") ;
SlidedetPane.add(SoftDOCLabel);
SlidedetPane.add(new JLabel(softbean.getSoftDOC()));
SlidedetPane.add(SoftLocation);
SlidedetPane.add(new JLabel(softbean.getSoftPath()));
SlidedetPane.add( SoftUpdates);
SlidedetPane.add(new JLabel(""));
//Onclick events
modedetails_Label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent clickeve) {
if(mr_det_flag){
mr_det_flag=false;
((SlidingPanel)MoredetPane).expand_sft_det(200,SlidedetPane);
add(SlidingPanel);
}
else{
mr_det_flag=true;
((SlidingPanel) MoredetPane).shrink_sft_det(0,SlidedetPane);
}
});
add( MoredetPane );
}