jsplitpane-可以设置最大尺寸;
自从我来这里以来已经有一段时间了。我正在学习Java,并且有一个问题,即为什么我在JSPlitpane中创建的面板可以调整我设定的最大值的大小:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow {
//set all components
JFrame frame;
JPanel showPanel;//displays individual contact when clicked on in the contacts panel;
JPanel listPanel;// displays the contactsPanel
JSplitPane contactsSplitPane;
public void buildMainWindow() {// open method
frame = new JFrame("Contacts");
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
showPanel = new JPanel();
showPanel.setBackground(Color.WHITE);
listPanel = new JPanel();
listPanel.setBackground(Color.LIGHT_GRAY);
listPanel.setMaximumSize(new Dimension (300,1000));
//create SplitPane for the listPanel and showPanel
contactsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listPanel,showPanel);
contactsSplitPane.setOneTouchExpandable(true);
contactsSplitPane.setDividerLocation(50);
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.add(BorderLayout.CENTER, contactsSplitPane);
}//close method
public static void main (String [] args) {
MainWindow MainWindow = new MainWindow ();
MainWindow.buildMainWindow();
}
}// close class
随时运行和编译。我已经将ListPanel的大小设置为最多300个像素,但是我可以将其调整范围超过此 - 几乎可以到框架的末端。不可能将单个可重新覆盖的窗格箱,不是吗?
有人可以让我知道我在做什么错吗?我显然缺少一些东西,但我不知道。
Been a while since I've been here. I'm learning java and have a question as to why the panel I've created in a JSplitPane can be resized beyond the maximum that I've set:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow {
//set all components
JFrame frame;
JPanel showPanel;//displays individual contact when clicked on in the contacts panel;
JPanel listPanel;// displays the contactsPanel
JSplitPane contactsSplitPane;
public void buildMainWindow() {// open method
frame = new JFrame("Contacts");
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
showPanel = new JPanel();
showPanel.setBackground(Color.WHITE);
listPanel = new JPanel();
listPanel.setBackground(Color.LIGHT_GRAY);
listPanel.setMaximumSize(new Dimension (300,1000));
//create SplitPane for the listPanel and showPanel
contactsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listPanel,showPanel);
contactsSplitPane.setOneTouchExpandable(true);
contactsSplitPane.setDividerLocation(50);
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.add(BorderLayout.CENTER, contactsSplitPane);
}//close method
public static void main (String [] args) {
MainWindow MainWindow = new MainWindow ();
MainWindow.buildMainWindow();
}
}// close class
feel free to run and compile. I've set the size of the listPanel to a maximum of 300 pixels, but I can resize it way beyond that -- almost to the end of the frame. It's not possible to crate a single resizable pane, no?
Can someone let me know what I'm doing wrong? I'm obviously missing something, but I don't know what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jsplitpane
不尊重任何一个组件的最大大小。但是,它确实尊重组件的最小尺寸。
因此,可以将一种方法设置为添加到拆分窗格的另一个组件上的最小尺寸。您需要覆盖此组件的
get -minimumSize()
方法,因为拆分窗格的大小可以动态变化。类似:
现在,红色面板的宽度只能在100到200像素之间尺寸。
A
JSplitPane
doesn't respect the maximum size of either component.However, it does respect the minimum size of a component.
So one approach could be do set the minimum size on the other component added to the split pane. You will need to override the
getMinimumSize()
method of this component since the size of the split pane can change dynamically.Something like:
Now the width of the red panel can only be sized between 100 and 200 pixels.