2017-07-19 3 views
0

J'ai deux panneaux. Le premier ressemble à ceci.Les panneaux occupent un espace égal

public class BoardPanel extends JPanel 
{ 
    public BoardPanel() 
    { 
     setLayout(null); 
     this.setOpaque(false); 

     Button button = new JButton(".."); 
     button.setLocation(...); 
     button.setSize(...); 
     add(button); 
    } 

    public void paintComponent(Graphics g) 
    { 
     /* 
     * Painting some stuff here. 
     */ 
    } 
} 

L'autre panneau est quelque chose comme ceci:

public class OtherPanel extends JPanel 
{ 
    public OtherPanel() 
    { 
     super(); 
     this.setLayout(null); 
     this.setOpaque(false); 

     JPanel panel1 = new JPanel(); 
     panel1.setLocation(...); 
     panel1.setSize(...); 
     panel1.setOpaque(..); 

     JPanel panel2 = new JPanel(); 
     panel2.setLocation(...); 
     panel2.setSize(...); 
     panel2.setOpaque(..); 

     add(panel1): 
     add(panel2); 
    } 

} 

Après cela, je mets mes deux panneaux dans un cadre. Mais je veux que mon BoardPanel occupe plus d'écran que OtherPanel. Donc, je GridBagLayout pour le cadre

public class MainFrame extends JFrame 
{ 
    private GridBagLayout aGridLayout = new GridBagLayout(); 
    private GridBagConstraints constraints = new GridBagConstraints(); 

    public MainFrame() 
    { 
     super("Quoridor"); 
     setLayout(gridLayout); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(1366, 768); 
     setVisible(true); 
     setResizable(false); 
     this.getContentPane().setBackground(Color.decode("#b2a6a6")); 

     BoardPanel boardPanel = new BoardPanel(); 
     OtherPanel otherPanel = new OtherPanel(); 

     this.addComponent(boardPanel, 1, 1, 2, 1); 
     this.addComponent(otherPanel, 1, 3, 1, 1); 
    } 

    public void addComponent(Component component , int row , int column , int width 
      , int height) 
    { 
     constraints.gridx = column; 
     constraints.gridy = row; 
     constraints.gridwidth = width; 
     constraints.gridheight = height; 
     aGridLayout.setConstraints(component, constraints); 
     add(component); 
    } 
} 

Le problème est que le cadre donne l'égalité d'espace pour les deux panneaux, et à ne pas donner plus d'espace au boardPanel. Pourquoi cela se produit-il? Est-ce que cela a à voir avec les limites des panneaux?

Répondre

1

Voici un bon tutoriel sur GridBagLayout: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html. Voir aussi le code ci-dessous et la capture d'écran. Le champ d'ancrage positionne le composant sur la première ligne. Le champ weightx donne plus d'espace aux colonnes pour boardPanel. Le champ ipady spécifie combien ajouter à la hauteur du composant. Ici, boardPanel obtient la plus grande partie de la largeur et de toute la hauteur. Le panneau otherPanel obtient la moitié de la hauteur.

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

public class GridExample { 
    private JFrame mainFrame; 
    private JPanel boardPanel, otherPanel; 

    public GridExample(){ 
     mainFrame = new JFrame(); 
     mainFrame.setSize(600,400); 
     mainFrame.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     mainFrame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent windowEvent){ 
       System.exit(0); 
      }   
     });  

     boardPanel = new JPanel(); 
     boardPanel.add(new JLabel("board panel")); 
     boardPanel.setBackground(Color.yellow); 

     otherPanel = new JPanel(); 
     otherPanel.add(new JLabel("other panel")); 
     otherPanel.setBackground(Color.green); 

     c.anchor = GridBagConstraints.FIRST_LINE_START; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.75; 
     c.ipady = 400; 
     c.gridx = 0; 
     c.gridy = 0; 
     mainFrame.add(boardPanel, c); 

     c.anchor = GridBagConstraints.FIRST_LINE_START; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.25; 
     c.ipady = 200; 
     c.gridx = 1; 
     c.gridy = 0;  
     mainFrame.add(otherPanel, c); 
     mainFrame.setVisible(true); 
    } 

    public static void main(String[] args){ 
     GridExample swingContainerDemo = new GridExample(); 
    } 
} 

enter image description here