Java Swing JSplitPane没有按预期显示

时间:2014-06-24 02:54:13

标签: java swing user-interface

我试图制作一个基于5个Jsplitpanes和内部组件的程序,但它们似乎并不能很好地相处。因为当我运行应用程序时,所有拆分窗格都集中在左上角

enter image description here

代码非常简单:

projectExplorer = new ProjectExplorer();
    editorExplorer = new EditorExplorer();
    favDownload = new FavDownload();
    favDownloadExplorer = new FavDownloadExplorer();
    //Define the listeners
    addComponentListener(new ResizeListener());

    horTopPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    horTopPane.setLeftComponent(projectExplorer);
    horTopPane.setRightComponent(editorExplorer);

    horBotPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    horBotPane.setLeftComponent(favDownload);
    horBotPane.setRightComponent(favDownloadExplorer);

    verticalPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    verticalPane.setTopComponent(horTopPane);
    verticalPane.setBottomComponent(horBotPane);


    setContentPane(verticalPane);
    new Timer().schedule(new TimerTask() {          
        @Override
        public void run() {
            resizeComponents();
        }
    }, 100);

resizeComponents方法是

public void resizeComponents(){
    int width = getWidth();
    int height = getHeight();
    horTopPane.setMinimumSize(new Dimension(width, (height/2)+(height/4)));
    horTopPane.setSize(horTopPane.getMinimumSize());
    horBotPane.setMinimumSize(new Dimension(width, (height/8)));
    horBotPane.setSize(horBotPane.getMinimumSize());
    projectExplorer.setMinimumSize(new Dimension(width/8, horTopPane.getHeight()));
    projectExplorer.setSize(projectExplorer.getMinimumSize());
    editorExplorer.setMinimumSize(new Dimension(width/2, horTopPane.getHeight()));
    editorExplorer.setSize(editorExplorer.getMinimumSize());
    favDownload.setMinimumSize(new Dimension(width/8, horBotPane.getHeight()));
    favDownload.setSize(favDownload.getMinimumSize());
    favDownloadExplorer.setMinimumSize(new Dimension(width/2, horBotPane.getHeight()));
    favDownloadExplorer.setSize(favDownloadExplorer.getMinimumSize());
}

它就像setSize一样开始,但由于某些原因它根本没有调整它们的大小,然后我推出了setMinimumSize并且它也没有调整它的大小,但是当我在程序上移动一些元素时它们都达到了最小尺寸,我错过了什么?

我已经制作了一些Java GUI程序,但是这个程序并不想正常工作。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,请不要混淆setMinimum/Maximum/PreferredSize,请查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?了解更多详情

接下来,不要从事件调度线程以外的任何线程更新UI组件的状态,Swing不是线程安全的。 java.util.Timer将在其自己的线程上下文中触发事件通知。

接下来,考虑从您的自定义组件覆盖(至少)getPreferredSize并返回默认值,例如......

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SplitPaneTest {

    public static void main(String[] args) {
        new SplitPaneTest();
    }

    public SplitPaneTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final ProjectExplorer projectExplorer;
        private final EditorExplorer editorExplorer;
        private final FavDownload favDownload;
        private final FavDownloadExplorer favDownloadExplorer;
        private final JSplitPane horTopPane;
        private final JSplitPane horBotPane;
        private final JSplitPane verticalPane;

        public TestPane() {
            projectExplorer = new ProjectExplorer();
            editorExplorer = new EditorExplorer();
            favDownload = new FavDownload();
            favDownloadExplorer = new FavDownloadExplorer();

            horTopPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            horTopPane.setLeftComponent(projectExplorer);
            horTopPane.setRightComponent(editorExplorer);

            horBotPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            horBotPane.setLeftComponent(favDownload);
            horBotPane.setRightComponent(favDownloadExplorer);

            verticalPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            verticalPane.setTopComponent(horTopPane);
            verticalPane.setBottomComponent(horBotPane);

            setLayout(new BorderLayout());
            add(verticalPane);
        }
    }

    protected abstract class AbstractPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class ProjectExplorer extends AbstractPane {

        public ProjectExplorer() {
            setBackground(Color.RED);
        }

    }

    public class EditorExplorer extends AbstractPane {

        public EditorExplorer() {
            setBackground(Color.BLUE);
        }

    }

    public class FavDownload extends AbstractPane {

        public FavDownload() {
            setBackground(Color.MAGENTA);
        }

    }

    public class FavDownloadExplorer extends AbstractPane {

        public FavDownloadExplorer() {
            setBackground(Color.CYAN);
        }

    }

}

如果您必须修改分频器的位置,请考虑使用JSplitSpane#setDividerLocation(double)JSplitSpane#setDividerLocation(int)

请查看How to Use Split Panes了解详情