Java的。无法在JScrolledPane中添加JPanel

时间:2013-05-27 23:36:19

标签: java swing components jscrollpane jtabbedpane

我正在向JTabbedPane添加JFrame

每个标签包含不同的bgPanelJPanel,其中包含bgPanel,其中JScrollPane包含JScrollPane,每个JPane包含nowyPanel 1}}(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]; 没有出现。

以下是一些代码:

{{1}}

2 个答案:

答案 0 :(得分:3)

我突然想到的是,您没有更改JPanel的默认布局管理器,即FlowLayout ...尝试更改JBackgroundPanel布局管理器改为BorderLayout

您应该避免使用setPreferredSize。这个问题是它可以被程序的其他部分改变。相反,您应该覆盖getPreferredSize方法,这应该在JBackgroundPanel类中完成。

enter image description here

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();
        }
    }
}

答案 1 :(得分:3)

bgPanel[b].add(vertical[b]);
vertical[b].add(nowyPanel[b]);

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

vertical[b].setViewportView(nowyPanel[b]);