首页 \ 问答 \ 应用投影的最佳方法是什么?(What's the best way to apply a drop shadow?)

应用投影的最佳方法是什么?(What's the best way to apply a drop shadow?)

应用阴影的最佳方法是什么? 我现在在一个网站上工作,我们有很多它们,但是,我一直在努力寻找最好的方法来做到这一点。 该网站的动画很重,因此阴影需要与此配合良好。

我尝试了一个jQuery阴影pulgin。 阴影看起来很好并且易于使用,但速度很慢,并且不适用于任何动画(需要大量重绘,非常慢跑)。

我也尝试创建自己的jQuery扩展,将我的元素包装在几个灰色的div中,然后稍微偏移它们以产生阴影效果。 这很好用。 它对动画的快速响应。 然而,它使得DOM操作/遍历变得麻烦,因为所有内容都包含在这些阴影div中。

我知道必须有一个更好的方法,但这不完全是我的强项。 思考?


What is the best method for applying drop shadows? I'm working on a site right now where we have a good deal of them, however, I've been fighting to find the best method to do it. The site is pretty animation heavy so shadows need to work well with this.

I tried a jQuery shadow pulgin. The shadows looked good and were easy to use but were slow and didn't work well with any animations (required lots of redrawing, very joggy).

I also tried creating my own jQuery extension that wraps my element in a couple gray divs and then offsets them a little bit to give a shadow effect. This worked well. It's quick and responsive to the animation. However, it makes DOM manipulation/traversal cumbersome since everything is wrapped in these shadow divs.

I know there has to be a better way but this isn't exactly my forte. Thoughts?


原文:https://stackoverflow.com/questions/239816
更新时间:2023-10-01 11:10

最满意答案

在将Panel添加到FinestradiRicerca之前,您需要将新元素添加到riquadroRicerca ,我建议您不要使用null布局,而是使用布局管理器或它们的组合。 如果您坚持保留null布局,请参阅下面的示例。 但对于这种应用程序,我建议使用CardLayout

我还建议不要使用多个JFrames因为它们会在用户不友好的任务栏上打开多个窗口。 请参阅: 使用多个JFrame,好/坏练习

另外,请遵循Java命名约定 。 例如,您将JFrame称为FinestradiRicerca而是将其重命名为: finestradiRicerca变量的第一个字母为小写)。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainWindow {
    // Seconda Finestra
    public static void NuovaFinestra (JPanel panel) {
        panel.setLayout(null);
        JButton Ricerca = new JButton("Ricerca");
        Ricerca.setBounds(100, 100, 200, 50);
        panel.add(Ricerca);
        Ricerca.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
                FinestradiRicerca.setBounds(300, 300, 500, 500);
                //If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
                FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel riquadroRicerca = new JPanel();
                JTextField ciao;
                JLabel myLabel = new JLabel("Here goes your label text");
                ciao = new JTextField ();
                ciao.setColumns(20);
                riquadroRicerca.add(myLabel);
                riquadroRicerca.add(ciao);
                FinestradiRicerca.add(riquadroRicerca);
                FinestradiRicerca.setVisible(true);
            }
        });
    }

    //Main  
    public static void main(String[] args) {
        //Finestra Principale
        JFrame finestra = new JFrame("Finestra principale");
        finestra.setSize(500, 500);
        finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //JPanel della finestra principale
        JPanel riquadro = new JPanel();
        finestra.add(riquadro);
        finestra.setVisible(true);
        NuovaFinestra(riquadro);
    }
}

因此,经过一些修改后,为了使JLabelJTextField可见,您的代码将提供以下输出:

在此处输入图像描述

但请遵循我的上述建议。


You needed to add your new elements to riquadroRicerca BEFORE adding the Panel to FinestradiRicerca, I recommend you NOT to use null layout but a Layout Manager or combinations of them. If you insist on keeping null layout then see below example. But for this kind of app I'd suggest a CardLayout.

I also suggest not using multiple JFrames since they will open multiple windows on task bar which is user unfriendly. See: Use of multiple JFrames, Good / Bad Practice

