很抱歉提出这样一个愚蠢的问题,但我在用 Java 将背景图像设置为固定图像时遇到了一些问题。这就是我的意思 -
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame{
public static void createGUI(){
JFrame frame = new JFrame("Warlords Organizer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(1280,720));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
JLabel contentPane = new JLabel();
contentPane.setIcon(/imageFolder/warlordsOrganizerBackground.png);
contentPane.setLayout(new BorderLayout());
frame.setContentPane(contentPane);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
} // public static void main(String[] args) Closing
} // public class MainFrame Closing
所以我可以获得框架以及类似的一切,但我似乎无法加载图像。我将图像放在名为 imageFolder 的源文件夹中 - http://i.imgur.com/LWqQ6JU.png
最后,当我向下滚动时,如何将背景图像设置到某个位置,图像停留在同一个位置,但我计划添加的文本和其他图像移动了?
最佳答案
contentPane.setIcon(/imageFolder/warlordsOrganizerBackground.png); // ????
- 您必须将一个 Icon 对象传递给您的
setIcon(...)
方法。我不确定你在那里传递的是什么,但它看起来像一个不带引号的字符串。 - 您要将组件添加到容器 contentPane,然后立即将此容器换成另一个。这将使您不会看到任何添加到跛脚鸭组件(旧 contentPane)的组件。
- 解决方案很明显:向最终显示的 contentPane 添加组件。
- 还要确保为 JLabel 提供一个合适的布局管理器。
- 要在图像上滚动,请在 JLabel/contentPane 上添加一个 JScrollPane,但要使 JScrollPane 及其 JViewPort 不透明。
例如:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MainFrame {
private static final String IMAGE_PATH = "imgFolder/ham-hamster.jpg";
public static void createGUI() {
JFrame frame = new JFrame("Fubars");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File imageFile = new File(IMAGE_PATH);
try {
JTextArea textArea = new JTextArea(5, 40);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
for (int i = 0; i < 30; i++) {
textArea.append("foo bars rule the world!\n");
}
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, 20));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setOpaque(false);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
BufferedImage backgroundImg = ImageIO.read(imageFile);
Icon backgroundIcon = new ImageIcon(backgroundImg);
JLabel contentLabel = new JLabel(backgroundIcon);
contentLabel.setLayout(new BorderLayout());
contentLabel.add(scrollPane, BorderLayout.CENTER);
frame.setContentPane(contentLabel);
frame.pack();
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
}
显示为:
关于java - (3 部分)固定背景,其他元素滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290673/