jpanel을 수동으로 수동으로 만들 수 있습니다
-
12-09-2019 - |
문제
나는있다 JFrame
~와 함께 BorderLayout
레이아웃 관리자로서.
남쪽 국경에는 a가 있습니다 JPanel
, 나는 이것을 원한다 JPanel
사용자가 조정할 수있는 크기는 사용자가 테두리의 가장자리를 클릭하여 더 크게 만들 수 있습니다.
내가 할 수 있다는 것을 아는 방법이 있습니까?
해결책
프레임으로 패널을 개별적으로 재개 할 수 있으려면 패널을 JSplitPane
.
프레임의 남쪽 부분에 넣는 대신 JSplitPane
가운데에. 분할 창은 스플릿의 하단 패널을 남쪽에있는 것처럼 보이고 분할의 상단 패널은 프레임 중앙에 있습니다.
두 패널의 방향을 setOrientation(JSplitPane.VERTICAL_SPLIT )
.
그런 다음 창에있는 패널의 크기를 조정할 수 있습니다.
다른 팁
나는 당신이 jpanel이라고 말하려고했다고 생각합니다. 사용자 정의 MousElistener를 추가하고 마우스 클릭을 처리하고 드래그 및 마우스 릴리스를 처리 한 다음 Panel Programmaticaly 크기를 조정할 수 있습니다.
이것은 이것을 보여줄 것입니다. JFrame은 JPANEL과 자동으로 크기를 조정하지 않습니다. 효과를보다 눈에 띄게 만들기 위해 패널을 빨간색으로 칠하고 경사적 인 테두리를 추가했습니다.
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());
}
}
나는 당신이 가져 가고 싶다면 수업을 만들었습니다. 바라보다. 아직 끝나지 않았습니다.
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);
}
}
지정해야 할 수도 있습니다 JFrame.setResizeable = true;
부모 모두 JFrame
(국경 레이아웃이있는 사람)와 아이 JFrame
.
당신은 또한 사용하고 싶을 수도 있습니다 JPanel
남쪽 국경에서.