How to set a JFrame size to fit the CardLayout displayed JPanel?(如何设置 JFrame 大小以适合 CardLayout 显示的 JPanel?)
问题描述
我有一个 JFrame
,在 CardLayout
中包含一组 JPanels
.每个 JPanel
都有不同的大小,我希望 JFrame
适应当前显示的 JPanel
的大小(而不是 JPanel
来适应 JFrame
的大小).
I have a JFrame
containing a set of JPanels
in a CardLayout
. Each JPanel
has a different size and I want the JFrame
to adapt to the size of the currently displayed JPanel
(not the JPanel
to adapt to the size of the JFrame
).
我怎样才能做到这一点?
How can I achieve this?
推荐答案
一般是:如果你有布局问题,总是用合适的 LayoutManager 解决.切勿调整组件的大小提示以达到您的目标.
The general is: if you have a layout problem, always solve it with an appropriate LayoutManager. Never tweak a component's sizing hint to reach your goal.
在这种情况下,调整 CardLayout 尤其容易.默认情况下,它会将其 prefSize 计算为所有卡的 prefSize 的最大值.只需子类化并实现以返回当前可见卡片的 prefSize(加上 insets):
In this case, it's particularly easy to adjust the CardLayout. By default, it calculates its prefSize to the max of prefSizes of all cards. Simply subclass and implement to return the prefSize (plus insets) of the currently visible card:
public static class MyCardLayout extends CardLayout {
@Override
public Dimension preferredLayoutSize(Container parent) {
Component current = findCurrentComponent(parent);
if (current != null) {
Insets insets = parent.getInsets();
Dimension pref = current.getPreferredSize();
pref.width += insets.left + insets.right;
pref.height += insets.top + insets.bottom;
return pref;
}
return super.preferredLayoutSize(parent);
}
public Component findCurrentComponent(Container parent) {
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
return comp;
}
}
return null;
}
}
使用它(借用@mKorbel 的示例),主要方法干净利落地缩小:
Using that (borrowing @mKorbel's example), the main method cleanly shrinks down:
private static void createAndShowUI() {
final CardLayout cardLayout = new MyCardLayout();
final JPanel cardHolder = new JPanel(cardLayout);
final JFrame frame = new JFrame("MultiSizedPanels");
JLabel[] labels = {
new JLabel("Small Label", SwingConstants.CENTER),
new JLabel("Medium Label", SwingConstants.CENTER),
new JLabel("Large Label", SwingConstants.CENTER)};
for (int i = 0; i < labels.length; i++) {
int padding = 50 * (i + 1);
Border lineBorder = BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.blue),
BorderFactory.createEmptyBorder(padding, padding, padding, padding));
labels[i].setBorder(lineBorder);
JPanel containerPanel = new JPanel();
containerPanel.add(labels[i]);
cardHolder.add(containerPanel, String.valueOf(i));
}
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(cardHolder);
frame.pack();
}
});
JPanel btnHolder = new JPanel();
btnHolder.add(nextButton);
frame.add(cardHolder, BorderLayout.CENTER);
frame.add(btnHolder, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
这篇关于如何设置 JFrame 大小以适合 CardLayout 显示的 JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何设置 JFrame 大小以适合 CardLayout 显示的 JPanel?


基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01