在JTabbedPane上调整JScrollPane的大小

时间:2016-01-20 22:54:19

标签: java jtable jtabbedpane

我有几个标签,在其中一个标签上我放了一个滚动窗格。我想在它下面添加另一个窗格,以便我可以添加一些按钮。它只是继续调整以填充整个选项卡。我尝试过setize和setprefered尺寸,但没有运气。任何人都可以看到我做错了什么或指出我正确的方向?非常感谢!

 JFrame frame = new JFrame(); 
    JScrollPane pane = new JScrollPane(table);
    pane.setBounds(0, 0, 415, 50);     // < ---This seems to do nothing
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    JTabbedPane jtp = new JTabbedPane();
    jtp.setBounds(-11, -14, 436, 170); // <---- I used -ve to hide border       

    jtp.addTab("tab1", new JLabel("Tab1"));
    jtp.addTab("tab2", new JLabel("Tab2"));
    jtp.addTab("tab3", new JLabel("Tap3"));
    jtp.addTab("tab4", new JLabel("Tab4"));
    jtp.addTab("tab5", pane);
    jtp.setTabPlacement(JTabbedPane.BOTTOM);
    frame.add(jtp,BorderLayout.CENTER);

1 个答案:

答案 0 :(得分:1)

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;

public class ScrollSizeRedone {
    public static void main(final String[] args) {

        final JFrame frame = new JFrame();

        final JTable table = new JTable(new DefaultTableModel(new String[][] { { "a", "b" }, { "c", "d" } }, new String[] { "col1", "col2" }));
        final JScrollPane pane = new JScrollPane(table);
        //      pane.setBounds(0, 0, 415, 50); // < ---This seems to do nothing // no use
        pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        final JPanel completePanel = new JPanel();
        completePanel.setLayout(new BorderLayout());
        completePanel.add(pane);

        final JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
        buttonsPanel.add(new JButton("LOL"));
        buttonsPanel.add(Box.createHorizontalStrut(100));
        buttonsPanel.add(new JButton("ROFL"));
        buttonsPanel.add(Box.createHorizontalGlue());
        buttonsPanel.add(new JButton("MUAHAHA"));
        completePanel.add(buttonsPanel, BorderLayout.SOUTH);

        final JTabbedPane jtp = new JTabbedPane();
        //      jtp.setBounds(-11, -14, 436, 170); // <---- I used -ve to hide border
        jtp.addTab("tab1", new JLabel("Tab1"));
        jtp.addTab("tab2", new JLabel("Tab2"));
        jtp.addTab("tab3", new JLabel("Tap3"));
        jtp.addTab("tab4", new JLabel("Tab4"));
        jtp.addTab("tab5", completePanel);

        jtp.setTabPlacement(JTabbedPane.BOTTOM);
        frame.add(jtp, BorderLayout.CENTER);

        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}