Domanda

I simply would like to place my JButton by the setBounds method. But whatever the parameters of this method, the button is on the middle at the top.

This is my code :

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Panneau_fenetre1A extends JPanel                      
{ 
    JButton boutonOK = new JButton ("OK");


    public Panneau_fenetre1A(int Na)
    {
        boutonOK.setBounds(300,300,30,30);
        add(boutonOK);

    }

}
È stato utile?

Soluzione

The placement of the button would also depend on the layout that you are currently using. Here's a Vusual Guide to Layout to help you out decide on the best layout for your requirement.

Altri suggerimenti

With a setLayout(null), it works !

You are extending JPanel so the default constructor of JPanel is used and the default LayoutManager is, for whatever reason, the FlowLayout ! So if you really want to use NullLayout you could add this to your class:

  public Panneau_fenetre1A()
  {
    super(null);
  }

But seriously consider other Layout managers, for example the BorderLayoutManager (super(new BorderLayout())) is a good start.

When designing your dialog, remember that you can and should nest your layouts: one JPanel inside another JPanel (e.g. a GridLayout inside a BorderLayout). Please note: a 'good' dialog should resize properly, so that if the user resizes your Frame, you want to automatically extend your information objects such as your table, and not show large areas of JPanel background. That's something you cannot achieve with a NullLayout.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top