|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
[JAVA] BorderLayout, Box e JTextField
Salve, ho un pannello con layout BorderLayout. A south ho un JButton, poco importa. Al centro ho un Box.createVerticalBox() che a sua volta contiene:
JTextField Box.createGlue() JTextField Il problema è che i due JTextField non vengono distanziati affatto, anzi vengono allargati dividendosi a metà lo spazio. Cosa che con i JButton per esempio non avviene. Come risolvo? |
![]() |
![]() |
![]() |
#2 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
Qualcosa del genere insomma...
Codice:
import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Prova { public static void ok() { JFrame frame = new JFrame(); frame.setSize(new Dimension(400, 600)); frame.setLayout(new BorderLayout()); Box south = Box.createHorizontalBox(); south.add(Box.createGlue()); south.add(new JButton("CIAO")); frame.add(south, BorderLayout.SOUTH); Box center = Box.createVerticalBox(); center.add(Box.createGlue()); JButton button = new JButton("OK"); button.setLayout(new BoxLayout(button, BoxLayout.LINE_AXIS)); center.add(button); frame.add(center, BorderLayout.CENTER); frame.setVisible(true); } public static void wrong() { JFrame frame = new JFrame(); frame.setSize(new Dimension(400, 600)); frame.setLayout(new BorderLayout()); Box south = Box.createHorizontalBox(); south.add(Box.createGlue()); south.add(new JButton("CIAO")); frame.add(south, BorderLayout.SOUTH); Box center = Box.createVerticalBox(); center.add(Box.createGlue()); JTextField field = new JTextField(); field.setLayout(new BoxLayout(field, BoxLayout.LINE_AXIS)); center.add(field); frame.add(center, BorderLayout.CENTER); frame.setVisible(true); } static public void main(String[] args) { ok(); wrong(); } } |
![]() |
![]() |
![]() |
#4 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
Niente, sembra che JTextField se ne freghi delle impostazioni e si spalmi sembre su tutto lo spazio disponibile.
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Incapsulandolo in un JPanel invece? Vabbè, siamo alla frutta
![]() |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Oct 2004
Città: Torino
Messaggi: 3054
|
Hai provato ad usare il costruttore del JTextField passandogli un intero?
JTextField field = new JTextField(10); per esempio. Così dovresti regolare la dimensione, non ho capito se hai esigenze sulla posizione...
__________________
Asus M4A785TD-V EVO||AMD Phenom 955 BE+Scythe Mugen 2||Corsair XMS3 2x2GB||Seasonic M12 600w||ATI HD4890+Accelero Twin Turbo Ho trattato con 62 utenti affidabili. ![]() ![]() Ciao ![]() |
![]() |
![]() |
![]() |
#7 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
Fermi tutti ho capito. Box (e BoxLayout) tendono a spalmare gli oggetti su tutta la superficie disponibile tenendo conto però della dimensione massima degli oggetti. JButton ha una dimensione massima standard mentre JTextField ha dimensione massima +infinto
![]() In termini di codice dovrebbe essere qualcosa del genere: Codice:
import java.awt.Dimension; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Prova { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(new Dimension(300, 500)); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS)); frame.getContentPane().add(new JButton("OK")); JTextField field = new JTextField(); field.setMaximumSize(new Dimension(500, 20)); frame.getContentPane().add(field); frame.setVisible(true); } } |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Bene, ho imparato qualcosa di nuovo
![]() Fin'ora non ho mai avuto bisogno di gestirmi i dettagli di layout dell'interfaccia grafica a questo livello, ho sempre interagito tramite Matisse (editor grafico dell'IDE NeatBeans) e mi è sempre andata bene ![]() |
![]() |
![]() |
![]() |
#9 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
Io matisse l'ho abbandonato, il codice non è personalizzabile totalmente e questa cosa non mi piace proprio.
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
|
![]() |
![]() |
![]() |
#11 |
Bannato
Iscritto dal: Feb 2003
Città: Anche Chuck Norris usa Debian e Gnome
Messaggi: 1270
|
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:10.