Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [Java] Problema Form

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305

    [Java] Problema Form

    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();
         }
        
        
        
        
        
    }

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    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 ?
    Guarda, non ho molto tempo adesso ma una cosa l'ho notata: non c'è la assegnazione di 'grid' (il GridBagLayout) ad un contenitore.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    Quote Originariamente inviata da andbin Visualizza il messaggio
    Guarda, non ho molto tempo adesso ma una cosa l'ho notata: non c'è la assegnazione di 'grid' (il GridBagLayout) ad un contenitore.
    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

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    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.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.