
Originariamente inviata da
linux_r
Si manca, forse non ho copiato bene, ma nel codice c'è. comunque non riesco a ottenere l'effetto desiderato nel senso che è come se i pannelli non riempissero ognuno la propria cella. Infatti il bottone e la jcombobox risultano attaccati. XD
Ho risolto cosi :
codice:
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Linux
*/
public class FormCercaCliente 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
String [] values={"ID","Name","Surname","ZipCode","Email","Score"};
this.setSize(200,200);//DIMENSIONE JFRAME
GridBagLayout grid=new GridBagLayout();//LAYOUT JFRAME
GridBagConstraints c=new GridBagConstraints();
this.setLayout(grid);
c.gridx=0;
c.gridy=0;
c.insets.top=0;
c.insets.left=0;
c.ipadx=this.getWidth();
c.ipady=(int)(0.9*this.getHeight());
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
//LA DIMENSIONE SERVE PER DARE DIMENSIONE ALLA CELLA
grid.setConstraints(jPanel1, c);//ABILITO I CONSTRAINTS
this.add(jPanel1);//INSERISCO L'ELEMENTO NEL FRAME
jCombo=new JComboBox(values);
jFormat=new JFormattedTextField();
jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));//AL PRIMO PANNELLO DO UN FLOWLAYOUT
jPanel1.add(new JLabel("Search for "));
jPanel1.add(jCombo);//INSERISCO LA JCOMBOBOX
jPanel1.add(jFormat);
jFormat.setText(null);
jFormat.setEditable(true);
jFormat.setSize(250,39);
//DIMENSIONI SECONDO PANNELLO
c.gridwidth=1;
c.gridy=1;
c.ipady=this.getHeight()-c.ipady;
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();
this.setResizable(true);
}
}
Il problema adesso è che quando ridimensiono la finestra il bottone non viene spostato e inoltre la JFormattedTextField non viene visualizzata.