As an aside note, follow Java naming conventions. For example you called a JFrame as FinestradiRicerca instead rename it to: finestradiRicerca (1st letter of a variable in lowercase).

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainWindow {
    // Seconda Finestra
    public static void NuovaFinestra (JPanel panel) {
        panel.setLayout(null);
        JButton Ricerca = new JButton("Ricerca");
        Ricerca.setBounds(100, 100, 200, 50);
        panel.add(Ricerca);
        Ricerca.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
                FinestradiRicerca.setBounds(300, 300, 500, 500);
                //If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
                FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel riquadroRicerca = new JPanel();
                JTextField ciao;
                JLabel myLabel = new JLabel("Here goes your label text");
                ciao = new JTextField ();
                ciao.setColumns(20);
                riquadroRicerca.add(myLabel);
                riquadroRicerca.add(ciao);
                FinestradiRicerca.add(riquadroRicerca);
                FinestradiRicerca.setVisible(true);
            }
        });
    }

    //Main  
    public static void main(String[] args) {
        //Finestra Principale
        JFrame finestra = new JFrame("Finestra principale");
        finestra.setSize(500, 500);
        finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //JPanel della finestra principale
        JPanel riquadro = new JPanel();
        finestra.add(riquadro);
        finestra.setVisible(true);
        NuovaFinestra(riquadro);
    }
}

So, your code after a few modifications, to make JLabel and JTextField visible gives the following output:

enter image description here

However, please follow my above recomendations.

相关问答

更多
  • 查看Text Prompt以获得灵活的解决方案。 您可以控制何时显示提示(始终提高焦点或减少焦点)。 您还可以自定义文本的样式。 Check out Text Prompt for a flexible solution. You can control when prompt is displayed (always, focus gained or focus lost). You can also customize the style of the text.
  • 这是因为你没有使用任何我建议使用FlowLayout和GridBagLayout的布局。 It's because you are not using any layout I suggest to use FlowLayout and GridBagLayout.
  • 编辑, 只有在屏幕上显示其父级时, JWindow内容才可访问 对于可编辑和可访问的内容,使用un_decorated JDialog而不是JWindow ,jDialog不会导致不可访问的内容, 之所以......,我无法解释,没有找不到原因,在这一刻没有办法,API没有告诉我什么导致可访问,可编辑...... 。 。 。 1. Why JTextField is uneditable in JWindow and how could i let it able to edit? 真的不知道 impor ...
  • 就像我刚才提到的,这对于你的问题是一个简单的方法。 首先创建所需的所有JTextField,并切换其可见性,而不是在运行时删除和添加它。 import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class DynamicTextFieldsApp { public static void main(String[] args){ javax.swing.SwingUt ...
  • 在将Panel添加到FinestradiRicerca之前,您需要将新元素添加到riquadroRicerca ,我建议您不要使用null布局,而是使用布局管理器或它们的组合。 如果您坚持保留null布局,请参阅下面的示例。 但对于这种应用程序,我建议使用CardLayout 。 我还建议不要使用多个JFrames因为它们会在用户不友好的任务栏上打开多个窗口。 请参阅: 使用多个JFrame,好/坏练习 另外,请遵循Java命名约定 。 例如,您将JFrame称为FinestradiRicerca而是将其重 ...
  • 您可以使用观察者模式或生产者/消费者模式来解决问题。 基本的想法是,你有一些产生价值的东西,或者想要被通知或消耗所产生的价值的东西。 您应该花时间学习的其他原则之一也是Code to interface(而不是实现) 。 这听起来很奇怪,但其目的是减少对象的不必要暴露(对非预期/受控的修改)并解耦代码,这样您就可以更改底层实现,而不会影响依赖它的任何其他代码 鉴于您的问题的性质,观察者模式可能更合适。 Swing的大多数听众都是基于同样的原则。 我们首先定义“生成器”将用于提供变更通知的合同...... p ...
  • 默认情况下, JFrame永远不会对KeyEvent做出反应,这意味着frame.setFocusable(true); 必须为JPanel设置setFocusable(true) ,然后KeyListener KeyEvents将触发所需的事件 不要将KeyListener用于Swing JComponent ,而是使用(大多数可伸缩,可设置的) KeyBindings ( 例如 ) 对BorderLayout使用常量 ,只能看到一个JComponent ,放在第5个中。 区 JFrame by defa ...
  • 如果需要两个单独的字段,则需要有两个单独的JTextField对象:尝试添加 JTextField field2 = new JTextField("Your second field"); 并将您frame上的第一个add()调用更改为 frame.add(BorderLayout.NORTH, field2); 这将产生 我认为是你想要的。 You need to have two seperate JTextField objects if you want two seperate fields ...
  • 如果您“不想再次打开第一帧”,则必须避免先创建新实例并使用之前实例的引用。 所以你需要一个引用...给你的第二个引用的第一种方法是在构造函数中提供它,如: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ second sec = new second(this); //notice I'm passing a reference ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)