Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note, the default layout of <code>JPanel</code> is <code>FlowLayout</code>; a <code>GridBagLayout</code> with default constraints is centered in the frame's <code>BorderLayout.CENTER</code>. Also, <code>pack()</code> the enclosing <code>Window</code> and display it last. Using <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html" rel="nofollow noreferrer"><em>Initial Threads</em></a> left as an exercise.</p> <p>Addendum: A useful trick for solving layout problems is setting the background color of an enclosing container to a contrasting color, for example</p> <pre><code>jpB2.setBackground(Color.blue); </code></pre> <p><img src="https://i.stack.imgur.com/SCmj1.png" alt="image"></p> <pre><code>import java.awt.BorderLayout; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Bouton2 { public static void main(String[] args) { final int LARGEUR = 400; final int HAUTEUR = 300; JFrame jf = new JFrame(); JPanel jp = new JPanel(); JPanel jpB1 = new JPanel(); JPanel jpB2 = new JPanel(new GridBagLayout()); JPanel jpB3 = new JPanel(); JButton jb1 = new JButton("Cliquez ici"); JButton jb2 = new JButton("Je compte"); JButton jb3 = new JButton("J'agrandis"); JLabel jl = new JLabel("0 clic"); jp.setLayout(new BorderLayout()); jpB1.add(jb1); jpB2.add(jb2); jpB3.add(jb3); jp.add(jpB1, BorderLayout.NORTH); jp.add(jpB2, BorderLayout.CENTER); jp.add(jpB3, BorderLayout.SOUTH); jf.setTitle("Fenêtre Bouton2"); jf.setContentPane(jp); jf.pack(); jf.setSize(LARGEUR, HAUTEUR); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setLocationRelativeTo(null); jf.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload