2012-09-04 70 views
3

我想將我的屏幕拆分爲兩部分,所以我使用了BorderLayout和East和West部分。我在調整大小時遇到​​了問題,最終我發現(請參閱:http://www.leepoint.net/notes-java/GUI/layouts/20borderlayout.html)東西方面板中的寬度未更改,而北面和南面面板中的高度沒有發生變化,兩者在中心面板中都發生了變化。Java Swing BorderLayout調整難度

但是,我希望在調整大小時更改寬度和高度,並且並排放置兩個面板。我嘗試過不同級別的嵌套來嘗試讓它工作,但我認爲它不會與BorderLayout一起使用。

對於默認的佈局管理器來說,這應該很容易,但也許我應該嘗試不同的佈局(例如BoxLayout)來達到我想要的效果?

而且,這裏是一些代碼,複製我談論這個問題(嘗試調整窗口大小):

import java.awt.BorderLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Main extends JFrame { 

    public static void main(String[] args) { 
     JFrame window = new Main(); 
     window.setVisible(true); 
    } 

    public Main() { 
     JButton east = new JButton("East"); 
     JButton west = new JButton("West"); 

     JPanel content = new JPanel(); 
     content.setLayout(new BorderLayout()); 

     content.add(east, BorderLayout.EAST); 
     content.add(west, BorderLayout.WEST); 

     setContentPane(content); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
    } 

} 

編輯:我不希望雙方是平等的,大約爲2:1的比例是我想要的。

+1

如果你只想分成兩部分。爲什麼你不使用GridLayout? – Sednus

回答

6

你可以在你的情況下使用的是GridLayout,這裏兩個JButtons將調整自己的大小,因爲JFrame調整大小。

import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Main extends JFrame { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       JFrame window = new Main(); 
       window.setVisible(true); 
      } 
     });   
    } 

    public Main() { 
     JButton east = new JButton("East"); 
     JButton west = new JButton("West"); 

     JPanel content = new JPanel(); 
     content.setLayout(new GridLayout(1, 2)); 

     content.add(east); 
     content.add(west); 

     setContentPane(content); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
    } 

} 

此外,總是最好從EDT - Event Dispatch Thread運行GUI相關代碼,而不是從主線程運行。請閱讀Concurrency in Swing,瞭解更多關於該主題的信息。

最新編輯:按照要求評論

使用的GridBagLayout指定你想給

import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Main extends JFrame { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       JFrame window = new Main(); 
       window.setVisible(true); 
      } 
     });   
    } 

    public Main() { 
     JPanel east = new JPanel(); 
     east.setOpaque(true); 
     east.setBackground(Color.WHITE); 
     JPanel west = new JPanel(); 
     west.setOpaque(true); 
     west.setBackground(Color.BLUE); 

     JPanel content = new JPanel(); 
     content.setLayout(new GridBagLayout()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 0.3; 
     gbc.weighty = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     content.add(east, gbc); 
     gbc.weightx = 0.7; 
     gbc.gridx = 1; 
     content.add(west, gbc); 

     setContentPane(content); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
    } 

} 
+0

感謝您的建議。然而,我之前沒有提到的是,我不希望雙方平等。 GridLayout可以做到嗎? – Paul

+0

對於這個問題,你需要'GridBagLayout',它可以讓你自由地在約束的幫助下定義'JComponents'的大小。給我一段時間,一個例子就是這樣:-) –

+0

現在請注意這個編輯,我希望這是你的意思是不等的大小。 –

7

大小你爲什麼不嘗試用JSplitPane

import javax.swing.*; 
import java.awt.*; 

public class AppDemo { 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(() -> { 
      JFrame frame = new JFrame(); 
      JButton eastButton = new JButton("East"); 
      JButton westButton = new JButton("West"); 
      JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, eastButton, westButton); 

      JPanel content = new JPanel(); 
      content.setLayout(new BorderLayout()); 
      content.add(splitPane, BorderLayout.CENTER); 

      frame.setContentPane(content); 
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      frame.setPreferredSize(new Dimension(500, 400)); 
      frame.pack(); 
      frame.setVisible(true); 
     }); 

    } 
} 

你會得到這個:

enter image description here

+0

資料性輸入+1 :-) –

+0

謝謝先生! :) –

+0

你最歡迎和保持微笑:-) –

0

如果你想保持你的BorderLayout您可以使用類似以下對象:

public class ResizablePanel extends JPanel { 

    public ResizablePanel(JComponent body) { 

    setLayout(new BorderLayout()); 
    JButton resize = new JButton(); 
    resize.setPreferredSize(new Dimension(Integer.MAX_VALUE, 4)); 
    resize.addMouseMotionListener(new MouseAdapter() { 
     public void mouseDragged(MouseEvent e) { 
      Dimension preferredSize = ResizablePanel.this.getPreferredSize(); 
      ResizablePanel.this.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height-e.getY())); 
      ResizablePanel.this.revalidate(); 
     } 
    });    
    add(resize, BorderLayout.PAGE_START); 
    add(body, BorderLayout.CENTER); 
    } 
} 

現在換你想要的ResizablePanel一個實例來調整的一部分,你就可以調整它通過拖動瘦按鈕。

注意,這是代碼是調整的高度,你把底部(PAGE_END)邊界佈局的一部分面板的,但它應該是相當簡單的改變,以調整大小的寬度。