调整工具栏图标的大小

发布于 2024-12-28 15:22:55 字数 1055 浏览 4 评论 0原文

美好的一天,

我有一个基本的工具栏,我在其中添加了 ImageIcon 按钮。然而,这些图像的大小不同。 我将如何调整图标的大小,使它们大小相同。

super("ToolBar");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //creating the icons for the toolbar
    ImageIcon savePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/save.png");
    ImageIcon openFilePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/open.png");
    ImageIcon printPic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/print.png");



    //creating buttons with initial text and icons. I.o.w. the buttons for the toolbar are created

    JButton save = new JButton("Save", savePic);
    JButton open = new JButton("Open", openFilePic);
    JButton print = new JButton("Print", printPic);


    JToolBar bar = new JToolBar();
    bar.add(save);
    bar.add(open);

    bar.add(new JToolBar.Separator());
    bar.add(print);

    JTextArea text = new JTextArea(10, 40);


    add(BorderLayout.NORTH, bar);
    add(BorderLayout.CENTER, text);

    pack();
    setVisible(true);

Good day

I have a basic toolbar to which I added ImageIcon buttons. The images are however different in size.
How would I go about in resizing the Icons so that they are all the same size.

super("ToolBar");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //creating the icons for the toolbar
    ImageIcon savePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/save.png");
    ImageIcon openFilePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/open.png");
    ImageIcon printPic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/print.png");



    //creating buttons with initial text and icons. I.o.w. the buttons for the toolbar are created

    JButton save = new JButton("Save", savePic);
    JButton open = new JButton("Open", openFilePic);
    JButton print = new JButton("Print", printPic);


    JToolBar bar = new JToolBar();
    bar.add(save);
    bar.add(open);

    bar.add(new JToolBar.Separator());
    bar.add(print);

    JTextArea text = new JTextArea(10, 40);


    add(BorderLayout.NORTH, bar);
    add(BorderLayout.CENTER, text);

    pack();
    setVisible(true);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-01-04 15:22:55

火烈鸟组件套件支持可调整大小的图标,支持类之一是 ImageWrapperResizingIcon。您可以尝试查看源代码,了解如何实现自动调整图标大小,而无需手动执行此操作。

或者,您可以自己创建图像的调整大小版本,然后使用该调整大小版本创建 ImageIcon

The flamingo component suite supports resizable icons, one of the support classes being ImageWrapperResizableIcon. You might try to have a look at the source to get an idea of how to implement automatically resizing icons without the need to manually do so.

Alternatively, just create a resized version of the image yourself and create the ImageIcon using that resized version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文