jsplitpane-可以设置最大尺寸;

发布于 2025-02-07 21:21:26 字数 1583 浏览 1 评论 0原文

自从我来这里以来已经有一段时间了。我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2025-02-14 21:21:26

jsplitpane不尊重任何一个组件的最大大小。

但是,它确实尊重组件的最小尺寸。

因此,可以将一种方法设置为添加到拆分窗格的另一个组件上的最小尺寸。您需要覆盖此组件的get -minimumSize()方法,因为拆分窗格的大小可以动态变化。

类似:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplitPaneMaximum extends JPanel
{
    JSplitPane splitPane;

    public SplitPaneMaximum()
    {

        setLayout( new BorderLayout() );

        JPanel red = new JPanel();
        red.setBackground( Color.RED );
        red.setPreferredSize( new Dimension(200, 100) );
        red.setMinimumSize( new Dimension(100, 0) );

        JPanel blue = new JPanel()
        {
            //  Setting a minimum size here will limit the maximum size
            //  of the other component added to the split pane

            @Override
            public Dimension getMinimumSize()
            {
                int parentWidth = getParent().getSize().width;
                Dimension d = getSize();
                d.width = parentWidth - 200;

                return d;
            }
        };

        blue.setBackground( Color.BLUE );
        blue.setPreferredSize( new Dimension(200, 100) );

        splitPane = new JSplitPane();
        splitPane.setLeftComponent( red );
        splitPane.setRightComponent( blue );
        splitPane.setResizeWeight(0.50);
        add(splitPane, BorderLayout.CENTER);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SplitPaneMaximum");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SplitPaneMaximum() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

现在,红色面板的宽度只能在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:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplitPaneMaximum extends JPanel
{
    JSplitPane splitPane;

    public SplitPaneMaximum()
    {

        setLayout( new BorderLayout() );

        JPanel red = new JPanel();
        red.setBackground( Color.RED );
        red.setPreferredSize( new Dimension(200, 100) );
        red.setMinimumSize( new Dimension(100, 0) );

        JPanel blue = new JPanel()
        {
            //  Setting a minimum size here will limit the maximum size
            //  of the other component added to the split pane

            @Override
            public Dimension getMinimumSize()
            {
                int parentWidth = getParent().getSize().width;
                Dimension d = getSize();
                d.width = parentWidth - 200;

                return d;
            }
        };

        blue.setBackground( Color.BLUE );
        blue.setPreferredSize( new Dimension(200, 100) );

        splitPane = new JSplitPane();
        splitPane.setLeftComponent( red );
        splitPane.setRightComponent( blue );
        splitPane.setResizeWeight(0.50);
        add(splitPane, BorderLayout.CENTER);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SplitPaneMaximum");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SplitPaneMaximum() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Now the width of the red panel can only be sized between 100 and 200 pixels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文