Salve sto cercando di creare un form ,che utilizzi due layer diversi ,per farlo procedo in questo modo creo prima il jframe poi uso come layout manager del jframe , il gridbagconstraints,poi inserisco due panel in ogni cella del frame . Ma niente non funziona !! sapreste aiutarmi ? vi allego il codice.
ps nel primo pannello deve esserci una jcombo box nel secondo un jbutton.
codice:public class Form extends JFrame { private JComboBox jCombo; private JFormattedTextField jFormat; private JButton jButton; private JPanel jPanel1; private JPanel jPanel2; public void initComponents() { //SI SUPPONE CHE IL FRAME SIA UNA MATRICE 2 RIGHE E UNA COLONNA //IN OGNI CELLA METTO UN JPANEL IN MODO DA SFRUTTARE PER OGNI JPANEL DIVERSI //LAYOUT MANAGER this.setSize(500,400);//DIMENSIONE JFRAME GridBagLayout grid=new GridBagLayout();//LAYOUT JFRAME GridBagConstraints c=new GridBagConstraints(); c.gridx=0; c.gridy=0; c.gridwidth=GridBagConstraints.REMAINDER;//DOPO IL PRIMO PANNELLO VADO A CAPO PER INSERIRE IL SECONDO //nella seconda cella che si trova virtualmente sotto la prima c.fill=GridBagConstraints.BOTH;//RIEMPIMENTO DELLA CELLA jPanel1=new JPanel(); jPanel2=new JPanel(); //INSERISCO IL PRIMO PANNELLO NELLA PRIMA CELLA DELLA GRIGLIA Dimension d=new Dimension(this.getWidth(), (int) (0.6*400));//DIMENSIONE DEL PRIMO PANNELLO //LA DIMENSIONE SERVE PER DARE DIMENSIONE ALLA CELLA jPanel1.setPreferredSize(d); grid.setConstraints(jPanel1, c);//ABILITO I CONSTRAINTS this.add(jPanel1);//INSERISCO L'ELEMENTO NEL FRAME jCombo=new JComboBox(); jPanel1.setLayout(new FlowLayout());//AL PRIMO PANNELLO DO UN FLOWLAYOUT jPanel1.add(jCombo,FlowLayout.LEFT);//INSERISCO LA JCOMBOBOX //DIMENSIONI SECONDO PANNELLO d.width=this.getWidth(); d.height=this.getHeight()-jPanel1.getHeight(); c.gridwidth=1; c.gridy=1; jPanel2.setPreferredSize(d); grid.setConstraints(jPanel2, c); this.add(jPanel2);//INSERIMENTO NELLA GRIGLIA SECONDO PANNELLO jButton=new JButton("Ok"); jPanel2.add(jButton);//INSERIMENTO BOTTONE //DETERMINO POSIZIONE BOTTONE jButton.setLocation(jPanel2.getWidth()/2, jPanel1.getHeight()+jPanel2.getHeight()/2); this.setVisible(true); // this.pack(); } }

Rispondi quotando