[英]How expand a jframe in left side?
我有三个JPanel
。 左右面板被隐藏。 默认情况下,只有中间面板可见。
当我按下一个按钮时,边框宽度将增加右侧面板的宽度,并且右侧面板变为可见。
我的问题是左侧面板,因为无法向左增加框架。
我的解决方案是:
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;
}
}
它可以工作,但是画面不久会闪烁。 我可以避免这种短暂的眨眼吗?
编辑:
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);
}
}
您可以调用setBounds()
,它允许您同时设置位置和大小。 实际上,在后台, setSize()
和setLocation()
最终都会调用setBounds()
。
此外,您可以避免在面板安装到位之前调用setVisible(true)
。
因此,如何将您的方法更改为:
if(!_leftPanelOpened) {
frame.setBounds(currCoords.x - panelWidth, currCoords.yframeWidth + panelWidth, frameHeight);
leftPanel.setVisible(true);
_leftPanelOpened = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.