两个拆分窗格放置在tabpane上

时间:2017-01-07 15:10:08

标签: java swing jtabbedpane jsplitpane

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;


public class MenuPage extends JFrame{

    final static int extraWindowWidth = 100;

    JSplitPane jSplitPane, jSplitPane2,jSplitPane3,jSplitPane4;
    JPanel jPanel2a, jPanel2b, jPanel3;

    public static JFrame frame; 

    public MenuPage(){
    }

    private final JList<String> list1=new JList<>(new String[]{"A","B"});
    private final JList<String> list2 = new JList<>();
    private final List<DefaultListModel> models = new ArrayList<>();



    public void addComponentToPane(Container pane) {
        JTabbedPane tabbedPane = new JTabbedPane();



        // Create the "cards".
        JPanel card1 = new JPanel() {
            // Make the panel wider than it really needs, so
            // the window's wide enough for the tabs to stay
            // in one row.
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                size.width += extraWindowWidth;
                return size;
            }
        };


         DefaultListModel<String> model1 = new DefaultListModel<>();
            model1.addElement("A1");
            model1.addElement("A2");
            model1.addElement("A3");
            models.add(model1);
            DefaultListModel<String> model2 = new DefaultListModel<>();
            model2.addElement("B1");
            model2.addElement("B2");
            models.add(model2);
            list2.setModel(model1);
            list1.addListSelectionListener((ListSelectionEvent e) -> {
                if (!e.getValueIsAdjusting()) {
                    list2.setModel(models.get(list1.getSelectedIndex()));
                }
            });






        jPanel2a = new JPanel();
        jPanel2b = new JPanel();
        jPanel3 = new JPanel();







        jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);





        jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
        jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);




        // panelin icerisine component eklendi
        JLabel lblHelloWorld = new JLabel("Hello World!");
        jPanel2a.add(lblHelloWorld);

        jSplitPane2.setOneTouchExpandable(true);
        jSplitPane2.setDividerLocation(100);


        jSplitPane3.setOneTouchExpandable(true);
        jSplitPane3.setDividerLocation(150);


        jSplitPane4.setOneTouchExpandable(true);
        jSplitPane4.setDividerLocation(300);



        JPanel card3 = new JPanel();
        JPanel card4 = new JPanel();
        JPanel card5 = new JPanel();
        JPanel card6 = new JPanel();
        JPanel card7 = new JPanel();
        JPanel card8 = new JPanel();



        card3.add(jSplitPane3,jSplitPane4);


        tabbedPane.addTab("Main", jSplitPane2);

        tabbedPane.addTab("Leagues",jSplitPane4);


        tabbedPane.addTab("Teams",card3 );
        tabbedPane.addTab("Ratios", card4);
        tabbedPane.addTab("Competitions", card5);
        tabbedPane.addTab("Analyze", card6);
        tabbedPane.addTab("Help", card7);
        tabbedPane.addTab("About", card8);

        pane.add(tabbedPane, BorderLayout.CENTER);


    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */

    public static void createAndShowGUI() {
        // Create and set up the window.
        frame = new JFrame("BetAnalyzer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(594, 310);

        // Create and set up the content pane.
        MenuPage demo = new MenuPage();
        demo.addComponentToPane(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setBounds(100, 100, 635, 251);

    }
}

我希望list1,list2和jpanel3将第2部分JSplitPane放在联盟选项卡中我的代码有问题。只需jSplitPane3或jSplitPane4就可以出现在布局中。

另外,我尝试将两个JSplitPane添加到Box中,然后我将Box放入tabbedpane,但这两个都不起作用。

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题。

没有理由继承

您的类继承自JFrame,但它不会扩展其行为(它不会覆盖JFrame的公共或受保护方法。

组件只能添加一次

与其他观点相比,这本身不是问题,而是问题的一部分。

 jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
 // ...     
 card3.add(jSplitPane3,jSplitPane4);

执行第二个语句时,jSplitPane3已从jSplitPane4中移除,仅显示为card3的内容。

使用标准布局

面板的(虚拟)内容相当小,因此preferedSize小于父容器中的可用空间。

JPanels的默认布局为FlowLayout,可将其每个组件缩减为preferedSize

您应该将JPanel的布局设置为BorderLayout,以允许其内容延伸到整个空间。

反过来你不能使用JPanel的varag方法.add(Component, component) 了。

以下是相关变化:

    jPanel2a = new JPanel(new BorderLayout());
    jPanel2b = new JPanel(new BorderLayout());
    jPanel3 = new JPanel(new BorderLayout());

    jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);

    jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
    jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jSplitPane3, jPanel3);

    // panelin icerisine component eklendi
    JLabel lblHelloWorld = new JLabel("Hello World!");
    jPanel2a.add(lblHelloWorld);

//      jSplitPane2.setOneTouchExpandable(true);
        jSplitPane2.setDividerLocation(100);
//
//      jSplitPane3.setOneTouchExpandable(true);
        jSplitPane3.setDividerLocation(150);
//
//      jSplitPane4.setOneTouchExpandable(true);
        jSplitPane4.setDividerLocation(300);

    JPanel card3 = new JPanel(new BorderLayout());
    JPanel card4 = new JPanel(new BorderLayout());
    JPanel card5 = new JPanel(new BorderLayout());
    JPanel card6 = new JPanel(new BorderLayout());
    JPanel card7 = new JPanel(new BorderLayout());
    JPanel card8 = new JPanel(new BorderLayout());

//      card3.add(jSplitPane3);
    card3.add(jSplitPane4,BorderLayout.SOUTH);

    tabbedPane.addTab("Main", jSplitPane2);