gpt4 book ai didi

java - Java中JToggle选择状态下的滑动抽屉动画?

转载 作者:行者123 更新时间:2023-12-02 01:56:32 27 4
gpt4 key购买 nike

我正在尝试制作一个程序,在选择 JToggleButton 时显示一个滑动抽屉动画面板,并在取消选择时隐藏它。

我想要这样的点击切换按钮,

|切换按钮|---->|面板|
像抽屉一样滑动面板

以及取消选择切换按钮

|切换按钮|<-----|隐藏面板|

我能够在选择 JToggle 按钮上创建一个新面板,但我对创建动画感到困惑。

myToggleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if(ev.getStateChange()==ItemEvent.SELECTED){

myPanel.add(slidingPanel,BorderLayout.CENTER);
myPanel.revalidate();
myPanel.repaint();

} else if(ev.getStateChange()==ItemEvent.DESELECTED){

myPanel.remove(slidingPanel);
myPanel.revalidate();
myPanel.repaint();
}
}
});

如何在单击 ToggleButton 时实现面板的滑动抽屉动画,然后隐藏面板。

最佳答案

我能够弄清楚如何实现它。我正在编写代码以寻求帮助。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlidePanelDemo extends JPanel {

private JPanel pnlMain;
private JFrame frame;
private JPanel contentPane;
private static int drawWidth = 200;
private static int drawHeight = 100;
private Timer timer;
private int graphWidth = 5;

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

public void createAndShowGUI() {
JToggleButton button = new JToggleButton("Tools");
button.setSize(30, 30);
button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if (ev.getStateChange() == ItemEvent.SELECTED) {
System.out.println("button is selected");
timer = new Timer(40, (ActionEvent e1) -> {
graphWidth = graphWidth + 5;
System.out.println("graphWidth is" + graphWidth);
pnlMain.setSize(graphWidth, drawHeight);
pnlMain.setVisible(true);
if (graphWidth >= 0 && graphWidth == drawWidth) {
timer.stop();
}
});
timer.setRepeats(true);
timer.start();
} else if (ev.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("button is not selected");
timer = new Timer(40, (ActionEvent e1) -> {
graphWidth = graphWidth - 5;
System.out.println("graphWidth is" + graphWidth);
if (graphWidth > 0 && graphWidth <= drawWidth) {
pnlMain.setSize(graphWidth, drawHeight);
pnlMain.setVisible(true);
}
if (graphWidth == 5) {
timer.stop();
}
});
timer.setRepeats(true);
timer.start();
}
}
});
pnlMain = createMainPanel();
JButton label = new JButton("Click");
JButton label2 = new JButton("Click me");
pnlMain.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
pnlMain.add(label);
pnlMain.add(label2);
contentPane = new JPanel();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
contentPane.setOpaque(true);
contentPane.add(button);
contentPane.add(pnlMain);

pnlMain.setVisible(true);

JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Slide Out Panel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(500, 300);
}

private JPanel createMainPanel() {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(graphWidth, drawHeight);
}
};
panel.setBorder(BorderFactory.createTitledBorder("Main"));
return panel;
}
}

关于java - Java中JToggle选择状态下的滑动抽屉动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395346/

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