您的位置:软件 > 开发者网络 > 开发工具 > Java > 正文
Java图形用户界面设计
[文章信息]
作者:文枫
时间:2003-05-16
出处:天极论坛
责任编辑:方舟
[文章导读]
Java技术也得到了越来越广泛的应用。而无论我们是采用J2SE、J2EE还是J2ME......
advertisement
热点推荐
· 11.15软件精选 制作MSI文件
· 闪客五周年之闪客光荣榜
· 用Winamp“品尝”无限音乐
· 网络加、解密的技术应用
· 闪客五周年之闪客光荣榜:zoron
[正文]

上一页  1 2 3  下一页

  GUI设计应用实例

  FlowLayout/GridLayout/BorderLayout的应用实例

  应用背景

  假设我们要编写一个简单的计算器JApplet,其基本界面如下

  解决方法

  通过其界面要求可知,我们可以通过将"BackSpace"和"Clear"JButton放置在一个JPanel(1)中,采用FlowLayout布局;将显示结果的JTextField和该JPanel一起放置到另外一个JPanel(2),采用GridLayout布局;而将其它的JButton则放置在另外一个JPanel(3)中,采用GridLayout布局;再将JPanel(2)和JPanel(3)加入该JApplet,即可实现界面需求。具体实现方法如下:

/**以FlowLayout布局JPanel(1)*/
JPanel p1 = new JPanel(new FlowLayout()); //默认组件从居中开始
//加入"BackSpace"和"Clear"JButton
p1.add(backButton);
p1.add(clearButton);
/**以GridLayout布局JPanel(2)*/
JPanel p2 = new JPanel(new GridLayout(2, 1)); //放置2行,每行1个组件
//加入显示结果的JTextField和JPanel(1)
p2.add(displayField);
p2.add(p1);
/**以GridLayout布局JPanel(3)*/
JPanel p3 = new JPanel(new GridLayout(4, 5)); //放置4行,每行5个组件
String buttonStr = "789/A456*B123-C0.D+=";
for (int i = 0; i < buttonStr.length(); i++)
this.addButton(p3, buttonStr.substring(i, i + 1));

//addButton方法
private void addButton(Container c, String s)
{
JButton b = new JButton(s);
if (s.equals("A"))
b.setText("sqrt");
else if (s.equals("B"))
b.setText("1/x");
else if (s.equals("C"))
b.setText("%");
else if (s.equals("D"))
b.setText("+/-");
b.setForeground(Color.blue);
c.add(b);
b.addActionListener(this);
}
/**以BorderLayout布局JApplet*/
this.setLayout(new BorderLayout());
this.add(p2, "North");
this.add(p3, "Center");


  这样,就一切OK啦。具体的实现代码可参见附件中的CalculateApplet.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(); //添加或移去组件后刷新界面
}

  通过该方法即可实现界面的动态刷新与调整。


上一页  1 2 3  下一页

·"WAP天极之IT新闻资讯,50万元等你拿"    ·天极WAP之游戏狂图,50万元等你下载


发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容阅读排行榜
  • 浅谈Java的输入输出流
  • 用Socket类实现HTTP协议客户端应用
  • 构建适用不同客户端的J2EE网络应用程序
  • 设计模式在EJB中的应用
  • 11.15软件精选 制作MSI文件
  • 闪客五周年之闪客光荣榜
  • 兰欣推出我国第一款网络游戏专用音箱
  • 玩家发现117亡灵种族Bug
  • [配置推荐]极品HIFI配置
  • 东瀛美少女COSPLAY(55)
  • 用Winamp“品尝”无限音乐
  • 迷你型PStwo全面透析专题
  • Advertisement