【问题标题】:JPanel in JTabBar is not scrollableJTabBar 中的 JPanel 不可滚动
【发布时间】:2014-07-23 07:51:37
【问题描述】:

在此处输入代码我遇到以下问题。

我将两个 JPanel 添加到一个面板,最后将其添加到我的 TabbedPane。但是,我想让它可滚动,因此我只是使面板可滚动,这只是将我的滚动条添加到栏的中间。

这是我的例子:

public class Test extends JFrame {

    private static final long serialVersionUID = -4682396888922360841L;    
    private JMenuBar menuBar;    
    private JMenu mAbout;    
    private JMenu mMain;    
    private JTabbedPane tabbedPane;    
    public SettingsTab settings = new SettingsTab();

    private void addMenuBar() {    
        menuBar = new JMenuBar();    
        mMain = new JMenu("Main");
        mAbout = new JMenu("About");    
        menuBar.add(mMain);
        menuBar.add(mAbout);    
        setJMenuBar(menuBar);    
    }

    public void createTabBar() {    
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);    
        JComponent panel2 = new JPanel();
        tabbedPane.addTab("Test1", null, panel2,
                "Displays the results");    
        tabbedPane.addTab("Settings", settings.createLayout());    
        add(tabbedPane);    
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);    
    }

    private void makeLayout() {    
        setTitle("Test");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));    
        addMenuBar();
        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);    
    }    

    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) {    
        Test gui = new Test();
        gui.start();    
    }

    public class SettingsTab extends JPanel {        
        public JPanel createLayout() {
            JPanel panel = new JPanel();    
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));    
            panel.add(table1());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table2());
            panel.add(Box.createRigidArea(new Dimension(0,10)));                
            panel.add(new JScrollBar());    
            return panel;
        }

        public JPanel table1() {
            JPanel panel1 = new JPanel();    
            String[] columnNames = {"First Name", "Last Name"};    
            Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
                    {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
            };    
            final JTable table = new JTable(data, columnNames);    
            panel1.add(table);    
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));    
            return panel1;
        }

        public JPanel table2() {
            JPanel panel1 = new JPanel();    
            String[] columnNames = {"First Name", "Last Name"};    
            Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"},
                    {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
            };    
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);    
            panel1.add(table);    
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));    
            return panel1;
        }
    }
}

任何建议我做错了什么?

感谢您的回复!

更新

我用 JScrollPane 试过了,但还是不行:

public class Test extends JFrame {

    private static final long serialVersionUID = -4682396888922360841L;

    private JMenuBar menuBar;

    private JMenu mAbout;

    private JMenu mMain;

    private JTabbedPane tabbedPane;

    public SettingsTab settings = new SettingsTab();

    private void addMenuBar() {

        menuBar = new JMenuBar();

        mMain = new JMenu("Main");
        mAbout = new JMenu("About");

        menuBar.add(mMain);
        menuBar.add(mAbout);

        setJMenuBar(menuBar);

    }

    public void createTabBar() {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        JComponent panel2 = new JPanel();
        tabbedPane.addTab("Test1", null, panel2,
                "Displays the results");

        tabbedPane.addTab("Settings", settings.createLayout());

        add(tabbedPane);

        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    }

    private void makeLayout() {

        setTitle("Test");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));

        addMenuBar();
        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) {

        Test gui = new Test();
        gui.start();

    }

    public class SettingsTab extends JScrollPane {


        public JPanel createLayout() {
            JPanel panel = new JPanel();

            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            panel.add(table1());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table2());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table3());
            panel.add(Box.createRigidArea(new Dimension(0,10)));

            add(new JScrollPane());

            return panel;
        }

        public JPanel table1() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name",
            "Last Name"};

            Object[][] data = {
                    {"Kathy", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

            final JTable table = new JTable(data, columnNames);

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }

        public JPanel table2() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name",
            "Last Name"};

            Object[][] data = {
                    {"Kathy", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }

        public JPanel table3() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name",
            "Last Name"};

            Object[][] data = {
                    {"Kathy", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }
    }
}

【问题讨论】:

  • @BackSlash 我编辑了标题,以免重复!
  • 也可以看到Initial Thread
  • 很好地使用SwingUtilities.invokeLaterpack
  • 它不起作用,因为您只需添加 jscrollpane!

标签: java swing jtable jpanel jscrollbar


【解决方案1】:

您必须在其上添加一个 JScrollPane! :)

我真的不知道你在那里做了什么,但基本上是这样的:

JPanel myPanel = new JPanel(); // the main content is on the myPanel
myPanel.add(myContent);// add everything you want

new JFrame().add(new JScrollPane(myPanel));

【讨论】:

  • 不,它不需要大小,如果您有任何内容,面板会调整为内容的大小:)
  • 感谢您的回答! TabBar 也可以这样做吗?
  • 我不知道.. 我想是的
【解决方案2】:

JScrollPane 中添加JTable,然后在JPanel 中添加JScrollPane

JScrollPane pane = new JScrollPane(table);
panel1.add(pane);

下面这行就不需要了:

panel.add(new JScrollBar());

请参阅How to Use Tables 上的 Swing 教程,您也可以在其中找到示例。


您可以根据需要设置JPanel 的大小(并非总是需要),然后将其添加到JScrollPane

JPanel panel = new JPanel(){
    @Override
    public Dimension getSize(){
        return new Dimension(...,....);
    }
};

编辑

您没有将它添加到JScrollPane 中。应该是

tabbedPane.addTab("Settings", new JScrollPane(settings.createLayout()));

并删除以下行:

add(new JScrollPane());

【讨论】:

  • 非常感谢您的回答!但是,这只会使 JTable 可滚动。是否也可以让 JPanel 在表格所在的位置可滚动?
  • 为什么需要 JPanel 可滚动?你想滚动什么?
  • 感谢您的回答!我只想在选项卡中滚动 JPanel。因此,JPanel 的站点上应该有一个可滚动的元素。
  • 我没有收到您的更新。 then add it inside the JScrollPane 是什么意思?
  • 是的,您可以简单地在 JScrollPane 中添加 JPanel,就像为 JTable 所做的那样。
【解决方案3】:

非常感谢您的回答!但是,这只会使 JTable 可滚动。是否也可以让 JPanel 在表格所在的位置可滚动?

如果这是你需要的

那么你可以这样做:

public JScrollPane createLayout() {

    JPanel panel = new JPanel();
    JScrollPane sp = new JScrollPane(panel);
    sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                    // or ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    panel.add(table1());
    panel.add(Box.createRigidArea(new Dimension(0,10)));
    panel.add(table2());
    panel.add(Box.createRigidArea(new Dimension(0,10)));


    return sp;
}

【讨论】:

  • 不错!非常感谢,正是我想要的!
  • @Vivien 很酷,不客气!如果您不想更改 createlayout 的签名,您可能还想考虑 Braj 的最后一次编辑。
猜你喜欢
  • 2010-11-26
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2012-04-16
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多