2012-03-22 20:40:05|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:Java, Swing, 界面设计|来源:唯设编程网
在Windows程序中,状态栏起着提示用户的功能,比如提示用户当前程序的状态,鼠标位置等,合理的使用状态栏能够大大改善系统的用户体验,在使用Java语言的Swing客户端程序中,状态栏并不是Swing提供的一个标准控件。本文将介绍一种实现Swing程序状态栏的方案,本方法实现了立体的状态栏效果,并且在右下方模拟的常见Windows程序的三角形小图标。
首先给出程序的运行效果:
下面是具体的Java源代码:
Java示例:package Test; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.SystemColor; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * Swing状态栏测试例子 * */ public class StatusBarSimulator { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(200, 200, 600, 200); frame.setTitle("Status bar simulator"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new BorderLayout()); StatusBar statusBar = new StatusBar(); contentPane.add(statusBar, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } /** * Swing状态栏Panel * */ class StatusBar extends JPanel { private static final long serialVersionUID = 1L; public StatusBar() { setLayout(new BorderLayout()); setPreferredSize(new Dimension(10, 23)); JPanel rightPanel = new JPanel(new BorderLayout()); rightPanel.add(new JLabel(new AngledLinesWindowsCornerIcon()), BorderLayout.SOUTH); rightPanel.setOpaque(false); add(rightPanel, BorderLayout.EAST); setBackground(SystemColor.control); } protected void paintComponent(Graphics g) { super.paintComponent(g); //状态栏顶部阴影 int y = 0; g.setColor(new Color(156, 154, 140)); g.drawLine(0, y, getWidth(), y); y++; g.setColor(new Color(196, 194, 183)); g.drawLine(0, y, getWidth(), y); y++; g.setColor(new Color(218, 215, 201)); g.drawLine(0, y, getWidth(), y); y++; g.setColor(new Color(233, 231, 217)); g.drawLine(0, y, getWidth(), y); //状态栏底部阴影 y = getHeight() - 3; g.setColor(new Color(233, 232, 218)); g.drawLine(0, y, getWidth(), y); y++; g.setColor(new Color(233, 231, 216)); g.drawLine(0, y, getWidth(), y); y++; g.setColor(new Color(221, 221, 220)); g.drawLine(0, y, getWidth(), y); } }
/** * 状态栏右侧的图标 * */ class AngledLinesWindowsCornerIcon implements Icon { private static final Color WHITE_LINE_COLOR = new Color(255, 255, 255); private static final Color GRAY_LINE_COLOR = new Color(172, 168, 153); private static final int WIDTH = 13; private static final int HEIGHT = 13; public int getIconHeight() { return WIDTH; } public int getIconWidth() { return HEIGHT; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(WHITE_LINE_COLOR); g.drawLine(0, 12, 12, 0); g.drawLine(5, 12, 12, 5); g.drawLine(10, 12, 12, 10); g.setColor(GRAY_LINE_COLOR); g.drawLine(1, 12, 12, 1); g.drawLine(2, 12, 12, 2); g.drawLine(3, 12, 12, 3); g.drawLine(6, 12, 12, 6); g.drawLine(7, 12, 12, 7); g.drawLine(8, 12, 12, 8); g.drawLine(11, 12, 12, 11); g.drawLine(12, 12, 12, 12); } }到此,一个包含状态栏的Swing窗体演示程序便完成了,本实例中使用了JPanel模拟窗体的状态栏,通过边界布局模式将状态栏放在窗体的底部。同时,定义了一个继承自Icon的类AngledLinesWindowsCornerIcon实现了右下角的小图标。