2009-07-13 29 views
10

Tôi có JFrame với BorderLayout làm trình quản lý bố cục.Tạo JPanel theo cách thủ công có thể định lại kích thước

Ở biên giới phía nam, tôi có một số JPanel, tôi muốn kích thước này có thể điều chỉnh bởi người dùng, tức là người dùng có thể nhấp vào mép biên giới và kéo nó lên để phóng to.

Có cách nào bạn biết rằng tôi có thể thực hiện việc này không?

Trả lời

19

Để làm cho các bảng trong khung có thể thay đổi kích thước, bạn cần phải thêm chúng vào một JSplitPane.

Thay vì đặt nó ở phần phía Nam của Khung hình, hãy đặt JSplitPane trong Trung tâm. Ngăn chia nhỏ sẽ làm cho bảng điều khiển phía dưới trong phần tách có vẻ như ở phía Nam và bảng điều khiển trên cùng trong phần chia tách sẽ nằm ở giữa khung hình.

Đảm bảo bạn đặt hướng của hai bảng với setOrientation(JSplitPane.VERTICAL_SPLIT).

Sau đó, bạn có thể thay đổi kích thước các bảng nằm trong ngăn.

11

Tôi nghĩ bạn muốn nói JPanel. Bạn có thể thêm một mouseListener tùy chỉnh và xử lý các lần nhấp chuột, kéo và thả chuột và sau đó thay đổi kích thước bảng chương trình.

Điều này sẽ chứng minh điều này. Lưu ý rằng jframe KHÔNG thay đổi kích thước tự động với JPanel. Để thực hiện hiệu quả rõ ràng hơn tôi vẽ bảng điều khiển màu đỏ và thêm một biên giới vát:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.BevelBorder; 

@SuppressWarnings("serial") 
public class ResizablePanel extends JPanel { 

    private boolean drag = false; 
    private Point dragLocation = new Point(); 

    public ResizablePanel() { 
     setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); 
     setPreferredSize(new Dimension(500, 500)); 
     final JFrame f = new JFrame("Test"); 
     addMouseListener(new MouseAdapter() { 
      @Override 
      public void mousePressed(MouseEvent e) { 
       drag = true; 
       dragLocation = e.getPoint(); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       drag = false; 
      } 
     }); 
     addMouseMotionListener(new MouseMotionAdapter() { 
      @Override 
      public void mouseDragged(MouseEvent e) { 
       if (drag) { 
        if (dragLocation.getX()> getWidth()-10 && dragLocation.getY()>getHeight()-10) { 
         System.err.println("in"); 
         setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())), 
           (int)(getHeight()+(e.getPoint().getY()-dragLocation.getY()))); 
         dragLocation = e.getPoint(); 
        } 
       } 
      } 
     }); 
     f.getContentPane().setLayout(new BorderLayout()); 
     f.getContentPane().add(this,BorderLayout.CENTER); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.pack(); 
     f.setVisible(true); 
    } 

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

    public void paintComponent(Graphics g) { 
     g.setColor(Color.red); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
    } 

} 
+3

Đó là cách trên đầu! Một 'JSplitPane' đơn giản sẽ hoàn thành công việc. – jjnguy

+2

tôi biết. tôi vừa trả lời câu hỏi của anh ấy. –

+0

Đây là một giải pháp thú vị. – jjnguy

-4

Bạn có thể phải chỉ định JFrame.setResizeable = true; trên cả Phụ Huynh JFrame (một với cách bố trí biên giới) và đứa trẻ JFrame.

Bạn cũng có thể muốn sử dụng JPanel ở biên giới phía nam.

+2

Anh ấy cần đổi kích thước bảng điều khiển chứ không phải khung – jjnguy

0

Tôi đã thực hiện một lớp học cho điều đó nếu bạn muốn tham gia look. Nó chưa kết thúc.

package projetoSplitPainel; 

import java.awt.Component; 
import java.util.ArrayList; 

import javax.swing.JSplitPane; 

/** 
* This Components is based on the JSplitPane. JSplitPane is used to divide two 
* (and only two) Components. This class intend to manipulate the JSplitPane in 
* a way that can be placed as many Component as wanted. 
* 
* @author Bode 
* 
*/ 
public class JSplitPaneMultiTabs extends JSplitPane { 
    private ArrayList<JSplitPane> ecanpsulationList = new ArrayList<JSplitPane>(); 
    private int numberOfComponents = 1; 
    private int sizeOfDivision = 6; 

    /** 
    * Builds the Pane 
    */ 
    public JSplitPaneMultiTabs() { 
     super(); 
     this.setLeftComponent(null); 
     this.setBorder(null); 
     ecanpsulationList.add(this); 
     setAllBorders(sizeOfDivision); 
    } 

    /** 
    * 
    * @param comp - adds a Component to the Pane 
    */ 
    public void addComponent(Component comp) { 
     JSplitPane capsule = new JSplitPane(); 

     capsule.setRightComponent(null); 
     capsule.setLeftComponent(comp); 
     capsule.setDividerSize(sizeOfDivision); 
     capsule.setBorder(null); 

     ecanpsulationList.get(numberOfComponents - 1).setRightComponent(capsule); 
     ecanpsulationList.add(capsule); 
     numberOfComponents++; 
     this.fixWeights(); 
    } 

    /** 
    * 
    * @param orientation 
    *   JSplitPane.HORIZONTAL_SPLIT - sets the orientation of the 
    *   Components to horizontal alignment 
    * @param orientation 
    *   JSplitPane.VERTICAL_SPLIT - sets the orientation of the 
    *   Components to vertical alignment 
    */ 
    public void setAlignment(int orientation) { 
     for (int i = 0; i < numberOfComponents; i++) { 
      ecanpsulationList.get(i).setOrientation(orientation); 

     } 
    } 

    /** 
    * 
    * @param newSize - resizes the borders of the all the Components of the Screen 
    */ 
    public void setAllBorders(int newSize) { 
     this.setDividerSize(newSize); 
     for (int i = 0; i < numberOfComponents; i++) { 
      ecanpsulationList.get(i).setDividerSize(newSize); 
     } 

    } 

    /** 
    * each Component added needs to be readapteded to the screen 
    */ 
    private void fixWeights() { 
     ecanpsulationList.get(0).setResizeWeight(1.0); 
     for (int i = 1; i < numberOfComponents; i++) { 
      double resize = (double) 1/(double) (i + 1); 
      ecanpsulationList.get(numberOfComponents - i - 1).setResizeWeight(
        resize); 
     } 
     ecanpsulationList.get(numberOfComponents - 1).setResizeWeight(0.0); 
    } 

} 
Các vấn đề liên quan