Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Problème affichage !!!

2 réponses
Avatar
Chris
Bonjour

J'ai un JPanel avec le layout FlowLayout, et ainsi j'ajoute des
composants dans ce panel.
Par conséquent, lorsqu'il y a plus de places pour ajouter le composant
il l'ajoute à la ligne et ainsi de suite. C'est le principe du
FlowLayout avec l'option FlowLayout.LEFT.
Seulement, j'ajoute un composant qui est un JTabbedPane et qui lui
contient des pannels avec le même layout et là les composants ne vont
pas à la ligne !!!
Où est le problème ?

J'ai vraiment tout essayé et j'aimerais avoir la solution à ce problème
et le code que je dois remplacé pour que ça marche ...
De plus, je ne veux pas mettre des tailles en durs car je veux pouvoir
redimensionner la taille de la fenêtre ...

Merci d'avance pour votre réponse ... car je vois vraiment pas comment
je pourrais coder cela pour que ça marche ...

Voici le code :

pour compiler : javac ExempleTest.java
pour exécuter : java ExempleTest

Code:

import javax.swing.*;
import java.awt.*;

class MyTabbedPane
extends JTabbedPane {

public static MyTabbedPane tabbedPane = new MyTabbedPane ();

public static int nb = 0;

private static final int TEXT_NB = 100;
private static final int PANE_NB = 3;

private MyTabbedPane () {
super (JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

for(int i=0; i<PANE_NB; i++)
addPaneText();
}

public void addPaneText () {
//center interne passe pas ...
JPanel panel = new JPanel ();
FlowLayout leftLayout = new FlowLayout ();
//panel.setLayout(Layout.layout);
panel.setLayout(leftLayout);

for(int i=0; i<TEXT_NB; i++) {
panel.add(new JTextField("Text!" + nb +"!" + i));
}

add("pane_" + nb, panel);
nb++;
}
}

class Layout
extends FlowLayout {

public static Layout layout = new Layout ();

private Layout () {
super(Layout.LEFT);
}
}

class CenterPanel
extends JPanel {

public static CenterPanel centerPanel = new CenterPanel ();

private static final int NB_FIELD = 100;
private static final int NUM_TABBED = 30;


private CenterPanel () {
setLayout(Layout.layout);

for(int i=0; i<NB_FIELD; i++) {
if(i == NUM_TABBED) {
// CENTER déjà essayé
add(MyTabbedPane.tabbedPane);
}
else
add(new JTextField("center_" + i));
}
}
}

class GeneralPanel
extends JPanel {
public static GeneralPanel generalPanel = new GeneralPanel ();

private GeneralPanel () {
BorderLayout layout = new BorderLayout ();
setLayout(layout);

add(new JTextField("WEST"), BorderLayout.WEST);
add(new JTextField("NORTH"), BorderLayout.NORTH);
add(CenterPanel.centerPanel, BorderLayout.CENTER);
}
}

class MyFrame
extends JFrame
{
public static MyFrame frame = new MyFrame ();

private static String FRAME_NAME = "Exemple";


private MyFrame () {
super(FRAME_NAME);
setContentPane(GeneralPanel.generalPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
}
}


public class ExempleTest {
public static void main(String []argv) {
MyFrame.frame.show();
}
}

2 réponses

Avatar
Anthony Goubard
Bonjour,

Le probleme vient du fait que la preferredSize de TabPanel est mal calculé
et non pas du Layout.

J'ai modifié le code source pour qu'il revienne à la ligne.

Anthony

import javax.swing.*;
import java.awt.*;

class MyTabbedPane
extends JTabbedPane {

public static MyTabbedPane tabbedPane = new MyTabbedPane ();

public static int nb = 0;

private static final int TEXT_NB = 100;
private static final int PANE_NB = 3;

private MyTabbedPane () {
super (JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

for(int i=0; i<PANE_NB; i++)
addPaneText();
}

public void addPaneText () {
//center interne passe pas ...
JPanel panel = new JPanel (new BorderLayout());
JPanel panel2 = new JPanel(new FlowLayout (FlowLayout.LEFT));

for(int i=0; i<TEXT_NB; i++) {
panel2.add(new JTextField("Text!" + nb +"!" + i));
}
panel.add(panel2, BorderLayout.CENTER);

add("pane_" + nb, panel);
nb++;
}
public Dimension getPreferredSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
// doesn't work return getParent().getPreferredSize();
}
}

class CenterPanel
extends JPanel {

public static CenterPanel centerPanel = new CenterPanel ();

private static final int NB_FIELD = 100;
private static final int NUM_TABBED = 30;


private CenterPanel () {
super(new FlowLayout(FlowLayout.LEFT));

for(int i=0; i<NB_FIELD; i++) {
if(i == NUM_TABBED) {
// CENTER déjà essayé
add(MyTabbedPane.tabbedPane);
}
else
add(new JTextField("center_" + i));
}
}
}

class GeneralPanel
extends JPanel {
public static GeneralPanel generalPanel = new GeneralPanel ();

private GeneralPanel () {
BorderLayout layout = new BorderLayout ();
setLayout(layout);

add(new JTextField("WEST"), BorderLayout.WEST);
add(new JTextField("NORTH"), BorderLayout.NORTH);
add(CenterPanel.centerPanel, BorderLayout.CENTER);
}
}

class MyFrame
extends JFrame
{
public static MyFrame frame = new MyFrame ();

private static String FRAME_NAME = "Exemple";


private MyFrame () {
super(FRAME_NAME);
setContentPane(GeneralPanel.generalPanel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
}
}


public class ExempleTest {
public static void main(String []argv) {
MyFrame.frame.show();
}
}

Chris a écrit dans le message ...
Bonjour

J'ai un JPanel avec le layout FlowLayout, et ainsi j'ajoute des
composants dans ce panel.
Par conséquent, lorsqu'il y a plus de places pour ajouter le composant
il l'ajoute à la ligne et ainsi de suite. C'est le principe du
FlowLayout avec l'option FlowLayout.LEFT.
Seulement, j'ajoute un composant qui est un JTabbedPane et qui lui
contient des pannels avec le même layout et là les composants ne vont
pas à la ligne !!!
Où est le problème ?

J'ai vraiment tout essayé et j'aimerais avoir la solution à ce problème
et le code que je dois remplacé pour que ça marche ...
De plus, je ne veux pas mettre des tailles en durs car je veux pouvoir
redimensionner la taille de la fenêtre ...

Merci d'avance pour votre réponse ... car je vois vraiment pas comment
je pourrais coder cela pour que ça marche ...

Voici le code :

pour compiler : javac ExempleTest.java
pour exécuter : java ExempleTest

Code:

import javax.swing.*;
import java.awt.*;

class MyTabbedPane
extends JTabbedPane {

public static MyTabbedPane tabbedPane = new MyTabbedPane ();

public static int nb = 0;

private static final int TEXT_NB = 100;
private static final int PANE_NB = 3;

private MyTabbedPane () {
super (JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

for(int i=0; i<PANE_NB; i++)
addPaneText();
}

public void addPaneText () {
//center interne passe pas ...
JPanel panel = new JPanel ();
FlowLayout leftLayout = new FlowLayout ();
//panel.setLayout(Layout.layout);
panel.setLayout(leftLayout);

for(int i=0; i<TEXT_NB; i++) {
panel.add(new JTextField("Text!" + nb +"!" + i));
}

add("pane_" + nb, panel);
nb++;
}
}

class Layout
extends FlowLayout {

public static Layout layout = new Layout ();

private Layout () {
super(Layout.LEFT);
}
}

class CenterPanel
extends JPanel {

public static CenterPanel centerPanel = new CenterPanel ();

private static final int NB_FIELD = 100;
private static final int NUM_TABBED = 30;


private CenterPanel () {
setLayout(Layout.layout);

for(int i=0; i<NB_FIELD; i++) {
if(i == NUM_TABBED) {
// CENTER déjà essayé
add(MyTabbedPane.tabbedPane);
}
else
add(new JTextField("center_" + i));
}
}
}

class GeneralPanel
extends JPanel {
public static GeneralPanel generalPanel = new GeneralPanel ();

private GeneralPanel () {
BorderLayout layout = new BorderLayout ();
setLayout(layout);

add(new JTextField("WEST"), BorderLayout.WEST);
add(new JTextField("NORTH"), BorderLayout.NORTH);
add(CenterPanel.centerPanel, BorderLayout.CENTER);
}
}

class MyFrame
extends JFrame
{
public static MyFrame frame = new MyFrame ();

private static String FRAME_NAME = "Exemple";


private MyFrame () {
super(FRAME_NAME);
setContentPane(GeneralPanel.generalPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
}
}


public class ExempleTest {
public static void main(String []argv) {
MyFrame.frame.show();
}
}



Avatar
Chris
Là il y a à nouveau un problème ... car dans le tabbedPane il y a bien
un retour à la ligne mais l'affichage du dernier JTextField du Tabbed et
coupé en deux !!!
De plus, l'espace réservé au Tabbed Pane est trop grand et par
conséquent on ne voit plus les JTextField qui appartenait au Panel qui
contient le tabbedPane, notamment de center-31 à center_99 !!!

As-tu ou une autre personne un autre code à me proposer car là je nage
vraiment ...


merci d'avance pour ton aide




Anthony Goubard wrote:
Bonjour,

Le probleme vient du fait que la preferredSize de TabPanel est mal calculé
et non pas du Layout.

J'ai modifié le code source pour qu'il revienne à la ligne.

Anthony

import javax.swing.*;
import java.awt.*;

class MyTabbedPane
extends JTabbedPane {

public static MyTabbedPane tabbedPane = new MyTabbedPane ();

public static int nb = 0;

private static final int TEXT_NB = 100;
private static final int PANE_NB = 3;

private MyTabbedPane () {
super (JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

for(int i=0; i<PANE_NB; i++)
addPaneText();
}

public void addPaneText () {
//center interne passe pas ...
JPanel panel = new JPanel (new BorderLayout());
JPanel panel2 = new JPanel(new FlowLayout (FlowLayout.LEFT));

for(int i=0; i<TEXT_NB; i++) {
panel2.add(new JTextField("Text!" + nb +"!" + i));
}
panel.add(panel2, BorderLayout.CENTER);

add("pane_" + nb, panel);
nb++;
}
public Dimension getPreferredSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
// doesn't work return getParent().getPreferredSize();
}
}

class CenterPanel
extends JPanel {

public static CenterPanel centerPanel = new CenterPanel ();

private static final int NB_FIELD = 100;
private static final int NUM_TABBED = 30;


private CenterPanel () {
super(new FlowLayout(FlowLayout.LEFT));

for(int i=0; i<NB_FIELD; i++) {
if(i == NUM_TABBED) {
// CENTER déjà essayé
add(MyTabbedPane.tabbedPane);
}
else
add(new JTextField("center_" + i));
}
}
}

class GeneralPanel
extends JPanel {
public static GeneralPanel generalPanel = new GeneralPanel ();

private GeneralPanel () {
BorderLayout layout = new BorderLayout ();
setLayout(layout);

add(new JTextField("WEST"), BorderLayout.WEST);
add(new JTextField("NORTH"), BorderLayout.NORTH);
add(CenterPanel.centerPanel, BorderLayout.CENTER);
}
}

class MyFrame
extends JFrame
{
public static MyFrame frame = new MyFrame ();

private static String FRAME_NAME = "Exemple";


private MyFrame () {
super(FRAME_NAME);
setContentPane(GeneralPanel.generalPanel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
}
}


public class ExempleTest {
public static void main(String []argv) {
MyFrame.frame.show();
}
}

Chris a écrit dans le message ...

Bonjour

J'ai un JPanel avec le layout FlowLayout, et ainsi j'ajoute des
composants dans ce panel.
Par conséquent, lorsqu'il y a plus de places pour ajouter le composant
il l'ajoute à la ligne et ainsi de suite. C'est le principe du
FlowLayout avec l'option FlowLayout.LEFT.
Seulement, j'ajoute un composant qui est un JTabbedPane et qui lui
contient des pannels avec le même layout et là les composants ne vont
pas à la ligne !!!
Où est le problème ?

J'ai vraiment tout essayé et j'aimerais avoir la solution à ce problème
et le code que je dois remplacé pour que ça marche ...
De plus, je ne veux pas mettre des tailles en durs car je veux pouvoir
redimensionner la taille de la fenêtre ...

Merci d'avance pour votre réponse ... car je vois vraiment pas comment
je pourrais coder cela pour que ça marche ...

Voici le code :

pour compiler : javac ExempleTest.java
pour exécuter : java ExempleTest

Code:

import javax.swing.*;
import java.awt.*;

class MyTabbedPane
extends JTabbedPane {

public static MyTabbedPane tabbedPane = new MyTabbedPane ();

public static int nb = 0;

private static final int TEXT_NB = 100;
private static final int PANE_NB = 3;

private MyTabbedPane () {
super (JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);

for(int i=0; i<PANE_NB; i++)
addPaneText();
}

public void addPaneText () {
//center interne passe pas ...
JPanel panel = new JPanel ();
FlowLayout leftLayout = new FlowLayout ();
//panel.setLayout(Layout.layout);
panel.setLayout(leftLayout);

for(int i=0; i<TEXT_NB; i++) {
panel.add(new JTextField("Text!" + nb +"!" + i));
}

add("pane_" + nb, panel);
nb++;
}
}

class Layout
extends FlowLayout {

public static Layout layout = new Layout ();

private Layout () {
super(Layout.LEFT);
}
}

class CenterPanel
extends JPanel {

public static CenterPanel centerPanel = new CenterPanel ();

private static final int NB_FIELD = 100;
private static final int NUM_TABBED = 30;


private CenterPanel () {
setLayout(Layout.layout);

for(int i=0; i<NB_FIELD; i++) {
if(i == NUM_TABBED) {
// CENTER déjà essayé
add(MyTabbedPane.tabbedPane);
}
else
add(new JTextField("center_" + i));
}
}
}

class GeneralPanel
extends JPanel {
public static GeneralPanel generalPanel = new GeneralPanel ();

private GeneralPanel () {
BorderLayout layout = new BorderLayout ();
setLayout(layout);

add(new JTextField("WEST"), BorderLayout.WEST);
add(new JTextField("NORTH"), BorderLayout.NORTH);
add(CenterPanel.centerPanel, BorderLayout.CENTER);
}
}

class MyFrame
extends JFrame
{
public static MyFrame frame = new MyFrame ();

private static String FRAME_NAME = "Exemple";


private MyFrame () {
super(FRAME_NAME);
setContentPane(GeneralPanel.generalPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
}
}


public class ExempleTest {
public static void main(String []argv) {
MyFrame.frame.show();
}
}