简体   繁体   中英

How expand a jframe in left side?

I have three JPanel s. The left and right panels are hidden. Only center panel is visible by default.

When I press one button, the frame width will be increased by the right panel's width and the right panel becomes visible.

My problem is with the left panel because the frame cannot be increased in the left direction.

My solution is:

public void mousePressed(MouseEvent e) {
    int frameWidth = frame.getBounds().width;
    int frameHeight = frame.getBounds().height;
    int panelWidth = leftPanel.getPreferredSize().width;
    Point currCoords = frame.getLocationOnScreen();

    if(!_leftPanelOpened) {
        frame.setSize(new Dimension(frameWidth + panelWidth, frameHeight));
        leftPanel.setVisible(true);
        frame.setLocation(currCoords.x - panelWidth, currCoords.y);
        _leftPanelOpened = true;
    }
    else {
        leftPanel.setVisible(false);
        frame.setSize(new Dimension(frameWidth - panelWidth, frameHeight));
        frame.setLocation(currCoords.x + panelWidth, currCoords.y);
        _leftPanelOpened = false;
    }
}

It works, but the frame shortly blinks. Can I avoid this short blink?

Edit:

public class Main {

    private static JPanel leftoption = new JPanel();
    private static JPanel rightoption = new JPanel();
    private static JFrame frame = new JFrame();
    private static JPanel leftPanel =  new JPanel();
    private static JPanel rightPanel =  new JPanel();
    private static JPanel _centerPanel =  new JPanel();
    private static boolean _leftPanelOpened = false;
    private static boolean _rightPanelOpened = false;

    public static void main(String[] args) {
        leftoption.setPreferredSize(new Dimension(50, 40));
        leftoption.setBackground(Color.CYAN);
        leftoption.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int frameWidth = frame.getBounds().width;
                int frameHeight = frame.getBounds().height;
                int panelWidth = leftPanel.getPreferredSize().width;
                Point currCoords = frame.getLocationOnScreen();

                if(!_leftPanelOpened) {
                    frame.setBounds(currCoords.x - panelWidth, currCoords.y, frameWidth + panelWidth, frameHeight);
                    leftPanel.setVisible(true);
                    _leftPanelOpened = true;
                }
                else {
                    leftPanel.setVisible(false);
                    frame.setBounds(currCoords.x + panelWidth, currCoords.y, frameWidth - panelWidth, frameHeight);
                    _leftPanelOpened = false;
                }
            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }
        });

        rightoption.setPreferredSize(new Dimension(50, 40));
        rightoption.setBackground(Color.ORANGE);
        rightoption.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int frameWidth = frame.getBounds().width;
                int frameHeight = frame.getBounds().height;
                int panelWidth = rightPanel.getPreferredSize().width;
                if(!_rightPanelOpened) {
                    frame.setSize(new Dimension(frameWidth+panelWidth, frameHeight));
                    rightPanel.setVisible(true);
                    _rightPanelOpened = true;
                }
                else {
                    rightPanel.setVisible(false);
                    frame.setSize(new Dimension(frameWidth-panelWidth, frameHeight));
                    _rightPanelOpened = false;
                }
            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }
        });


        _centerPanel.setPreferredSize(new Dimension(300, 600));
        _centerPanel.setBackground(Color.WHITE);
        _centerPanel.add(leftoption);
        _centerPanel.add(rightoption);

        leftPanel.setPreferredSize(new Dimension(300, 600));
        leftPanel.setBackground(Color.BLUE);
        leftPanel.setVisible(false);

        rightPanel.setPreferredSize(new Dimension(300, 600));
        rightPanel.setBackground(Color.RED);
        rightPanel.setVisible(false);

        frame.add(leftPanel, BorderLayout.LINE_START);
        frame.add(_centerPanel, BorderLayout.CENTER);
        frame.add(rightPanel, BorderLayout.LINE_END);

        frame.setSize(new Dimension(300, 600));
        frame.getContentPane().setBackground(Color.WHITE);

        frame.setVisible(true);
    }

}

You can call setBounds() , which allows you to set position and size at the same time. Actually, under the hood, both setSize() and setLocation() call setBounds() in the end.

Furthermore, you can avoid calling setVisible(true) before the panel is in place.

Therefore, how about changing your method to:

if(!_leftPanelOpened) {
    frame.setBounds(currCoords.x - panelWidth, currCoords.yframeWidth + panelWidth, frameHeight);
    leftPanel.setVisible(true);
    _leftPanelOpened = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM