简体   繁体   English

如何在左侧扩展jframe?

[英]How expand a jframe in left side?

I have three JPanel s. 我有三个JPanel 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. 您可以调用setBounds() ,它允许您同时设置位置和大小。 Actually, under the hood, both setSize() and setLocation() call setBounds() in the end. 实际上,在后台, setSize()setLocation()最终都会调用setBounds()

Furthermore, you can avoid calling setVisible(true) before the panel is in place. 此外,您可以避免在面板安装到位之前调用setVisible(true)

Therefore, how about changing your method to: 因此,如何将您的方法更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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