2011-10-02 14 views
11

Tôi đang cố gắng đặt vị trí dải phân cách của JSplitPane nhưng có vẻ như không hoạt động.Đặt vị trí dải phân cách trên JSplitPane không hoạt động

Dưới đây là một SSCCE:

import java.awt.Color; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JSplitPane; 


public class JSplitProblem extends JFrame { 

    public JSplitProblem(){ 
     JPanel upperPanel = new JPanel(); 
     upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS)); 

     JPanel leftPanel = new JPanel(); 

     leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS)); 
     JPanel red = new JPanel(); 
     red.setBackground(Color.red); 
     leftPanel.add(red); 

     JPanel rightPanel = new JPanel(); 
     rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); 
     JPanel blue = new JPanel(); 
     blue.setBackground(Color.blue); 
     rightPanel.add(blue); 

     upperPanel.add(leftPanel); 
     upperPanel.add(rightPanel); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setBackground(Color.black); 

     JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel); 
     mainSplittedPane.setOneTouchExpandable(true); 
     mainSplittedPane.setDividerLocation(0.5); 

     this.add(mainSplittedPane); 
     this.setSize(800,600); 
     this.setResizable(true); 
     this.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new JSplitProblem(); 
    } 

} 

Tôi muốn bảng điều khiển phía dưới màu đen đặt trên 50% của toàn bộ khu vực theo mặc định. Tôi đang làm gì sai?

+0

đơn giản cộng một cho sscce – mKorbel

+1

IIRC setDividerLocation được thực hiện trong hệ thống phân cấp ngăn chặn. Đó có thể là do phương pháp tính ngay lập tức vị trí thực tế bằng pixel, dựa trên kích thước hiện tại. – jfpoilpret

+0

@jfpoilpret: vâng. Tôi hiểu điều đó từ câu trả lời của mKorbel. Nếu không bằng cách sử dụng gọi sau này sẽ không ảnh hưởng đến nó. Cảm ơn bạn đã chỉ ra. – Heisenbug

Trả lời

7

complicated in this case, với các quy tắc

1) PrefferedSize phải returns Childs không như tôi sai lầm khi đặt trong trường hợp của tôi quá :-), sau đó câu trả lời của tôi là không @kleopatra chống lại quá

2) đưa mọi thứ về rezize, kích thước, bất cứ điều gì cho JSplitPane vào 01. .

enter image description here .

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

public class JSplitProblem extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JSplitPane mainSplittedPane; 

    public JSplitProblem() { 
     JPanel upperPanel = new JPanel(); 
     upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS)); 
     JPanel leftPanel = new JPanel(); 
     leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS)); 
     JPanel red = new JPanel(); 
     red.setBackground(Color.red); 
     leftPanel.add(red); 
     JPanel rightPanel = new JPanel(); 
     rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); 
     JPanel blue = new JPanel(); 
     blue.setBackground(Color.blue); 
     rightPanel.add(blue); 
     upperPanel.add(leftPanel); 
     upperPanel.add(rightPanel); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setBackground(Color.black); 

     mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel, bottomPanel); 
     mainSplittedPane.setOneTouchExpandable(true); 
     mainSplittedPane.setDividerLocation(0.5); 

     add(mainSplittedPane); 
     setPreferredSize(new Dimension(400, 300)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setResizable(true); 
     setVisible(true); 
     pack(); 
     restoreDefaults(); 
    } 

    private void restoreDefaults() { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().height /2); 
       //mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().width /2); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     JSplitProblem jSplitProblem = new JSplitProblem(); 
    } 
} 
+0

oh..thanks cho câu trả lời nhưng tôi vẫn có một số vấn đề (và tôi đã được một chút không chính xác trong khi đặt câu hỏi). Vấn đề là giải pháp của bạn chỉ hoạt động nếu tôi không thay đổi kích cỡ khung hình. Nếu tôi thay đổi kích thước khung hình theo cách thủ công thì không phải là mantained. Có cách nào để giữ vị trí diveder luôn ở một giá trị nhất định không? +1 anyway – Heisenbug

+0

có rất đơn giản, bạn phải thêm WindowListener, ghi đè lên ResizeEvent thích hợp (s), nhưng thông báo gọi 'restoreDefaults()' để trì hoãn rằng với 'java.swing.Timer', bởi vì bạn chưa từng biết Thay đổi kích thước kết thúc, vì nếu kích thước lại tiếp tục, thì chỉ cần khởi động lại 'Timer' :-) – mKorbel

+0

+1 khóa là setDividerLocation (double) chỉ hoạt động trên GUI có thể nhìn thấy vì nó không thể tính tỷ lệ kích thước cho đến khi chia cửa sổ thực sự có kích thước. Trong ví dụ đơn giản, không cần sử dụng invokeLater. Chỉ cần gọi phương thức setDividerLocation (...) sau gói(), setVisible (true). Nếu bạn muốn sử dụng phương thức restoreDefaults() thì bạn nên gọi nó khi bạn tạo ra splitpane. – camickr

3

Tôi không chắc chắn, nhưng tôi nghĩ bạn nên cố gắng pack() khung hình của mình. Và nếu điều đó không hiệu quả, hãy thử đặt lại vị trí dải phân cách sau khi bạn đã đóng khung.

12

Nếu bạn muốn cả hai nửa của cửa sổ chia để chia sẻ trong không gian thêm hoặc loại bỏ các cửa sổ chia của, thiết lập trọng lượng thay đổi kích thước đến 0,5: (Tutorial)

JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel); 
mainSplittedPane.setOneTouchExpandable(true); 
mainSplittedPane.setResizeWeight(0.5);        
+1

+1: cảm ơn setResizeWeight. Tôi sẽ chơi với nó một chút và xem những gì tôi có thể làm. – Heisenbug

1

Chỉ cần thêm mã bên dưới và điều đó sẽ đủ công bằng.

mainSplittedPane.setResizeWeight(0.5); 
Các vấn đề liên quan