我正在尝试使用幻灯片效果更改卡片布局中的当前可见效果。但是我在幻灯片的开头看到一个轻弹,我无法调试/解决。我怎么能避免那个电影?
以下是重现错误的示例代码:
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
(“Harry Joy”)。然后我让label2
(“Harsh Raval”)可见,并尝试更改两者的位置以提供幻灯片效果。但是这里发生的事情是第一次两个标签都显示在彼此之上,然后它开始滑动。我怎么能阻止它,我的意思是将两个标签叠加在一起?如果你运行一次,你可以更好地理解我的意思。
答案 0 :(得分:3)
主要问题是你正在踩着CardLayout的脚趾。 CardLayout都管理组件的边界(位置和大小)和可见性,所以你的循环在这里:
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将自动显示您添加的第一个组件并隐藏所有其他组件。它还将所有组件的边界设置为相同的边界。
在第一次迭代结束时,label2的可见性发生变化(从false
变为true
),最终触发您的CardLayout重新执行组件布局,从而设置所有组件的边界达到相同的界限,这就是你看到重叠的原因。 CardLayout并不意味着同时可以看到多个组件。
请注意,您正在运行EDT(事件调度线程)中的所有这些,这真是一个坏主意。它可能会导致死锁和不可预测的行为(例如您在此处看到的行为)。
答案 1 :(得分:2)
答案 2 :(得分:2)
一些评论,可能只是评论
Thread.sleep
的想法是正确的,但仅从Runnable.Thread
开始
对于Swing GUI,有Swing Timer
,
Swing Timer
保证移动标准JComponents
LayoutManager
但在这个(我的代码示例)情况下,我再次使用Swing Timer,并且所有事件都将在EDT上完成,通过从Runnable生活自己的生活调用而不影响其余的Swing事件,GUI ei,
注意此问题基于AWT嵌套/继承,例如对于JComboBoxes弹出窗口(resize和doLayout())不起作用
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShakingButtonDemo implements Runnable {
private JButton button;
private JRadioButton radioWholeButton;
private JRadioButton radioTextOnly;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new ShakingButtonDemo());
}
@Override
public void run() {
radioWholeButton = new JRadioButton("The whole button");
radioTextOnly = new JRadioButton("Button text only");
radioWholeButton.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(radioWholeButton);
bg.add(radioTextOnly);
button = new JButton(" Shake with this Button ");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shakeButton(radioWholeButton.isSelected());
}
});
JPanel p1 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("Shake Options"));
p1.setLayout(new GridLayout(0, 1));
p1.add(radioWholeButton);
p1.add(radioTextOnly);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(0, 1));
p2.add(button);
JFrame frame = new JFrame();
frame.setTitle("Shaking Button Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setSize(240, 160);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void shakeButton(final boolean shakeWholeButton) {
final Point point = button.getLocation();
final Insets margin = button.getMargin();
final int delay = 75;
Runnable r = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
if (shakeWholeButton) {
moveButton(new Point(point.x + 5, point.y));
Thread.sleep(delay);
moveButton(point);
Thread.sleep(delay);
moveButton(new Point(point.x - 5, point.y));
Thread.sleep(delay);
moveButton(point);
Thread.sleep(delay);
} else {// text only
setButtonMargin(new Insets(margin.top, margin.left + 3, margin.bottom, margin.right - 2));
Thread.sleep(delay);
setButtonMargin(margin);
Thread.sleep(delay);
setButtonMargin(new Insets(margin.top, margin.left - 2, margin.bottom, margin.right + 3));
Thread.sleep(delay);
setButtonMargin(margin);
Thread.sleep(delay);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
};
Thread t = new Thread(r);
t.start();
}
private void moveButton(final Point p) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setLocation(p);
}
});
}
private void setButtonMargin(final Insets margin) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setMargin(margin);
}
});
}
}