更改卡片布局中的当前卡片,在摇摆中使用滑动效果 [英] Change current card in card layout with slide effect in swing

查看:113
本文介绍了更改卡片布局中的当前卡片,在摇摆中使用滑动效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用幻灯片效果更改卡片布局中的当前可见效果。但是我看到幻灯片开始时的一个轻弹,我无法调试/解决。如何避免轻弹?

I'm trying to change current visible in card layout with slide effect. But I see a flick at the start of slide which I'm not able to debug/solve. How can I avoid that flick?

以下是重现错误的示例代码:

Here is sample code to reproduce error:

import java.awt.CardLayout;

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

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new CardLayout());
        JLabel label1 = new JLabel("Harry Joy");
        panel.add(label1, "1");
        JLabel label2 = new JLabel("Harsh Raval");
        panel.add(label2, "2");
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(500L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            label2.setLocation(label1.getX() + 10 - label1.getWidth(), label1.getY());
            label1.setLocation(label1.getX() + 10, label1.getY());
            label2.setVisible(true);
        }
        label2.setVisible(false);
        CardLayout cl = (CardLayout)panel.getLayout(); 
        cl.next(panel);
    }
}

首先 label1 <显示/ code>(Harry Joy)。然后我可以看到 label2 (Harsh Raval),并尝试更改两者的位置以提供幻灯片效果。但是这里发生的事情是第一次两个标签都显示在彼此之上,然后它开始滑动。我怎么能阻止它,我的意思是将两个标签叠加在一起?如果你运行一次,你可以更好地理解我的意思。

Here at first label1("Harry Joy") is displayed. Then I make label2("Harsh Raval") visible and try to change location of both to provide a slide effect. But what happening here is first time both labels are displayed on top of each other and then it starts to slide. How can I stop that, I mean displaying both labels on top of each other? You can get better idea of what I mean if you run it once.

推荐答案

主要的问题是你正在踩着CardLayout的脚趾。 CardLayout都管理组件的边界(位置和大小)和可见性,所以你的循环在这里:

The main problem is that you are stepping on CardLayout's toes. CardLayout both manages bounds (location and size) and visibility of your components, so your loop here:

    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        label2.setLocation(label1.getX() + 10 - label1.getWidth(), label1.getY());
        label1.setLocation(label1.getX() + 10, label1.getY());
        label2.setVisible(true);
    }

与CardLayout的作用相冲突。默认情况下,CardLayout将自动显示您添加的第一个组件并隐藏所有其他组件。它还将所有组件的边界设置为相同的边界。

is conflicting with what CardLayout does. By default, CardLayout will automatically show the first component you added and hide all the others. It also sets up the bounds of all components to the same bounds.

在第一次迭代结束时,label2的可见性发生变化(来自 false true )最终会触发您的CardLayout重新执行组件的布局,这会将所有组件的边界设置为相同的边界,这是为什么你看到重叠。 CardLayout并不意味着同时可以看到多个组件。

At the end of the first iteration, the visibility of label2 changes (from false to true) which eventually triggers your CardLayout to reperform the layout of your components which sets the bounds of all the components to the same bounds which is why you are seeing the overlapping. CardLayout is not meant to have multiple components visible simultaneously.

请注意,你是在EDT(事件调度线程)上运行所有这些,这真是一个坏主意。它可能导致死锁和不可预测的行为(例如你在这里看到的行为)。

Note that you are running all this off the EDT (Event Dispatching Thread) which is really a bad idea. It can cause deadlocks and unpredictable behaviour (such as the one you are seeing here).

这篇关于更改卡片布局中的当前卡片,在摇摆中使用滑动效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