调整图标大小以正确适合 ScrollPane 角按钮

发布于 2024-08-15 16:10:51 字数 888 浏览 13 评论 0原文

我想将 java 的默认 QuestionIcon 添加到滚动窗格中的角按钮,但是图像不正确(因为按钮非常小)。我应该怎么做才能使图标正确显示?

这是代码,它有效,所以你可以尝试一下并亲眼看看问题:)

import javax.swing.*;

public class CornerButton extends JFrame 
{
    public CornerButton()
    {

        JTextArea area = new JTextArea(20, 20);
        JScrollPane scroll = new JScrollPane(area);
        JButton btn = new JButton(UIManager.getIcon("OptionPane.questionIcon"));
        scroll.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, btn);
        this.add(scroll);
    }

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        CornerButton mainFrame = new CornerButton();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        mainFrame.setSize(200,200);
    }
}

NB。我在 Sun 的论坛上发布了同样的问题,但没有人回答,希望我能从这里找到一些帮助:)

提前致谢。

I would like to add java's default questionIcon to a corner button in a Scrollpane, however the image doesn't fit correctly (since the button is very small). What should i do to make the icon fit correctly?

Here is the code, It works, so you can try it out and see for yourselfs the problem :)

import javax.swing.*;

public class CornerButton extends JFrame 
{
    public CornerButton()
    {

        JTextArea area = new JTextArea(20, 20);
        JScrollPane scroll = new JScrollPane(area);
        JButton btn = new JButton(UIManager.getIcon("OptionPane.questionIcon"));
        scroll.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, btn);
        this.add(scroll);
    }

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        CornerButton mainFrame = new CornerButton();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        mainFrame.setSize(200,200);
    }
}

NB. I posted the same question on Sun's forums but no one answered, hope I find some help from here :)

thanks in advance.

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

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

发布评论

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

评论(2

指尖上得阳光 2024-08-22 16:10:51

如果您不想破坏外观和感觉,您可以调整图标图像的大小并创建一个新图标,如下所示:

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

public class CornerButton extends JFrame
{
    public CornerButton()
    {
        JTextArea area = new JTextArea(20, 20);
        JScrollPane scroll = new JScrollPane(area);
        Icon icn = UIManager.getIcon("OptionPane.questionIcon");
        int neededWidth = scroll.getVerticalScrollBar().getPreferredSize().width;
        int neededHeight = scroll.getHorizontalScrollBar().getPreferredSize().height;
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(neededWidth, neededHeight, Image.SCALE_AREA_AVERAGING));
        JButton smallBtn = new JButton(icon);
        scroll.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, smallBtn);
        this.add(scroll);
    }

    public static void main(String[] args) 
    {
        CornerButton mainFrame = new CornerButton();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        mainFrame.setSize(200,200);
    }
}

它看起来也不错。

If you would prefer not to violate the look and feel, you might just resize the icon image and create a new icon like this:

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

public class CornerButton extends JFrame
{
    public CornerButton()
    {
        JTextArea area = new JTextArea(20, 20);
        JScrollPane scroll = new JScrollPane(area);
        Icon icn = UIManager.getIcon("OptionPane.questionIcon");
        int neededWidth = scroll.getVerticalScrollBar().getPreferredSize().width;
        int neededHeight = scroll.getHorizontalScrollBar().getPreferredSize().height;
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(neededWidth, neededHeight, Image.SCALE_AREA_AVERAGING));
        JButton smallBtn = new JButton(icon);
        scroll.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, smallBtn);
        this.add(scroll);
    }

    public static void main(String[] args) 
    {
        CornerButton mainFrame = new CornerButton();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        mainFrame.setSize(200,200);
    }
}

It looks pretty good too.

梦在夏天 2024-08-22 16:10:51

您可以尝试增加 JScrollBars 的宽度,以便按钮有更多的显示空间。例如:

scroll.getVerticalScrollBar().setPreferredSize(new Dimension(30, 30));
scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(30, 30));

You could try increasing the width of the JScrollBars so that the button has more room to be displayed. For example:

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