gpt4 book ai didi

java - 调整桌面 GUI 中组件的大小

转载 作者:行者123 更新时间:2023-12-01 19:03:57 25 4
gpt4 key购买 nike

我正在用 Java 制作一个用于数据结构的 GUI。我想要一个功能,每当用户单击表单顶部的最大化按钮时,组件和表单中的所有内容也应该随着窗口扩展而调整大小,反之亦然。我进行了很多搜索但找不到解决方案。

如何缩放 GUI?

最佳答案

Can you help me with some short code like how to resize a Toolbar when the maximize button is pressed..

我会做得更好。下面是一个简短的代码示例,其中显示了 5 个它们,它们具有不同的调整大小行为,具体取决于它们在 BorderLayout 中的放置位置。

ResizableToolBars

import java.awt.BorderLayout;
import javax.swing.*;

public class ResizableToolBars {

public static void showFrameWithToolBar(String toolBarPosition) {
// the layout is important..
JPanel gui = new JPanel(new BorderLayout());

JToolBar tb = new JToolBar();
// ..the constraint is also important
gui.add(tb, toolBarPosition);
tb.add(new JButton("Button 1"));
tb.add(new JButton("Button 2"));
tb.addSeparator();
tb.add(new JButton("Button 3"));
tb.add(new JCheckBox("Check 1", true));

JFrame f = new JFrame(toolBarPosition + " Sreeeetchable Tool Bar");
f.setContentPane(gui);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.pack();

// we don't normally set a size, this is to show where
// extra space is assigned.
f.setSize(400,120);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
showFrameWithToolBar(BorderLayout.PAGE_START);
showFrameWithToolBar(BorderLayout.PAGE_END);
showFrameWithToolBar(BorderLayout.LINE_START);
showFrameWithToolBar(BorderLayout.LINE_END);
showFrameWithToolBar(BorderLayout.CENTER);
}
});
}
}

如果您返回Nested Layout Example之后,您应该能够弄清楚我如何将较小的组件组​​组合在一起,每个组件在父容器的一个区域中都有自己的布局(在面板中)。

关于java - 调整桌面 GUI 中组件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035722/

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