1

对于这么愚蠢的问题,我很抱歉,但我在将 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

在此处输入图像描述

最后,当我向下滚动时,如何使背景图像保持在同一位置,但我计划添加的文本和其他图像移动?

4

2 回答 2

3
contentPane.setIcon(/imageFolder/warlordsOrganizerBackground.png); // ????
  • 您必须将 Icon 对象传递给您的setIcon(...)方法。我不确定你在那里传递了什么,但它看起来像一个没有引号的字符串。
  • 您正在向容器 contentPane 添加组件,然后迅速将这个容器换成另一个容器。这将使您不会看到任何添加到 lame-duck 组件(旧的 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();
         }
      });
   }
} 

显示为:

在此处输入图像描述

于 2013-04-30T01:12:48.893 回答
3

有关如何加载图像的示例,请参阅如何使用图标

如果您需要使用缩放图像作为背景,那么您可以使用Background Panel。它将在何时为您设置滚动窗格和视口不透明。

于 2013-04-30T01:34:27.450 回答