Java图形用户界面设计

http://tech.ddvip.com   2006年11月24日    社区交流

本文详细介绍Java图形用户界面设计

  带工具栏和状态栏的GridLayout/BorderLayout应用实例

  实际问题

  在很多情况下我们需要动态设置工具栏和状态栏,看下面的应用实例:

  

  以上是在视图的工具栏和状态栏都被复选的时候,以下分别为某一个没选或都未选的情况。

  

  解决方法

  /**工具栏JToolBar采用从左开始的FlowLayout布局*/
JToolBar toolBar = new JToolBar();
toolBar.setBorderPainted(false); //不画边界
toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
/**窗体采用动态的BorderLayout布局,通过获取工具栏或状态栏的复选标记进行界面的动态调整*/
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); //设置统计窗口分隔条的方向
splitPane.setDividerLocation(300); //设置分隔条的位置
splitPane.setOneTouchExpandable(true);
JCheckBoxMenuItem toolBarItem = new JCheckBoxMenuItem("工具栏(T)", true);
JLabel statusLabel = new JLabel("当前统计目标:");
JCheckBoxMenuItem statusBarItem = new JCheckBoxMenuItem("状态栏(S)", true);
  /**设置系统窗体布局并动态设置工具栏和状态栏*/
private void setLayout()
{
if (toolBarItem.getState() &&am;' statusBarItem.getState())
{
this.getContentPane().add(BorderLayout.NORTH, toolBar);
this.getContentPane().add(BorderLayout.CENTER, splitPane);
this.getContentPane().add(BorderLayout.SOUTH, statusLabel);
}
else if (toolBarItem.getState() && !statusBarItem.getState())
{
this.getContentPane().add(BorderLayout.NORTH, toolBar);
this.getContentPane().remove(statusLabel);
}
else if (statusBarItem.getState() && !toolBarItem.getState())
{
this.getContentPane().add(BorderLayout.SOUTH, statusLabel);
this.getContentPane().remove(toolBar);
}
else if (!toolBarItem.getState() && !statusBarItem.getState())
{
this.getContentPane().remove(toolBar);
this.getContentPane().remove(statusLabel);
}
this.show(); //添加或移去组件后刷新界面
}
  通过该方法即可实现界面的动态刷新与调整。

责编:豆豆技术应用

正在加载评论...