JFrame添加阴影



JFrame添加阴影

如 果您想突出显示一个 “about” 对话框的标题文字,可以考虑以一定的偏移量和指定的颜色绘制背景文字以达到投放“阴影”的效果。 选择一个适当的颜色做为背景字的颜色,注意与前景文字和背景的颜色搭配。 然后使用反失真技术使边缘上的小锯齿变得平滑。 效果如图 1 所示:

添加阴影 - alpsdyk2001 - 编程学习!

图 1:阴影可以强调对话框的标题

图 1 展示了一个具有蓝色文字、黑色阴影和白色背景的 “about” 对话框。 该对话框是在 DE>AboutBox2DE> 程序的 DE>AboutBox(JFrame frame, String centerMode)DE> 构造函数内创建的。 由于该程序的代码与 DE>AboutBox1.javaDE> 极为相似,所以我只给出其构造函数:

 DE>AboutBox (JFrame frame, String centerMode)
{
   super (frame, "AboutBox", true /* modal */);

   // Add a panel that presents some text to the dialog box's content pane.

   getContentPane ().add (new JPanel ()
                          {
                              final static int SHADOW_OFFSET = 3;

                              {
                                 // Establish the drawing panel's preferred
                                 // size.

                                 setPreferredSize (new Dimension (250, 100));

                                 // Create a solid color border that both
                                 // surrounds and is part of the drawing
                                 // panel. Select the panel background
                                 // color that is appropriate to this look
                                 // and feel.

                                 Color c = 
                                   UIManager.getColor ("Panel.background");
                                 setBorder (new MatteBorder (5, 5, 5, 5, c));
                              }

                              public void paintComponent (Graphics g)
                              {
                                 // Prevent jagged text.

                                 ((Graphics2D) g).setRenderingHint
                                   (RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);

                                 // Because the border is part of the panel,
                                 // we need to make sure that we don't draw
                                 // over it.

                                 Insets insets = getInsets ();

                                 // Paint everything but the border white. 

                                 g.setColor (Color.white);
                                 g.fillRect (insets.left, insets.top,
                                             getWidth ()-insets.left-
                                             insets.right,
                                             getHeight ()-insets.top-
                                             insets.bottom);

                                 // Select an appropriate text font and 
                                 // obtain the dimensions of the text to be 
                                 // drawn (for centering purposes). The
                                 // getStringBounds() method is used instead
                                 // of stringWidth() because antialiasing is
                                 // in effect -- and the documentation for
                                 // stringWidth() recommends use of this
                                 // method whenever the antialiasing or
                                 // fractional metrics hints are in effect.

                                 g.setFont (new Font ("Verdana",
                                                      Font.BOLD,
                                                      32));
                                 FontMetrics fm = g.getFontMetrics ();
                                 Rectangle2D r2d;
                                 r2d = fm.getStringBounds ("About Box", g);
                                 int width = (int)((Rectangle2D.Float) r2d)
                                             .width;
                                 int height = fm.getHeight ();

                                 // Draw shadow text that is almost
                                 // horizontally and vertically (the
                                 // baseline) centered within the panel.

                                 g.setColor (Color.black);
                                 g.drawString ("About Box",
                                               (getWidth ()-width)/2+
                                               SHADOW_OFFSET,
                                               insets.top+(getHeight()-
                                               insets.bottom-insets.top)/2+
                                               SHADOW_OFFSET);

                                 // Draw blue text that is horizontally and
                                 // vertically (the baseline) centered
                                 // within the panel.

                                 g.setColor (Color.blue);
                                 g.drawString ("About Box",
                                               (getWidth ()-width)/2,
                                               insets.top+(getHeight()-
                                               insets.bottom-insets.top)/2);
                              }
                          }, BorderLayout.NORTH);

   final JButton btnOk = new JButton ("Ok");
   btnOk.addActionListener (new ActionListener ()
                            {
                                public void actionPerformed (ActionEvent e)
                                {
                                   dispose ();
                                }
                            });
   getContentPane ().add (new JPanel () {{ add (btnOk); }},
                          BorderLayout.SOUTH);

   pack ();

   setLocationRelativeTo (centerMode.equals ("W") ? frame : null);
}DE>

除了向您介绍如何在 DE>JPanelDE> 子类组件的 DE>public void paintComponent(Graphics g)DE> 方法中呈现阴影以外,构造函数还揭示了一个技巧:使用 DE>UIManager.getColor(“Panel.background”)DE> 获取与对话框背景色匹配的组件边框的颜色(即现在的外观)。