I'm adding JTabbedPane
to JFrame
.
Each tab contains a different bgPanel
( JPanel
with drawn image as background), each bgPanel
contains JScrollPane
, and each JScrollPane
contains a JPane
( nowyPanel
).
bgPanels
are only a background. Each JScrollPane
has setOpaque(false);
and getViewport().setOpaque(false);
. Each JPanel
nowyPanel
has setOpaque(false);
only.
I can see the background and JScrollPanels
but no JPanels
. I Checked it and it seems that content is correctly added there. The problem is with JPanels
which doesn't show up.
Here is some code:
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];
The first thing that jumps out at me is that you've not changed the default layout manager of the JPanel
, which is FlowLayout
...Try changing the JBackgroundPanel
's layout manager to something like BorderLayout
instead.
You should avoid using setPreferredSize
. The problem with this is it can be changed by some other part of the program. Instead, you should override the getPreferredSize
method, this should really be done within JBackgroundPanel
class.
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();
}
}
}
bgPanel[b].add(vertical[b]);
vertical[b].add(nowyPanel[b]);
Don't add components to a scrollpane. Instead the code should be:
vertical[b].setViewportView(nowyPanel[b]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.