简体   繁体   English

JPanel setColor()重复无法正常工作,或者我做错了什么?

[英]JPanel setColor() repeatation is not working properly or am I doing something wrong?

So I have this method that takes a JFrame, and two JPanels as arguments, and makes a fadeout/fadein transition from one Jpanel to another. 因此,我有一个使用JFrame和两个JPanels作为参数,并从一个Jpanel到另一个Jpanel进行淡入淡出/淡入淡出过渡的方法。 All I do is add a new Jpanel on the first Jpanel on the parameter and keep updating its background color in a loop until it becomes black from transparent. 我要做的就是在参数的第一个Jpanel上添加一个新的Jpanel,并不断循环更新其背景颜色,直到它从透明变为黑色。 The first half works fine until it comes to second half where I try to run a while loop where it keeps looping until the background color of the tint panel on the 2nd Jpanel becomes transparent from black. 前一半效果很好,直到到达后一半,我尝试运行while循环,直到第二个Jpanel上的着色面板的背景颜色从黑色变为透明为止。 It stays black the whole time. 它始终保持黑色。

Any thoughts on this? 有什么想法吗? What am I doing wrong? 我究竟做错了什么?

public static void fadeShow(JFrame frame, JPanel panel1, JPanel panel2) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    JPanel tint = new JPanel();
                    tint.setOpaque(true);
                    tint.setBackground(new Color(0,0,0,0));
                    tint.setBounds(frame.getBounds().x+1, frame.getBounds().y, frame.getWidth(), frame.getHeight());
                    panel1.add(tint);
                    Thread.sleep(1500);
                    int i = 5;
                    while(i <= 255) {
                        tint.setBackground(new Color(0,0,0,i));
                        i+=25;
                        Thread.sleep(50);
                    }
                    panel1.setVisible(false);
                    tint.setVisible(false);
                    panel1.remove(tint);
                    tint = new JPanel();
                    tint.setOpaque(true);
                    tint.setBackground(new Color(0,0,0,255));
                    tint.setBounds(frame.getBounds().x+1, frame.getBounds().y, frame.getWidth(), frame.getHeight());
                    panel2.add(tint);
                    frame.add(panel2);
                    i = 250;
                    while(i > 0) {
                        tint.setBackground(new Color(0,0,0,i));
                        i-=25;
                        Thread.sleep(50);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }).start();




}

Your program flouts Swing threading rules by making significant Swing gui state changes from within a background thread. 您的程序通过在后台线程内进行重大的Swing GUI状态更改来甩掉Swing线程规则。 This risks freezing your GUI or causing strange visual artifacts. 这可能会冻结您的GUI或引起奇怪的视觉失真。 Instead use a Swing Timer , since this can allow you to make repeated calls with an interval delay, and guarantee that those calls will all be made on the Swing event thread. 而是使用Swing Timer ,因为这可以让您以一定的间隔延迟进行重复调用,并确保这些调用都将在Swing事件线程上进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM