0

我正在JTabbedPane添加JFrame.

每个选项卡都包含一个不同的bgPanelJPanel以绘制的图像为背景),每个选项卡都bgPanel包含JScrollPane,并且每个选项卡都JScrollPane包含一个JPane( nowyPanel)。

bgPanels只是一个背景。每个JScrollPane都有setOpaque(false);getViewport().setOpaque(false);。每个JPanel nowyPanel只有一个setOpaque(false);

我可以看到背景,JScrollPanels但没有JPanels。我检查了一下,似乎内容已正确添加到那里。问题在于JPanels哪个没有出现。

这是一些代码:

static class JBackgroundPanel extends JPanel {

    private BufferedImage img;

    JBackgroundPanel() {
        try {
            img = ImageIO.read(new File("2.jpg"));
        } catch (IOException e) {
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
}

//method to add components to element of array nowyPanel[] JPanel
//i[a] and o are set earlier and they are fine
static void skrotyDoPaneli() {
    int a, b;
    for (a = 0; a < o; a++) {
        for (b = 0; b < i[a]; b++) {
            nowyPanel[a].add(new dodanieIkony(ikonki[a][b], podpisyIkonek[a][b], listaOdnosnikow[a][b]));
            nowyPanel[a].repaint();
            nowyPanel[a].revalidate();
        }
    }
}

//set properties for all panels 
static void ladowaniePaneli() {
    int b;
    Dimension osiemsetnaszescset = new Dimension(800, 600);
    for (b = 0; b < o; b++) {
        bgPanel[b] = new JBackgroundPanel();
        vertical[b] = new JScrollPane();
        nowyPanel[b] = new JPanel();

        ((FlowLayout) bgPanel[b].getLayout()).setVgap(0);
        bgPanel[b].setPreferredSize(osiemsetnaszescset);
        nowyPanel[b].setPreferredSize(osiemsetnaszescset);
        vertical[b].setPreferredSize(new Dimension(789, 517));
        vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        vertical[b].setOpaque(false);
        nowyPanel[b].setOpaque(false);
        vertical[b].getViewport().setOpaque(false);
        vertical[b].add(nowyPanel[b]);
        bgPanel[b].add(vertical[b]);
        vertical[b].add(nowyPanel[b]);
    }
}
//each element of bgPanel array is set as individual tab
static JTabbedPane tabbedPane() {
    JTabbedPane panelZakladek = new JTabbedPane();
    int j;
    for (j = 0; j < o; j++) {
        panelZakladek.addTab(nazwyGrup[j], bgPanel[j]);
    }
    return panelZakladek;
}

//does everything what needs to be done before JFrame shows up
static void przedStartem() throws FileNotFoundException, IOException {
    odczyt();
    odczytPaneli();
    ladowaniePaneli();
    skrotyDoPaneli();
}

//does przedStartem() and then initialize JFrame with content
public static void main(String[] args) throws FileNotFoundException, IOException {
    przedStartem();
    //przywolanie do zycia okna glownego JFrame
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            glowneOkno okno = new glowneOkno();
            okno.setVisible(true);
        }
    });
}
//Variables Declaration
static int o = 0;
static int[] i = new int[1000];
static JBackgroundPanel[] bgPanel = new JBackgroundPanel[1000];
static JScrollPane[] vertical = new JScrollPane[1000];
static JPanel[] nowyPanel = new JPanel[1000];
static FileSystemView fsv = FileSystemView.getFileSystemView();
static String[] nazwyGrup = new String[1000];
static File plikZGrupami = new File("grupy.txt");
static String[][] podpisyIkonek = new String[1000][1000];
static Icon[][] ikonki = new Icon[1000][1000];
static String[][] listaOdnosnikow = new String[1000][1000];
static File[][] listaPlikow = new File[1000][1000];
4

2 回答 2

3

我首先想到的是您没有更改 的默认布局管理器JPanel,即FlowLayout...尝试将JBackgroundPanel的布局管理器更改为类似的东西BorderLayout

你应该避免使用setPreferredSize. 问题是它可以被程序的其他部分改变。相反,您应该重写该getPreferredSize方法,这实际上应该在JBackgroundPanel类中完成。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestBackgroundPane {

    public static void main(String[] args) {
        new TestBackgroundPane();
    }

    public TestBackgroundPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JPanel top = new JPanel(new GridBagLayout());
            top.setOpaque(false);
            top.add(new JLabel("Look ma, no hands"));
            JScrollPane sp = new JScrollPane();
            sp.setOpaque(false);
            sp.getViewport().setOpaque(false);
            sp.setViewportView(top);
            add(sp);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.drawLine(0, 0, getWidth(), getHeight());
            g2d.drawLine(getWidth(), 0, 0, getHeight());
            g2d.dispose();
        }
    }
}
于 2013-05-28T00:30:15.790 回答
3
bgPanel[b].add(vertical[b]);
vertical[b].add(nowyPanel[b]);

不要将组件添加到滚动窗格。相反,代码应该是:

vertical[b].setViewportView(nowyPanel[b]);
于 2013-05-28T00:33:33.567 回答