Bonjour,
Le code ci-dessous crée un frame contenant un panel dont la taille est fixée à 900X900. Ce panel contient juste un JEditorPanel. Comme j'ai appelé setLayout(null) au début, c'est à moi de gérer la taille et la localisation de JEditrorPanel par rapport à son parent. C'est pour cela que j'ai introduit la classe MyEditorPanel dont le but est seulement d'appelr setBounds pour placer JEditorPanel.
Ce que je ne comprends pas est que si j'appelles au début setLayout(new BorderLayout()) au lieu de setLayout(null), Ca marche et j'ai mon JEditorPanel au bon endroit. Par contre, si je garde l'appel setLayout(null) au début, je ne vois plus JEditorPanel sur l'écran. Je m'attendais plutôt au contraire.
Pourtant j'ai déjà utilisé cette technique pour d'autre composants que JEditorPanel (des JPanel) et ça marche avec setLayout(null).
J'ai isolé le code dans un programme simple que j'ai collé ci-dessous où j'ai toujours le même problème. J'espere que quelqu'un pourra m'aider.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 import javax.swing.*; import java.awt.*; class MyEditorPanel extends JEditorPane { protected void paintComponent(Graphics g) { this.setBounds(200,200,200,200); super.paintComponent(g); } } public class EditorPanelTest extends JPanel { public Dimension getPreferredSize() { int w = 900; int h = 900; return new Dimension(w,h); } public EditorPanelTest() { // setLayout(new BorderLayout()); setLayout(null); MyEditorPanel editorPane = createEditorPane(); add(editorPane); } private MyEditorPanel createEditorPane() { MyEditorPanel editorPane = new MyEditorPanel(); editorPane.setEditable(false); editorPane.setText("totototototootototo tototototo"+"\n"+" titititi"); return editorPane; } public static void main(String[] args) { JFrame frame = new JFrame("EditorPanelTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new EditorPanelTest(); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } }
Partager