如何将图像放入我的表单中?

发布于 2024-08-25 02:51:50 字数 1411 浏览 1 评论 0原文

我有问题。我想在 Java 中将图像放入表单中,但我不知道我是否使用了正确的技术(在网页中的某个位置找到了它)。

private void iconSelect() {
    String iconString = "";
    if (typeCombobox.getSelectedIndex() == 0) {
        iconString = "LP_";
    } else if (typeCombobox.getSelectedIndex() == 1) {
        iconString = "HP_";
    } else if (typeCombobox.getSelectedIndex() == 2) {
        iconString = "BP_";
    } else if (typeCombobox.getSelectedIndex() == 3) {
        iconString = "BS_";
    }
    if (RB_Gain_Clean.isSelected()) {
        iconString = iconString + "Clean";
    } else if (RB_Gain_dB.isSelected()) {
        iconString = iconString + "dB";
    }

    ImageIcon icon = new ImageIcon("images/" + iconString + ".jpg");
    Image img = icon.getImage();
    if (iconGraphLabel.getWidth() > 0 && iconGraphLabel.getHeight() > 0) {
        img = img.getScaledInstance(iconGraphLabel.getWidth(), iconGraphLabel.getHeight(), java.awt.Image.SCALE_SMOOTH);
    }
    icon = new ImageIcon(img);
    iconGraphLabel.setIcon(icon);
}

所以它实际上显示了图像并且正在调整大小,但是当我调整表单大小然后再次缩小它时,标签似乎没有跟随调整大小,因此它仍然比窗口大。

另外,由于我对java的图形不太熟悉,谁能告诉我如何控制窗口大小调整事件以便我重新绘制图片?现在该方法是由代码中显示的组合框和单选按钮触发的。

提前致谢!

edit1: 嗯,表单是我的 jFrame。 iconGraphLabel 是我要放入图像的 jLabel。我将尝试解释父组件的层次结构。

PlotArea [jPanel] (cardLayout) > plotArea_Image [jPanel](“cardDraw”)> iconGraphPanel [jPanel] >图标图标签

I'm having a problem. I want to put an image inside a form in Java and I don't know if I'm using a proper technique (found it somewhere in a web page).

private void iconSelect() {
    String iconString = "";
    if (typeCombobox.getSelectedIndex() == 0) {
        iconString = "LP_";
    } else if (typeCombobox.getSelectedIndex() == 1) {
        iconString = "HP_";
    } else if (typeCombobox.getSelectedIndex() == 2) {
        iconString = "BP_";
    } else if (typeCombobox.getSelectedIndex() == 3) {
        iconString = "BS_";
    }
    if (RB_Gain_Clean.isSelected()) {
        iconString = iconString + "Clean";
    } else if (RB_Gain_dB.isSelected()) {
        iconString = iconString + "dB";
    }

    ImageIcon icon = new ImageIcon("images/" + iconString + ".jpg");
    Image img = icon.getImage();
    if (iconGraphLabel.getWidth() > 0 && iconGraphLabel.getHeight() > 0) {
        img = img.getScaledInstance(iconGraphLabel.getWidth(), iconGraphLabel.getHeight(), java.awt.Image.SCALE_SMOOTH);
    }
    icon = new ImageIcon(img);
    iconGraphLabel.setIcon(icon);
}

So it actually shows the image and it is resizing but when I resize my form and then make it smaller again, the label doesn't seem to follow the resizing so it stays bigger than the window.

Also, since I'm not very familiar with java's graphics, can anyone tell me how can I control a window resizing event so I redraw the picture? Right now the method is triggered by the combobox and the radiobuttons shown in the code.

Thanks in advance!

edit1: Well the form is my jFrame. The iconGraphLabel is the jLabel I'm putting the image in. I'll try to explain the hierarchy of the parent components.

PlotArea [jPanel] (cardLayout) > plotArea_Image [jPanel] ("cardDraw") > iconGraphPanel [jPanel] > iconGraphLabel

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

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

发布评论

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

评论(2

深府石板幽径 2024-09-01 02:51:50

但是当我调整表单大小然后
再次缩小标签
似乎没有遵循调整大小所以
它比窗户大

正确,JLabel 或任何使用图标的 Swing 组件都会以其实际大小绘制图标。如果您希望图标根据可用空间进行缩放,那么您需要进行自定义绘制。

Background Panel 类提供了用于显示图像的不同选项(您可以可以只使用 Icon,getImage() 方法)。您还应该阅读 Swing 教程中关于 自定义绘画 更好地理解上面的代码是如何工作的。

but when I resize my form and then
make it smaller again, the label
doesn't seem to follow the resizing so
it stays bigger than the window

Correct, a JLabel, or any Swing component that uses Icons will paint the Icon at its actual size. If you want the Icon to scale depending on the space available then you need to do custom painting.

The Background Panel classes provides different options for displaying an image (you can just use the Icon,getImage() method). You should also read the section from the Swing tutorial on Custom Painting to better understand how the above code works.

戏蝶舞 2024-09-01 02:51:50

找到了解决方案。这是最终的代码:

private void iconSelect() {
    iconGraphPanel.removeAll();
    ImageIcon icon = new ImageIcon("image.jpg");
    BackgroundPanel imagePanel = new BackgroundPanel(icon.getImage(), BackgroundPanel.SCALED);
    iconGraphPanel.add(imagePanel);
    iconGraphPanel.revalidate();
}

iconGraphPanel 是一个常见的 jPanel,我用作占位符。需要将其设置为 BorderLayout。可以在此处找到BackgroundPanel类。需要使用removeAll(),以便旧图像消失。如果你不把这些图像开始堆积起来。不知道是否有更好的方法来做到这一点,但它对我来说效果很好。需要 revalidate() 方法,因为我们创建了一个新面板,因此需要刷新。

这主要是 camickr 和来自 sun 论坛的其他一些名叫 Maxideon 的人的工作。我只是发布供将来参考。

Found a solution. This is the final code:

private void iconSelect() {
    iconGraphPanel.removeAll();
    ImageIcon icon = new ImageIcon("image.jpg");
    BackgroundPanel imagePanel = new BackgroundPanel(icon.getImage(), BackgroundPanel.SCALED);
    iconGraphPanel.add(imagePanel);
    iconGraphPanel.revalidate();
}

iconGraphPanel is a common jPanel that I use as a place holder. It needs to be set to BorderLayout. The BackgroundPanel class can be found here. The removeAll() is needed so the old image disappears. If you don't put this images start to stack up. Don't know if there is a better way to do that but it works just fine for me. The revalidate() method is needed because we create a new panel so it needs to refresh.

This is mostly camickr work and some other guy from sun forum named Maxideon. I'm just posting for future reference.

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