2009-07-13 12 views
10

मेरे पास लेआउट मैनेजर के रूप में BorderLayout के साथ JFrame है।एक जेपीनल मैन्युअल रूप से आकार बदलने योग्य

दक्षिण सीमा में, मैं एक JPanel है, मैं इस JPanel के आकार उपयोगकर्ता द्वारा समायोज्य होना चाहते हैं, जैसे कि उपयोगकर्ता सीमा के किनारे पर क्लिक करें और इसे यह बड़ा बनाने के लिए ऊपर खींच सकते हैं।

क्या कोई तरीका है कि आप जानते हैं कि मैं यह कर सकता हूं?

उत्तर

19

आदेश में एक फ्रेम में पैनल बनाने के लिए अलग-अलग आकार बदलने योग्य आप उन्हें एक JSplitPane पर जोड़ने की जरूरत है।

इसके बजाय फ्रेम के दक्षिण भाग में डालने की

, केंद्र में JSplitPane डाल दिया। स्प्लिट फलक स्प्लिट में निचला पैनल बना देगा जैसा कि यह दक्षिण में है, और विभाजन में शीर्ष पैनल फ्रेम के केंद्र में होगा।

आप setOrientation(JSplitPane.VERTICAL_SPLIT) के साथ दो पैनलों के उन्मुखीकरण सेट सुनिश्चित करें।

फिर, आप फलक में मौजूद पैनलों का आकार बदल सकते हैं।

11

मुझे लगता है कि आप जेपीनल कहना चाहते हैं। आप एक कस्टम माउस लिस्टर जोड़ सकते हैं और माउस क्लिक, ड्रैग और माउस रिलीज को नियंत्रित कर सकते हैं और फिर पैनल प्रोग्रामेटिकल का आकार बदल सकते हैं।

यह यह प्रदर्शित करेगा। ध्यान दें कि jframe जेपीनल के साथ स्वचालित रूप से आकार बदल नहीं आता है। प्रभाव अधिक दिखाई मैं पैनल चित्रित लाल और एक beveled सीमा जोड़ा बनाने के लिए:

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

शीर्ष पर रास्ता है कि! एक सरल 'JSplitPane' काम पूरा हो जाएगा। – jjnguy

+2

मुझे पता है। मैंने अभी अपने प्रश्न का उत्तर दिया। –

+0

हालांकि यह एक दिलचस्प समाधान है। – jjnguy

-4

आप दोनों जनक JFrame (सीमा लेआउट के साथ एक) और बच्चे JFrame पर JFrame.setResizeable = true; निर्दिष्ट करने के लिए हो सकता है।

तुम भी दक्षिण सीमा में एक JPanel उपयोग करने के लिए चाहते हो सकता है।

+2

उसे पैनल का आकार बदलने की जरूरत है, फ्रेम नहीं – jjnguy

0

यदि आप look लेना चाहते हैं तो मैंने इसके लिए एक कक्षा बनाई है। यह अभी तक खत्म नहीं हुआ है।

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); 
    } 

} 
संबंधित मुद्दे