我有三个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);
}
}
答案 0 :(得分:0)
您可以拨打setBounds()
,这样您就可以同时设置位置和大小。实际上,最后,setSize()
和setLocation()
都会调用setBounds()
。
此外,您可以避免在面板到位之前致电setVisible(true)
。
因此,如何将您的方法更改为:
if(!_leftPanelOpened) {
frame.setBounds(currCoords.x - panelWidth, currCoords.yframeWidth + panelWidth, frameHeight);
leftPanel.setVisible(true);
_leftPanelOpened = true;
}