java swing 为调整 Jframe 高度制作平滑过渡动画

问题描述 投票:0回答:1

我正在开发一个小型 GUI 应用程序,并且该应用程序的一部分我打算在按下按钮后调整 JFrame 的大小。我设法调整 JFrame 的大小,但我想为该 JFrame 的打开和关闭创建平滑的动画。我不知道从哪里开始以及如何开始。我知道我可以想象按下按钮将运行线程/方法并调整 JFrame 的大小。我会尝试通过一些屏幕截图提供尽可能多的细节。

Here is a before - 在 JFrame 调整大小之前

and here is a after - JFrame 调整大小后

JFrame 当前调整大小的代码

changeBottomFrame.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(!heightIncreased[0]){
            try{
                frame.setSize(frame.getWidth(), frame.getHeight()+80);
                if(frame.getHeight()==450){
                    frame.add(bottomFrame, BorderLayout.PAGE_END);
                    changeBottomFrame.setText("∨");
                    heightIncreased[0] =true;
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }

        } else if (heightIncreased[0]) {
            try {
                frame.setSize(frame.getWidth(), frame.getHeight() - 80);
                if(frame.getHeight()==370) {
                    frame.remove(bottomFrame);
                    changeBottomFrame.setText("∧");
                    heightIncreased[0] = false;
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        {}
    }
});

在屏幕截图中,我添加了一个 JPanel 来突出显示添加的区域量,并在代码中显示,您可以看到

frame.add(bottomFrame)
进行澄清。

我是 Java 编码新手,我已经在 Python GUI 中实现了平滑的动画过渡,但我对 Java Swing 没有经验。

java swing user-interface jframe jpanel
1个回答
0
投票

下面是一个可运行的演示,演示了如何完成此操作。用于对 JFrame 窗口的打开、扩展和关闭进行动画处理的方法位于名为 animateForm()

animateFormClose()
AnimateForm
类中。
animateForm()
方法使用 Swing 计时器,
animateFormClose()
方法利用
while
循环以动画方式关闭表单。

请记住,这只是一个简单的演示。


SwingFormAnimation演示类:

public class SwingFormAnimationDemo {

    private javax.swing.JFrame frame;
    private javax.swing.JPanel rootPanel;
    private javax.swing.JButton resizeButton;

    private int desiredFormWidth = 500;
    private int desiredFormHeight = 300;
    private int widthIncrement = 10;
    private int heightIncrement = 10;
    private int speed = 5;
    private int initialDelay = 300;

    private int desiredFormExtentionWidthAmount = 0;
    private int desiredFormExtentionHeightAmount = 100;
    private int extensionWidthIncrement = 3;
    private int extensionHeightIncrement = 3;
    private int extentionSpeed = 10;
    private int extensionInitialDelay = 300;

    private AnimateForm animate;

    public SwingFormAnimationDemo() {
        initForm();
        animate = new AnimateForm();
        animate.animateForm(frame, desiredFormWidth, desiredFormHeight,
                widthIncrement, heightIncrement, speed, initialDelay);

        frame.addWindowListener(new java.awt.event.WindowListener() {
            @Override
            public void windowOpened(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                animate.animateFormClose(frame);
                if (AnimateForm.animateFormTimer.isRunning()) {
                    AnimateForm.animateFormTimer.stop();
                }
                frame.dispose();
            }

            @Override
            public void windowClosed(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }

            @Override
            public void windowIconified(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }

            @Override
            public void windowDeiconified(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }

            @Override
            public void windowActivated(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }

            @Override
            public void windowDeactivated(java.awt.event.WindowEvent e) {
                // Do Nothing...
            }
        });
        
        resizeButton.addActionListener((java.awt.event.ActionEvent e) -> {
            if (resizeButton.getText().equals("Longer Form")) {
                animate.animateForm(frame, frame.getWidth() + desiredFormExtentionWidthAmount,
                        frame.getHeight() + desiredFormExtentionHeightAmount,
                        extensionWidthIncrement, extensionHeightIncrement,
                        extentionSpeed, extensionInitialDelay);
                resizeButton.setText("Shorter Form");
            }
            else {
                animate.animateForm(frame, frame.getWidth() - desiredFormExtentionWidthAmount,
                        frame.getHeight() - desiredFormExtentionHeightAmount,
                        extensionWidthIncrement, extensionHeightIncrement,
                        extentionSpeed, extensionInitialDelay);
                resizeButton.setText("Longer Form");
            }
        });
    }

    public static void main(String[] args) {
        new SwingFormAnimationDemo().startApp(args);
    }

    private void startApp(String[] args) {
        java.awt.EventQueue.invokeLater(() -> {
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
        });

    }

    private void initForm() {
        frame = new javax.swing.JFrame();
        frame.setTitle("Swing Form Animation Demo");
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        frame.setPreferredSize(new java.awt.Dimension(0, 0));
        
        rootPanel = new javax.swing.JPanel();

        resizeButton = new javax.swing.JButton("Longer Form");
        rootPanel.add(resizeButton);

        javax.swing.JTable table = new javax.swing.JTable(17, 3);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        table.setGridColor(java.awt.Color.LIGHT_GRAY);
        javax.swing.JScrollPane sp = new javax.swing.JScrollPane(table);
        sp.setPreferredSize(new java.awt.Dimension(400, 300));
        rootPanel.add(sp);

        frame.setContentPane(rootPanel);
        frame.pack();
    }
}

AnimateForm 类:

public class AnimateForm {

    public static javax.swing.Timer animateFormTimer;

    public void animateForm(javax.swing.JFrame suppliedFrame, int width, int height,
            int widthIncrement, int heightIncrement, int speed, int initialDelay) {
        animateFormTimer = new javax.swing.Timer(speed, (java.awt.event.ActionEvent evt) -> {
            boolean widthRetraction = width < suppliedFrame.getWidth();
            boolean heightRetraction = height < suppliedFrame.getHeight();

            // Form animation...
            if (width > suppliedFrame.getWidth()) {
                suppliedFrame.setSize(suppliedFrame.getWidth() + widthIncrement,
                        suppliedFrame.getHeight());
            }
            else if (width < suppliedFrame.getWidth()) {
                suppliedFrame.setSize(suppliedFrame.getWidth() - widthIncrement,
                        suppliedFrame.getHeight());
            }
            else {
                suppliedFrame.setSize(width, suppliedFrame.getHeight());
            }

            if (height > suppliedFrame.getHeight()) {
                suppliedFrame.setSize(suppliedFrame.getWidth(),
                        suppliedFrame.getHeight() + heightIncrement);
            }
            else if (height < suppliedFrame.getHeight()) {
                suppliedFrame.setSize(suppliedFrame.getWidth(),
                        suppliedFrame.getHeight() - heightIncrement);
            }
            else {
                suppliedFrame.setSize(suppliedFrame.getWidth(), height);
            }

            if ((widthRetraction ? suppliedFrame.getWidth() <= width : suppliedFrame.getWidth() >= width)
                    && (heightRetraction ? suppliedFrame.getHeight() <= height : suppliedFrame.getHeight() >= height)) {
                animateFormTimer.stop();
            }
        });
        animateFormTimer.setInitialDelay(initialDelay);
        animateFormTimer.start();
    }

    public void animateFormClose(javax.swing.JFrame frame) {
        while (frame.getWidth() > 140 || frame.getHeight() > 40) {
            frame.setSize(frame.getWidth() - 1, frame.getHeight() - 1);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.