0

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

如何缩放 GUI?

4

1 回答 1

4

你能帮我一些简短的代码吗,比如当按下最大化按钮时如何调整工具栏的大小..

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

可调整大小的工具栏

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

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

于 2012-06-14T15:38:35.493 回答