gpt4 book ai didi

java - JPanel 的滑动效果

转载 作者:搜寻专家 更新时间:2023-11-01 01:17:58 25 4
gpt4 key购买 nike

我试着创建一个简单的类,它可以像这样滑动一个 JPanel:

+----------+    +------+---+    +----------+|          |    |      |   |    |          || JPanel1  | => | JPane| JP| => | JPanel2  ||          |    |      |   |    |          |+----------+    +------+---+    +----------+

I created javax.swing.Timer and added in class

timer = new Timer(50, this);
timer.start();

static final int frames = 5;
int counter = 0;

actionPerformed(ActionEvent e) {
if (counter >= frames) {
timer.stop();
counter = 0;
} else {
counter++;
jPanel2.setBounds(800 - 800 * counter / frames, 0, 800, 600);
}
}

这是工作,但非常缓慢。我只有 2-3 fps,不知道如何加快这种方法。你能帮帮我吗?

最佳答案

也许将计时器的间隔从 50 更改为更小的数字。

即从这个:

timer = new Timer(50, this);

为此:

timer = new Timer(10, this);

注意:您应该避免在此处使用魔数(Magic Number)。

哎呀,有点像......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SlideEg extends JPanel {
private SlideContainer slideContainer = new SlideContainer();

public SlideEg() {
setLayout(new BorderLayout());
add(slideContainer, BorderLayout.CENTER);

JLabel helloLabel = new JLabel("Hello", SwingConstants.CENTER);
slideContainer.add(helloLabel);

Timer myTimer = new Timer(5000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JLabel goodbyeLabel = new JLabel("Goodbye", SwingConstants.CENTER);
goodbyeLabel.setOpaque(true);
goodbyeLabel.setBackground(Color.pink);
slideContainer.add(goodbyeLabel);
}
});
myTimer.setRepeats(false);
myTimer.start();
}

private static void createAndShowGui() {
SlideEg mainPanel = new SlideEg();

JFrame frame = new JFrame("SlideEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class SlideContainer extends JLayeredPane {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int SLIDE_DELAY = 20;
protected static final int DELTA_X = 2;
Component oldComponent;

public SlideContainer() {
setLayout(null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

@Override
public Component add(Component comp) {
Component[] comps = getComponents();
if (comps.length > 0) {
oldComponent = comps[0];
}
if (oldComponent == comp) {
return super.add(comp);
}
if (oldComponent != null) {
putLayer((JComponent) oldComponent, JLayeredPane.DEFAULT_LAYER);
}
Component returnResult = super.add(comp);
putLayer((JComponent) comp, JLayeredPane.DRAG_LAYER);
comp.setSize(getPreferredSize());
comp.setVisible(true);
comp.setLocation(getPreferredSize().width, 0);
slideFromRight(comp, oldComponent);
return returnResult;
}

private void slideFromRight(final Component comp,
final Component oldComponent2) {
new Timer(SLIDE_DELAY, new ActionListener() {

@Override
public void actionPerformed(ActionEvent aEvt) {
int x = comp.getX();
if (x <= 0) {
comp.setLocation(0, 0);
putLayer((JComponent) comp, JLayeredPane.DEFAULT_LAYER);
if (oldComponent2 != null) {
remove(oldComponent2);
}
((Timer) aEvt.getSource()).stop();
} else {
x -= DELTA_X;
comp.setLocation(x, 0);
}
repaint();
}
}).start();
}
}

关于java - JPanel 的滑动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11283554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com