1

我无法更好地解释标题中的问题,所以这里是 -

我创建了一个平铺的背景图像。然后我将创建的背景图像设置为我的 JFrame。但是,我将我的 JScrollPane 添加到所述背景中。根据我放置代码的顺序,会发生两件事中的一件。当我有这样的 JScrollPane 时-

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class TestingApp {

public static JFrame programFrame;
public static JLabel projectBackground;
public static JLabel projectLogo;
public static JPanel allContent;
public static JPanel fourRows;
public static JPanel centerPanel;
public static JScrollPane scrollPane;

// Tiled Background
public static void tiledBackground() {

    @SuppressWarnings("serial")
    class ImagePanel extends JPanel {
        public Image image;
        public boolean tile;

        ImagePanel(Image image) {
            this.image = image;
            this.tile = true;
        };

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int iw = image.getWidth(this);
            int ih = image.getHeight(this);
            if (iw > 0 && ih > 0) {
                for (int x = 0; x < getWidth(); x += iw) {
                    for (int y = 0; y < getHeight(); y += ih) {
                        g.drawImage(image, x, y, iw, ih, this);
                    }
                }
            }
        }
    }
}

// Making the parts for the GUI
public static void createGUI() {
    java.net.URL img1 = null;
    try {
        img1 = new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSE6uHn-B_qKtLZOKjQNVeIxhOaxbmfio45VMUq-mVgGKvgmeghKw");
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    Image image = null;
    try {
        image = ImageIO.read(img1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // programFrame Title and Layout
    programFrame = new JFrame("Organizer");
    programFrame.setLayout(new BorderLayout());


    Icon backgroundIcon = new ImageIcon(img1);
    projectBackground = new JLabel(backgroundIcon);

    // Logo JLabel
    java.net.URL img2 = null;
    try {
        img2 = new URL("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTvXxBQRsJ5NgSb8VOSNU_Qfom6HRV_crcazhD6bSZUh_ux3VHbgQ");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    Icon logoIcon = new ImageIcon(img2);
    projectLogo = new JLabel(logoIcon);
    projectLogo.setBorder(new EmptyBorder(10, 10, 10, 10));

    // New JPanel for GridLayouts to hold each JPanel with GridLayouts
    fourRows = new JPanel(new GridLayout(1, 4));
    fourRows.setLayout(new GridLayout());
    fourRows.setOpaque(false);
    fourRows.add(new JButton("Button"));
    fourRows.add(new JButton("Button"));
    fourRows.add(new JButton("Button"));
    fourRows.add(new JButton("Button"));

    // Makes the Initial BorderLayout (Using allContent JPanel)
    allContent = new JPanel();
    allContent.setLayout(new BorderLayout());
    allContent.add(projectLogo, BorderLayout.NORTH);
    allContent.setVisible(true);
    allContent.setOpaque(false);
    allContent.add(fourRows, BorderLayout.CENTER);

    // Add ScrollPane
    scrollPane = new JScrollPane(allContent);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.getVerticalScrollBar().setUnitIncrement(10);
    scrollPane.setOpaque(false);
    scrollPane.getViewport().setOpaque(false);

    // JFrame programFrame Constructors
    programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    programFrame.setLayout(new BorderLayout());
    programFrame.setContentPane(new ImagePanel(image));
    programFrame.add(scrollPane);

    programFrame.pack();
    programFrame.setVisible(true);
    programFrame.setResizable(true);
    programFrame.setSize(1280, 720);
    programFrame.setLocationRelativeTo(null);

} // public static void createGUI() Closing

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createGUI();

        } // public void run() Closing
    });
}
}

它不会在整个 JFrame 上滚动,只是在 contentStuff 上滚动,即使这样,它也会延伸到内容的底部,直到滚动条不存在的地方,并且您无法滚动。

但是,当我重新排列该代码时,我把我的

programFrame.setContentPane(new ImagePanel(image));

programFrame.add(scrollPane);

我只是得到重复的背景图像,没有内容。

编辑 - 添加了一个 SSCCE

EDIT2 - 这是我正在尝试的解决方案。我尝试创建一个空面板来添加应根据 JFrame 大小更新的内容。不用说,它没有用。已编辑代码的小片段,没有其他任何更改 -

// Makes the Initial BorderLayout (Using allContent JPanel)
allContent = new JPanel();
allContent.setLayout(new BorderLayout());
allContent.add(warlordsLogo, BorderLayout.NORTH);
allContent.setVisible(true);
allContent.setOpaque(false);
allContent.add(fourRows, BorderLayout.CENTER);

int widthForCenterPanel = programFrame.getWidth();
int heightForCenterPanel = programFrame.getHeight();

// Makes a Panel to add the ScrollPane to to center is properly
centerPanel = new JPanel();
centerPanel.setOpaque(false);
centerPanel.setBounds(0, 0, widthForCenterPanel, heightForCenterPanel);
centerPanel.add(allContent);

// Add ScrollPane
scrollPane = new JScrollPane(centerPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));

编辑 3 - 修复了我的 SSCCE。如果您在尝试时注意到,这取决于 JPanel 的大小,而不是 JFrame。

4

0 回答 0