简体   繁体   English

如何将图像放入表单中?

[英]How can I put an image in my form?

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). 我想将图像放在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);
}

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? 另外,由于我对Java的图形不是很熟悉,所以谁能告诉我如何控制窗口大小调整事件,以便重绘图片? 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. edit1:好的形式就是我的jFrame。 The iconGraphLabel is the jLabel I'm putting the image in. I'll try to explain the hierarchy of the parent components. iconGraphLabel是我要放入图像的jLabel。我将尝试解释父组件的层次结构。

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

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. 正确,一个JLabel或使用图标的任何Swing组件都将按其实际大小绘制图标。 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). Background Panel类提供了用于显示图像的不同选项(您可以仅使用Icon,getImage()方法)。 You should also read the section from the Swing tutorial on Custom Painting to better understand how the above code works. 您还应该阅读Swing教程中有关Custom Paint的部分,以更好地理解上述代码的工作原理。

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. iconGraphPanel是常用的jPanel,用作占位符。 It needs to be set to BorderLayout. 它需要设置为BorderLayout。 The BackgroundPanel class can be found here . 您可以在此处找到BackgroundPanel类。 The removeAll() is needed so the old image disappears. 需要removeAll(),以便旧图像消失。 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. 需要revalidate()方法,因为我们创建了一个新面板,因此需要刷新。

This is mostly camickr work and some other guy from sun forum named Maxideon. 这主要是camickr的工作,还有来自太阳论坛的其他人,名为Maxideon。 I'm just posting for future reference. 我只是发布以供将来参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM