gpt4 book ai didi

java - 在 JTabbedPane 上调整 JScrollPane 的大小

转载 作者:行者123 更新时间:2023-11-30 07:39:22 24 4
gpt4 key购买 nike

我有几个选项卡,在其中一个选项卡上放置了一个滚动 Pane 。我想在它下面添加另一个 Pane ,以便我可以添加一些按钮。它只是不断重新调整以填充整个选项卡。我尝试过 setsize 和 setpreferred size 但没有运气。谁能看到我做错了什么或指出我正确的方向?非常感谢!

 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);

最佳答案

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);
}
}

关于java - 在 JTabbedPane 上调整 JScrollPane 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com