Visualizzazione dei risultati da 1 a 4 su 4

Discussione: (java) GridBagLayout

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883

    (java) GridBagLayout

    Volevo creare una status bar in un programma swing
    Nella status bar ci sono tre jlabel

    La prima deve occupare tutto lo spazio possibile
    La seconda e la terza devono avere larghezza fissa ad 80
    Quando l'utente ridimensiona il jframe che contiene la status bar, si dovrebbe stringere o allargare solo la prima label mentre le altre due devono rimanere di dimensione fissa

    Ho usato i seguenti parametri

    JLabel fileNameLabel = new JLabel();

    JLabel languageNameLabel = new JLabel();
    languageNameLabel.setSize(80, 20);

    JLabel lineNumberLabel = new JLabel();
    lineNumberLabel.setSize(80, 20);

    JPanel statusBarPanel = new JPanel();
    statusBarPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    statusBarPanel.add(fileNameLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(languageNameLabel, c);

    c.gridx = 2;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(lineNumberLabel, c);

    Ma non funziona, sapete aiutarmi?

  2. #2
    specifica un po meglio cosa significa che non funziona....che risultato ottieni col tuo codice?
    0m4r
    http://omar.adobati.it

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    ho riscritto un codice compilante
    Con non funziona intendo dire i due campi di tipo label ai quali ho settato la dimensione a 80 non rimangono di dimensione 80 quando aumento o diminuisco la dimensione del JFrame

    import javax.swing.*;
    import java.awt.*;

    public class Layout extends JFrame{
    public Layout() {
    JLabel fileNameLabel = new JLabel("Ciao");

    JLabel languageNameLabel = new JLabel("Cane");
    languageNameLabel.setSize(80, 20);

    JLabel lineNumberLabel = new JLabel("Ciao");
    lineNumberLabel.setSize(80, 20);

    JPanel statusBarPanel = new JPanel();
    statusBarPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    statusBarPanel.add(fileNameLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(languageNameLabel, c);

    c.gridx = 2;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(lineNumberLabel, c);

    getContentPane().add(statusBarPanel);
    this.setSize(600, 800);
    this.setVisible(true);
    }
    public static void main (String[] args) {
    new Layout();
    }
    }

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    Problema risolto

    import javax.swing.*;
    import java.awt.*;

    public class Layout extends JFrame{
    public Layout() {
    JLabel fileNameLabel = new JLabel("AAAAA");

    JLabel languageNameLabel = new JLabel("BBBBB");
    languageNameLabel.setPreferredSize(new Dimension(80, 20));

    JLabel lineNumberLabel = new JLabel("CCCCC");
    lineNumberLabel.setPreferredSize(new Dimension(80, 20));

    JPanel statusBarPanel = new JPanel();
    statusBarPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    statusBarPanel.add(fileNameLabel, c);

    c.weightx = 0;
    c.gridx = 1;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(languageNameLabel, c);

    c.gridx = 2;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    statusBarPanel.add(lineNumberLabel, c);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(new JTextArea("dsdsdsd sdssd ssdsdsd sdd"), "Center");
    getContentPane().add(statusBarPanel,BorderLayout.S OUTH);
    this.setSize(400, 200);
    this.setVisible(true);
    }
    public static void main (String[] args) {
    new Layout();
    }
    }

    Bisogava azzerare il campo weightx per le variabili lineNumberLabel e languageNameLabel con la seguente linea di codice:

    c.weightx = 0;

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 © 2024 vBulletin Solutions, Inc. All rights reserved.