使JPanel手动resize
我有一个带BorderLayout
的JFrame
作为布局管理器。
在南边界,我有一个JPanel
,我希望这个JPanel
的大小可以由用户调整,即用户可以点击边框的边缘并将其向上拖动以使其更大。
有什么方法你知道我能做到吗?
为了使框架中的面板可单独resize,您需要将它们添加到JSplitPane
。
不要将它放在框架的南部,而是将JSplitPane
放在中心。 拆分窗格将使拆分中的底部面板看起来像在南部,并且拆分中的顶部面板将位于框架的中心。
确保使用setOrientation(JSplitPane.VERTICAL_SPLIT )
设置两个面板的方向。
然后,您可以调整窗格中的面板的大小。
我想你想说JPanel。 您可以添加自定义mouseListener并处理鼠标单击,拖动和鼠标释放,然后以programmaticaly方式调整面板大小。
这将certificate这一点。 请注意,jframe不会使用JPanel自动resize。 为了使效果更加明显,我将面板涂成红色并添加了斜面边框:
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 ecanpsulationList = new ArrayList (); 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
。
- 无法从类型对象转换为long
- SelectOneMenu更新其他SelectOneMenu
- 在alpine / Jprofile 10中运行jpenable时出现UnsatisfiedLinkError
- 如何告诉visualvm在哪里找到我的源代码?
- 找不到com.fasterxml.jackson.core.JsonParseException的类文件
- 如何使用Hibernate Projection检索复杂的类及其成员?
- MyBatis / iBatis – 在单独的SQL Map文件中可重用的sql片段?
- 如何使用Struts框架构造自定义错误json响应
- mvn + jetty出错:无法执行目标org.eclipse.jetty:jetty-maven-plugin: