2016-02-08 77 views
0

我有三個JPanel s。 左側和右側面板隱藏。只有中心面板默認可見。如何在左側擴展jframe?

當我按下一個按鈕時,框架寬度將增加右側面板的寬度,右側面板變爲可見。

我的問題是左面板,因爲框架不能在左方向增加。

我的解決辦法是:

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

你需要計算框架的新x位置。所以隨着框架變寬,你需要改變x的位置,使其「看起來」像確實擴展到左邊的 – MadProgrammer

+0

。但我確實有閃爍的問題。 –

+0

框架不會乾淨地調整大小,請嘗試手動調整窗口大小 – MadProgrammer

回答

0

你可以叫setBounds(),它允許你設置的位置和大小在同一時間。其實在引擎蓋下,setSize()setLocation()最後都會調用setBounds()

此外,您可以避免在面板到位之前調用setVisible(true)

因此,如何改變你的方法:

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

不幸的是,仍然使閃光。它可能會有另一個問題?我搜索了網絡,似乎沒有人有這個問題。 –