Java 如何在左侧展开jframe?
我有三个Java 如何在左侧展开jframe?,java,swing,jpanel,Java,Swing,Jpanel,我有三个JPanels。 左面板和右面板是隐藏的。默认情况下,只有中间面板可见 当我按下一个按钮时,框架宽度将增加右面板的宽度,右面板将可见 我的问题是左面板,因为无法在左方向增加框架 我的解决办法是: public void mousePressed(MouseEvent e) { int frameWidth = frame.getBounds().width; int frameHeight = frame.getBounds().height; int panel
JPanel
s。
左面板和右面板是隐藏的。默认情况下,只有中间面板可见
当我按下一个按钮时,框架宽度将增加右面板的宽度,右面板将可见
我的问题是左面板,因为无法在左方向增加框架
我的解决办法是:
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;
}
}
它可以工作,但帧很快就会闪烁。我能避免这短暂的眨眼吗
编辑:
您可以调用
setBounds()
,它允许您同时设置位置和大小。实际上,在引擎盖下,最后都是setSize()
和setLocation()
调用setBounds()
此外,您可以避免在面板就位之前调用setVisible(true)
因此,将您的方法更改为:
if(!_leftPanelOpened) {
frame.setBounds(currCoords.x - panelWidth, currCoords.yframeWidth + panelWidth, frameHeight);
leftPanel.setVisible(true);
_leftPanelOpened = true;
}
您需要计算帧的新x位置。因此,随着帧变宽,您需要更改x位置,使其“看起来”像是在向左侧扩展。但我确实有闪烁的问题。框架无法清晰地调整大小,请尝试手动调整窗口大小,然后张贴一个帖子。一定要复制粘贴你的代码到一个新的项目,并确保它编译和运行之前,张贴在这里。不幸的是,仍然使闪光。可能还有其他问题吗?我在网上搜索,似乎没有人有这个问题。
if(!_leftPanelOpened) {
frame.setBounds(currCoords.x - panelWidth, currCoords.yframeWidth + panelWidth, frameHeight);
leftPanel.setVisible(true);
_leftPanelOpened = true;
}