java 滚动显示信息_实现工具栏滚动显示的方法

工具栏ToolBar中可加入如JLabel、JPanel、JButton或JComboBox等很多可视化组件的对象,且默认为FlowLayout布局管理器,有时候因为编程的需要,我们要往里面插入很多个组件,如何显示出完整的组件列表成了一个问题,而ToolBar的对象放在JScrollPane对象中是显示不出来的,原因是它和JPanel一样是没有边框的,我的解决办法是,将要插入到工具栏的对象先放入集合中(如果全是相同类型的对象,放在数组中保存更好),再通过for循环依次加入到ToolBar对象中,在ToolBar外面设置一个按钮,编写响应该按钮的点击事件,当该按钮被点击一次时,通过数组下标(或集合索引)从ToolBar中remove()去掉一个组件,并调用revalidate()方法,让紧跟其后的组件移上去,添补删除后留下的空白,并把删除的那个组件重新用add()方法追加到工具栏尾部,这样循环删除和添加,就实现了工具栏的滚动效果,由于工具栏的FlowLayout布局,使它在未设置时只能向一个方向添加组件,故我的这个工具栏的滚动效果是单向的。

对于两向滚动,我考虑在反向滚动前重新设置工具栏的布局管理器,以改变其添加组件的方向,从而达到双向滚动的效果。具体能否实现,有待进一步编码检测。

在这里我把单向滚动的源代码记录如下:

/**DiaryEditToolBarPane类构建一个包含一个工具栏和一个控制滚动的按钮 */

public class DiaryEditToolBarPane extends JPanel implements ActionListener

{

JTextPane jTextPane;

DiaryEditToolBar diaryEditToolBar;

JPanel jPanelSouth = new JPanel();

JButton jButtonUp = new JButton(ConstValue.IMG_UP);

//JButton jButtonDn = new JButton(ConstValue.IMG_DN);

public DiaryEditToolBarPane(JTextPane jTextPane)

{

this.jTextPane = jTextPane;

diaryEditToolBar = new DiaryEditToolBar(this.jTextPane);

jButtonUp.setMargin(new Insets(0,0,0,0));

//jButtonDn.setMargin(new Insets(0,0,0,0));

jButtonUp.setActionCommand("up");

//jButtonDn.setActionCommand("down");

//jPanelSouth.setLayout(new GridLayout(2,1));

jPanelSouth.add(jButtonUp);

//jPanelSouth.add(jButtonDn);

this.setLayout(new BorderLayout());

this.add(diaryEditToolBar,BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

jButtonUp.addActionListener(this);

//jButtonDn.addActionListener(this);

}

public void setTextPane(JTextPane jTextPane)

{

this.jTextPane = jTextPane;

diaryEditToolBar.setTextPane(this.jTextPane);

}

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("up"))

{

diaryEditToolBar.moveUp();

}

/*

else if(e.getActionCommand().equals("down"))

{

diaryEditToolBar.moveDown();

}*/

}

}

/**定义一个具体实现工具栏组件布局与滚动的类DiaryEditToolBar  */

public class DiaryEditToolBar extends JToolBar

{/**用于保存工具按钮的对象数组*/

private BrimlessButton[] toolBarButton;

/**工具按钮增、删索引值*/

private static int index = 0;private void init()

{

this.setFloatable(false);

BrimlessButton[] temp =

{

toolBarButtonSave,toolBarButtonSave,toolBarButtonCut,

toolBarButtonCopy,toolBarButtonPaste,toolBarButtonFontStyle,

toolBarButtonFontSize,toolBarButtonFontForeground,toolBarButtonBackground,

toolBarButtonBold,toolBarButtonItalic,toolBarButtonUnderline,

toolBarButtonFlusLeft,toolBarButtonCenter,toolBarButtonFlushRight,

toolBarButtonUndo,toolBarButtonRedo,toolBarButtonIndentLeft,

toolBarButtonIndentRight,toolBarButtonURL,toolBarButtonImage,

toolBarButtonDateTime

};

toolBarButton = temp;

for(int i=0; i<22; i++)

{

this.addElement(toolBarButton[i]);

}}private void addElement(JButton jButton)

{

this.add(jButton);

}

private void delElement(JButton jButton)

{

this.remove(jButton);

}

/**删除工具栏开头的工具按钮,并将其添加到工具栏末尾,以实现工具栏向上滚动的效果*/

public void moveUp()

{

delElement(toolBarButton[index]);

this.revalidate();

addElement(toolBarButton[index]);

index = (++index)%22;

}

/**增加工具栏开头的工具按钮,以实现工具栏向下滚动的效果*/

public void moveDown()

{

//addElement(toolBarButton[index]);

//this.revalidate();

//repaint();

}}

posted on 2005-11-29 23:18 水秀清灵 阅读(1066) 评论(0)  编辑  收藏 所属分类: 学习笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值