Java swing gridbaglayout panel resizing problem

I'm making a simple little swing application for a relative, but I'm having trouble animating it

This is the description of my layout:

I have a main window (created by instantiating, packaging and displaying JFrame) I told the content panel of the main window to use gridbaglayout as its layout My grid is 2 squares wide and 3 squares high In the first column, I have three jbuttons (a, B and C). Each JButton occupies a row of vertical grid space and a column of horizontal grid space In the second column, I have another JPanel, which is one column wide and three rows high

The second JPanel is also set to use gridbaglayout as its layout In this case, there are two columns and one row The left column has a wide, one row high JPanel with button 1 The right column contains only one JButton (button 2), which is also one column wide and one row high

This is the screenshot I just described:

Now that you know what the layout is, let me explain what I've been trying to do:

I've been trying to use the universal tween engine to adjust the JPanel containing button 1 However, in my attempt, I got this result:

You may have seen that the JPanel containing button 1 has been successfully resized! However, the size and position of button 2 have not changed at all. In fact, button 2 is cut off by the JPanel containing button 1!

This is a problem because I want to resize the JPanel containing button 1 to change the cell size of the gridbaglayout it contains at the same time It seems that I am very wrong in this matter

So this reminds me of my question... What do I need to do to "update" my gridbaglayout to adjust its grid size to accommodate the increase in the size of the JPanel containing button 1? I tried calling gridbaglayout on my gridbaglayout Invalidlayout(), I also tried to call JFrame on my main window getContentPane(). invalidate(). It doesn't seem to have any effect

This is the compiled source code for my project:

ImageManager. java:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import aurelienribon.tweenaccessors.swing.*;
import aurelienribon.utils.swing.*;
import aurelienribon.tweenengine.*;
import aurelienribon.tweenengine.equations.*;

public class ImageManager
{
    /**
     * @param args
     */

    public static JFrame mainwindow;
    public static TweenManager tweenManager;
    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokelater(new Runnable() //For thread safety!
        {
            public void run()
            {
                InitGUI();
                InitTween();
            }
        });
    }


    private static void InitTween()
    {
        ((Window)mainwindow).addWindowListener(new WindowAdapter() {
            @Override public void windowOpened(WindowEvent e)
            {
                    new DrawingCanvas()
                    {
                            @Override protected void update(int elapsedMillis)
                            {
                                tweenManager.update(elapsedMillis);
                            }
                    }.start();
            }
        });
    }


    private static void InitGUI()
    {
        //Init the window and layout systems
        mainwindow = new JFrame("Imaffect");
        Container pane = mainwindow.getContentPane();
        pane.setLayout(new GridBagLayout());

            //Begin creating the UI!
            pane.add(new JButton("Button A"),new GridBagConstraints(0,1,GridBagConstraints.PAGE_START,GridBagConstraints.VERTICAL,new Insets(0,0),100,0));
            pane.add(new JButton("Button B"),100));
            pane.add(new JButton("Button C"),2,20));

            pane.add(InitPreviewGUI(),new GridBagConstraints(1,3,GridBagConstraints.BOTH,0));


        //Finalize the window
        mainwindow.pack();
        mainwindow.setVisible(true);
    }


    private static JPanel InitPreviewGUI()
    {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(InitSettingsGUI(),0));
        panel.add(new JButton("Button 2"),0));


        return panel;
    }

    private static JPanel InitSettingsGUI()
    {
        JPanel panel = new JPanel();
            SetupSettingsTweens(panel);
            SetupSettingsContent(panel);
        return panel;
    }


    private static void SetupSettingsTweens(JPanel panel)
    {
        Tween.registerAccessor(Component.class,new ComponentAccessor());
        tweenManager = new TweenManager();
        Tween.to(panel,ComponentAccessor.WIDTH,1000)
            .target(200)
            .ease(Cubic.INOUT)
            .repeatYoyo(-1,200)
            .delay(500)
            .start(tweenManager);

    }

    private static void SetupSettingsContent(JPanel panel)
    {
        panel.add(new JButton("Button 1"));
    }
}

If you want to compile your own code, you need the following three resources:

> Universal Tween Engine library jar > DrawingCanvas. Java package (aurelieneribon. Utils. Swing) > componentaccessor Java package (aurelianribon. Tweeters. Swing)

If you want to experience my problem yourself without complex project settings, you can download the eclipse Juno project or runnable jar file:

> Imaffect Eclipse Juno Project > Imaffect Runnable Jar File

If you want to stay away from downloading, please list the actions in progress here:

>Set the GUI structure > set the requirements for repair rooms (nothing special) > set one to use swing utilities Invokeandwait calls the thread of the function named update() > whenever update() is called, it uses container Setsize() to animate the size change on the JPanel containing button 1

Thank you very much for any help on this issue! Thank you:)

PS: I'm sorry if this question is repeated I searched a lot, but I couldn't find anything related to my problem

Solution

I will say in your update () method that you should change the preferred size of button1 and call

container.revalidate();
container.repaint();

Changing the preferred size and revalidate () updating the layout and repaint () reflect the changes

Alternatively, you can call setsize() to increase the width of button1 and decrease the width of button2, and then call repeat() (revalidate() is not required because revalidation will restore the original layout)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>