java 如何让 JTabbedPane 自动调整大小以适应页面尺寸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2510310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make JTabbedPane autoresize to fit page dimensions?
提问by teamon
I have only JTabbedPane inside JFrame. JTabbedPane sets its dimensions to biggest page width/height. As pages has different size is it possible to force JTabbedPane to change its dimensions when selecting other page?
我在 JFrame 中只有 JTabbedPane。JTabbedPane 将其尺寸设置为最大页面宽度/高度。由于页面大小不同,是否可以在选择其他页面时强制 JTabbedPane 更改其尺寸?
Top one is how it behave now and bottom one is how i want it to behave (i resized frame by hand)
顶部是它现在的行为方式,底部是我希望它的行为方式(我手动调整了框架的大小)
回答by Eugene Ryzhikov
This is fairly simple. It involves dynamic calculation of differences between your pages dimensions and the using them to force preferred size on you JTabbedPane. I did a quick experiment and it worked. So instead of putting a lot of text here - here is the code. It is not perfect but you should get an idea. Questions are welcome, of course.
这相当简单。它涉及动态计算您的页面尺寸之间的差异,并使用它们来强制您选择首选尺寸JTabbedPane。我做了一个快速的实验,它奏效了。因此,与其在此处放置大量文本,不如将代码放在此处。它并不完美,但你应该有一个想法。当然,欢迎提问。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
private static int maxW = 0;
private static int maxH = 0;
public static void main(String[] args) {
final JFrame f = new JFrame();
final JTabbedPane tabs = new JTabbedPane();
tabs.add( createPanel(Color.RED, 100, 100), "Red");
tabs.add( createPanel(Color.GREEN, 200, 200), "Green");
tabs.add( createPanel(Color.BLUE, 300, 300), "Blue");
final Dimension originalTabsDim = tabs.getPreferredSize();
tabs.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Component p = ((JTabbedPane) e.getSource()).getSelectedComponent();
Dimension panelDim = p.getPreferredSize();
Dimension nd = new Dimension(
originalTabsDim.width - ( maxW - panelDim.width),
originalTabsDim.height - ( maxH - panelDim.height) );
tabs.setPreferredSize(nd);
f.pack();
}
});
f.setContentPane(tabs);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static final JPanel createPanel( Color color, int w, int h ) {
JPanel p = new JPanel();
p.setBackground(color);
p.setPreferredSize( new Dimension(w, h));
maxW = Math.max(w, maxW);
maxH = Math.max(h, maxH);
return p;
}
}
回答by jfpoilpret
I think another option is to dynamically change the panels of each tab when the tab is selected:
我认为另一种选择是在选择选项卡时动态更改每个选项卡的面板:
- install a listener on
JTabbedPaneselection - install an empty panel on every tab but the selected tab by default (that contains the real panel for that tab)
- in the selection listener:
- remove the panel from the previously selected tab (ie, replace it with an empty panel)
- change the empty panel by the real panel in the newly selected tab
- call
pack()on the window/dialog containing theJTabbedPane
- 在
JTabbedPane选择时安装侦听器 - 在每个选项卡上安装一个空面板,但默认情况下选择的选项卡(包含该选项卡的真实面板)
- 在选择侦听器中:
- 从先前选择的选项卡中删除面板(即,将其替换为空面板)
- 在新选择的选项卡中通过真实面板更改空面板
- 调用
pack()包含JTabbedPane
Disclaimer: I haven't tested this approach but I believe it should work according to what you want.
免责声明:我还没有测试过这种方法,但我相信它应该可以根据您的需要工作。
Please also note that dynamically changing the size of the dialog based on the selected tab is not very user-friendly from a pure GUI viewpoint.
另请注意,从纯 GUI 的角度来看,基于所选选项卡动态更改对话框的大小对用户不是很友好。
回答by ben2613
How about this?
这个怎么样?
tabbedPane.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent arg0) {
Component mCompo=tabbedPane.getSelectedComponent();
tabbedPane.setPreferredSize(mCompo.getPreferredSize());
BasicFrame.this.pack();
}
});

