|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Jul 2008
Messaggi: 43
|
[java] Maledetti LayoutManager
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.
Codice:
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();
}
}
|
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
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): Codice:
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);
}
}
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 13-05-2010 alle 13:35. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 05:08.




















