PDA

View Full Version : [java] Maledetti LayoutManager


Dolcezeus
13-05-2010, 08:59
cari amici devo sviluppare in java un gioco che stia in un JFrame a tutto schermo.. ma ho problemi con il layout manager del JPanel che ho piazzato all'interno.

private void initGUI() {
try {
getContentPane().setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setFocusTraversalPolicyProvider(true);
{
background = new JPanel();
background.setBackground(Color.blue);
background.setLayout(new BorderLayout());
background.setVisible(true);
getContentPane().add(background);
}
{
System.out.println(background.getSize().toString());
btStart = new JButton("Start");
btStart.setVisible(true);
btStart.setSize(200, 120);
btStart.setMaximumSize(new Dimension (200,120));
btStart.setMinimumSize(new Dimension (200,120));
btStart.setIgnoreRepaint(true);
background.add(btStart);
}

} catch (Exception e) {
e.printStackTrace();
}

}

il problema è che il bottone start viene massimizzato mentre dovrebbe restare delle dimensioni richieste. ho provato anche a mettere il layoout manager a null e centrare il bottone prelevando le dimensioni del JPanel ma il comando background.getSize(); mi ritorna 0 0 come fare?

banryu79
13-05-2010, 12:29
il problema è che il bottone start viene massimizzato mentre dovrebbe restare delle dimensioni richieste. ho provato anche a mettere il layoout manager a null e centrare il bottone prelevando le dimensioni del JPanel ma il comando background.getSize(); mi ritorna 0 0 come fare?

Un componente infilato nella posizione nota come "CENTER" in un BorderLayout tende ad occupare tutto lo spazio disponibile (infatti il tuo JPanel colorato di blu, che viene infilato implicitamente in quella posizione si espande fino a riempire tutta l'area disponibile).

Dato che hai settato BorderLayout come layout manager del pannello, e poi ci infili il bottone (se durante la add() non specifichi esplicitamente una posizione, di default è CENTER), quest'ultimo si espande.

Potresti usare la classe Box, che è simile a JPanel (cioè è un contenitore generico per altri componenti) ma ha delle politiche di layouting diverse, ecco un esempio che fa quello che desideri (tiene al centro il bottone senza modificarne le dimensioni):

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;


public class CenteredButton extends JFrame
{
public static void main(String[] args) {
JFrame frame = new CenteredButton();

JButton btStart = new JButton("Start");
{
Dimension size = new Dimension(200,120);
btStart.setPreferredSize(size); // width
btStart.setMaximumSize(size); // height
btStart.setIgnoreRepaint(true);
}

Box background = Box.createHorizontalBox();
{
background.setBackground(Color.BLUE);
background.add(Box.createHorizontalGlue());
background.add(btStart);
background.add(Box.createHorizontalGlue());
}

frame.add(background);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}