【问题标题】:JSplitPane - split application into two?JSplitPane - 将应用程序分成两部分?
【发布时间】:2012-11-19 00:55:18
【问题描述】:

我制作了一个 Java swing 应用程序。我的主要课程完成了整个SwingUtilities.invokeLater(new Runnable() 的工作。

我用过的第二堂课:

JPanel container = (JPanel) getContentPane();

然后通过调用添加所有位..

container.add([name of component]

我现在想将整个“应用程序”放入JSplitPane。因此,我希望我的应用程序在一侧,而其他东西在右侧。

我该怎么做?

    public class one{
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            JFrame f = new App("main");
            f.setSize(1920,1080);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
}


public class App extends JFrame {
        SuppressWarnings("empty-statement")
        public App(String title) {
        super(title);
        JPanel container = (JPanel) getContentPane();

        container.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JButton new button = new JButton("new");
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        //ENTER LOTS OF CONSTRAINTS
        container.add(new, c);

        JButton next to button = new JButton("next to");
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 2;
        //ENTER LOTS OF CONSTRAINTS
        container.add(new, c);

        //I HAVE LOTS OF SWING COMPONENTS AND BUTTONS about 30

}

我希望所有这些都在拆分窗格的左侧?

我该怎么做?

【问题讨论】:

  • 一种方法是将内容放入两个容器(例如JPanel)并将它们添加到拆分窗格中,然后将拆分窗格添加到内容窗格中。为了尽快获得更好的帮助,请发布当前代码的SSCCE(而不是代码 sn-ps 和描述)。
  • 这样更好吗?我只是写了一些模拟代码来演示我的观点。
  • 我看不出你为什么要创建 one 类只是为了将 main 方法放入其中。
  • “一个”类用于多线程应用程序...我认为
  • 另见example

标签: java swing jpanel jsplitpane


【解决方案1】:
  1. 不要从JFrame 扩展,您不会添加任何功能,而是将所有应用程序组件和逻辑移动到单独的JPanel
  2. 创建JSplitPane 的实例,向其中添加“主”面板,向其中添加辅助面板
  3. 创建JFrame 的实例,将拆分窗格添加到其中...

更新

public class TestSplitPane {

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

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

                JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                pane.setLeftComponent(new MainPane());
                pane.setRightComponent(new JLabel("On the right"));

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

    public class MainPane extends JPanel {

        public MainPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            JButton newButton = new JButton("new");
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 2;
            //ENTER LOTS OF CONSTRAINTS
            add(newButton, c);

            JButton next = new JButton("next to");
            c.gridx = 1;
            c.gridy = 1;
            c.gridwidth = 2;
            //ENTER LOTS OF CONSTRAINTS
            add(next, c);
        }
    }
}

【讨论】:

  • 请您帮我输入我们的快速 SSCCE 吗?
  • “我刚刚将这些方法应用到我的代码中,并且成功了!” 我的意思是,它当然会做出改变;)
  • =] 任何原因;当我将中间的拆分向左分级时,它不会移动并调整所有摆动组件的大小?
  • 拆分窗格可能会受到面板首选/最小尺寸的限制
  • 好吧,如果我调整整个窗口的大小,它会完美缩放,但拒绝在窗格中缩放。真的不确定=S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 2019-11-05
  • 1970-01-01
相关资源
最近更新 更多