Come aggiungo lo spazio a un JPanel, in modo che Jscrollepane non si siede in cima ai miei componenti?
-
15-11-2019 - |
Domanda
Ho un JScrollPane e quando carico la mia applicazione la barra è seduta in cima a uno dei miei pulsanti.Quello che vorrei fare è aggiungere uno spazio sul lato del mio pulsante in modo che la barra di scorrimento attira lo spazio e non il mio pulsante.
Codice di esempio che ho provato:
JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
.
Il problema con questo codice è che sovrascrive ancora il mio pulsante e non viene aggiunto spazio.Qual è il modo migliore per assicurarsi che JScrollPane non sovrapponi i componenti nel mio JFrame?
Grazie
Soluzione
To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().
Altri suggerimenti
In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:
eButton.add(editButton, BorderLayout.WEST);
eButton.add(spaceFiller, BorderLayout.EAST);
That would make more sense, sicne the scrollbar will appear on the right side (EAST).
Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.