Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883

    (java) Ridimensionamento di un JFrame

    Ho un jframe con una status bar
    Quando ridimensiono il frame in larghezza e lo faccio diventare molto stretto, i dati sulla statusbar si sovrappongono
    La statusbar è formata da 6 jlabel

    Io vorrei che i dati che non possono essere visualizzati perchè il frame è troppo stretto, scomparissero 1 alla volta (più l'utente restringe il frmae più jlabel dovrebbero scomparire) e non si sovrapponessero diventando illeggibili come fa attualmente

    Posto il codice compilante, sapete dirmi dov'è il problema?

    THX

    codice:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class StatusBar extends JPanel {
    
    	private JLabel userJLabel;
    	private JLabel groupJLabel;
    	private JLabel serverJLabel;
    	private JLabel statusJLabel;
    	private JLabel versionJLabel;
    
    	public StatusBar() {
    		userJLabel = new JLabel("LABEL UNO");	
    		groupJLabel = new JLabel("LABEL DUE");
    		serverJLabel = new JLabel("LABEL TRE");	
    		statusJLabel = new JLabel("LABEL QUATTRO");
    		versionJLabel = new JLabel("LABEL CINQUE");
    
    		//create border
    		BevelBorder loweredBevelBorder1 = (BevelBorder) BorderFactory.createLoweredBevelBorder();
    		
    		//set border
    		userJLabel.setBorder(loweredBevelBorder1);
    		groupJLabel.setBorder(loweredBevelBorder1);
    		serverJLabel.setBorder(loweredBevelBorder1);
    		statusJLabel.setBorder(loweredBevelBorder1);
    		versionJLabel.setBorder(loweredBevelBorder1);
    
    		//set statusBar layout
    		this.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    
    		c.weightx = 0;
    		c.gridx = 0;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.NONE;
    		this.add(userJLabel, c);
    
    		c.weightx = 0;
    		c.gridx = 1;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.NONE;
    		this.add(groupJLabel, c);
    
    		c.weightx = 0;
    		c.gridx = 2;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.NONE;
    		this.add(serverJLabel, c);
    
    		//non rimuovere il carattere di spazio altrimenti la statusBar non viene visualizzata correttamente
    		//se l'utente diminuisce le dimensioni del JFrame dello studio
    		JLabel emptyLabel = new JLabel(" ");	
    		//emptyLabel.setPreferredSize(new Dimension(30, 23));	
    		emptyLabel.setBorder(loweredBevelBorder1);
    		c.weightx = 1;
    		c.gridx = 3;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		this.add(emptyLabel, c);
    
    		c.weightx = 0;
    		c.gridx = 4;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.NONE;
    		this.add(statusJLabel, c);
    		
    		c.weightx = 0;
    		c.gridx = 5;
    		c.gridy = 0;
    		c.fill = GridBagConstraints.NONE;
    		this.add(versionJLabel, c);		
    		
    	}
    	
    	public static void main (String[] args) {
    		JFrame frame = new JFrame("PROVA");
    		frame.getContentPane().setLayout(new BorderLayout());
    		frame.getContentPane().add(new JTextArea("bla bla bla"), BorderLayout.CENTER);
    		frame.getContentPane().add(new StatusBar(), BorderLayout.SOUTH);
    		frame.setSize(800, 600);
    		frame.setVisible(true);
    	}
    
    
    }

  2. #2
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Non vorrei dire vaccate, ma credo che non ci sia nessun problema, semplicemente devi impostare la cosa diversamente....non so forse usando un JInternalFrame?
    Qualche post fa qualcuno aveva il tuo stesso problema, solo al contrario, a lui scomparivano i JInternalFrame durante il ridimensionamento del JFrame che li conteneva...ora cerco e poi ti posto il 3d.
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  3. #3
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    in quel topic non viene fornita una soluzione e poi non mi sembra lo stesso problema

  5. #5
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Mi spiego meglio: in quel 3d Lele (mi pare) usando dei JInternalFrame otteneva lo stesso effetto che vuoi tu (anche se a lui non andava bene), quindi potresti provare a seguire quellla strada.
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  6. #6
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    sto leggendo un pò di esempi presi da internet
    il problema risiede quasi sicuramente in qualche costante del layout GrdiBag che non ho definito o che ho definito male

  7. #7
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,304
    Ehm... veramente il problema era diverso e non l'ho risolto... perchè pensavo che fosse una cosa gestita automaticamente da Windows, mentre nemmeno lui la gestisce.

    Credo che per il tuo problema dovresti gestire manualmente la situazione: aggiungi un ComponentListener alla tua StatusBar. Gestisci il metodo componentResized() e controlli, quando questo viene invocato, che le JLabel non vadano sotto una certa dimensione. Se hanno una dimensione inferiore o uguale a quella prefissata da te (o vai in percentuale di finestra), ne rimuovi una.

    E' una soluzione un po' così, ma credo che funzioni.

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  8. #8
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    ho trovato quest'esempio
    Il comportamento che ha quest'esempio quando rimpiccoilisci la finestra sarebbe buono, infatti impedisce che il jframe venga reso più piccolo di una certa dimensione e quindi le jlabel non si vanno a sovrapporre a jtextfield
    Ma non capisco nel codice dove venga impostata questa caratteristica

    codice:
    import javax.swing.*;
    
    import java.awt.*;
    
    public class Layout extends JFrame {
    
    //Creiamo i componenti 
    
    JLabel ipLabel = new JLabel("IP host", SwingConstants.LEFT);
    
    JLabel passwordLabel = new JLabel("Password", SwingConstants.LEFT);
    
    JLabel fileDaInviareLabel = new JLabel("File da inviare", SwingConstants.LEFT);
    
    JTextField ipText = new JTextField();
    
    JPasswordField passwordText = new JPasswordField(); 
    
    JTextField fileDaInviareText = new JTextField();
    
    JButton pulsante = new JButton("Inizia TX"); 
    
    // Definiamo un metodo che ci servirà per definire i limiti di layout
    
    void impostaLimite(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
    
    gbc.gridx = gx;
    
    gbc.gridy = gy;
    
    gbc.gridwidth = gw;
    
    gbc.gridheight = gh;
    
    gbc.weightx = wx;
    
    gbc.weighty = wy;
    
    }
    
    
    
    
    
    
    
    
    
    public Layout() { // Il costruttore della nostra classe
    
    super("File transfer appz");
    
    setSize(300, 120);
    
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    JPanel pannello = new JPanel();
    
    // impostiamo le proprietà dei componenti
    
    ipText.setEditable(true);
    
    fileDaInviareText.setEditable(true);
    
    passwordText.setEchoChar('*'); 
    
    //definiamo il gestore di layout
    
    GridBagLayout grigliaAvanzata = new GridBagLayout();
    
    GridBagConstraints limite = new GridBagConstraints();
    
    pannello.setLayout(grigliaAvanzata);
    
    //definiamo i limiti di ogni componente e lo aggiungiamo al pannello
    
    impostaLimite(limite,0,0,1,1,35,0); //etichetta IP host
    
    limite.fill = GridBagConstraints.NONE;
    
    limite.anchor = GridBagConstraints.EAST;
    
    grigliaAvanzata.setConstraints(ipLabel,limite);
    
    pannello.add(ipLabel);
    
    impostaLimite(limite,1,0,1,1,65,100); //campo IP host
    
    limite.fill = GridBagConstraints.HORIZONTAL;
    
    grigliaAvanzata.setConstraints(ipText,limite);
    
    pannello.add(ipText);
    
    impostaLimite(limite,0,1,1,1,0,0); //etichetta password
    
    limite.fill = GridBagConstraints.NONE;
    
    limite.anchor = GridBagConstraints.EAST;
    
    grigliaAvanzata.setConstraints(passwordLabel,limite);
    
    pannello.add(passwordLabel);
    
    impostaLimite(limite,1,1,1,1,0,100); //campo password
    
    limite.fill = GridBagConstraints.HORIZONTAL;
    
    grigliaAvanzata.setConstraints(passwordText,limite);
    
    pannello.add(passwordText);
    
    impostaLimite(limite,0,2,1,1,0,0); //etichetta File da inviare
    
    limite.fill = GridBagConstraints.NONE;
    
    limite.anchor = GridBagConstraints.EAST;
    
    grigliaAvanzata.setConstraints(fileDaInviareLabel,limite);
    
    pannello.add(fileDaInviareLabel);
    
    impostaLimite(limite,1,2,1,1,0,100); //campo File da inviare
    
    limite.fill = GridBagConstraints.HORIZONTAL;
    
    grigliaAvanzata.setConstraints(fileDaInviareText,limite);
    
    pannello.add(fileDaInviareText);
    
    impostaLimite(limite,0,3,2,1,0,50); // Pulsante
    
    limite.fill = GridBagConstraints.NONE;
    
    limite.anchor = GridBagConstraints.CENTER;
    
    grigliaAvanzata.setConstraints(pulsante,limite);
    
    pannello.add(pulsante);
    
    setContentPane(pannello); // rendiamo il pannello parte del nostro frame
    
    show(); // Visualizziamo il tutto!
    
    }
    
    
    
    
    
    public static void main(String argv[]) {
    
    Layout nf = new Layout();
    
    }
    
    }

  9. #9
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    883
    questa è la miglior soluzione che sono riuscito a realizzare per creare una statusBar decente

    codice:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    
    public class StatusBar extends JPanel {
    
    	private JLabel userJLabel;
    	private JLabel userValue;
    	private JLabel groupJLabel;
    	private JLabel groupValue;
    	private JLabel serverJLabel;
    	private JLabel serverValue;
    
    	private JLabel statusJLabel;
    	private JLabel statusValue;
    	private JLabel versionJLabel;
    	private JLabel versionValue;
    
    	public StatusBar() {
    		userJLabel = new JLabel("<html><font face='Arial' size='2' color=Black>Nome:</font></html>");
    		userValue = new JLabel("<html><font face='Arial' size='2' color=Red>Mario</font></html>");
    		
    		JPanel userPanel = new JPanel();
    		userPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2 ,2));
    		userPanel.add(userJLabel);
    		userPanel.add(userValue);	
    
    		groupJLabel = new JLabel("<html><font face='Arial' size='2' color=Black>Gruppo:</font></html>");
    		groupValue = new JLabel("<html><font face='Arial' size='2' color=Green>lavoratori</font></html>");
    		
    		JPanel groupPanel = new JPanel();
    		groupPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2 ,2));
    		groupPanel.add(groupJLabel);
    		groupPanel.add(groupValue);
    
    		serverJLabel = new JLabel("<html><font face='Arial' size='2' color=Black>Comune:</font></html>");
    		serverValue = new JLabel("<html><font face='Arial' size='2' color=Green>Firenze</font></html>");
    		
    		JPanel serverPanel = new JPanel();
    		serverPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2 ,2));
    		serverPanel.add(serverJLabel);
    		serverPanel.add(serverValue);
    
    		statusJLabel = new JLabel("<html><font face='Arial' size='2' color=Black>Stato:</font></html>");
    		statusValue = new JLabel("<html><font face='Arial' size='2' color=Red>non attivo</font></html>");
    		
    		JPanel statusPanel = new JPanel();
    		statusPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2 ,2));
    		statusPanel.add(statusJLabel);
    		statusPanel.add(statusValue);
    
    		versionJLabel = new JLabel("<html><font face='Arial' size='2' color=Black>Versione:</font></html>");
    		versionValue = new JLabel("<html><font face='Arial' size='2' color=Green>estiva</font></html>");
    				
    		JPanel versionPanel = new JPanel();
    		versionPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2 ,2));
    		versionPanel.add(versionJLabel);
    		versionPanel.add(versionValue);	
    		
    
    		//create border
    		BevelBorder loweredBevelBorder = (BevelBorder) BorderFactory.createLoweredBevelBorder();
    		
    		JPanel emptyPanel = new JPanel();	
    		
    		//set border
    		userPanel.setBorder(loweredBevelBorder);
    		groupPanel.setBorder(loweredBevelBorder);
    		serverPanel.setBorder(loweredBevelBorder);
    		emptyPanel.setBorder(loweredBevelBorder);
    		statusPanel.setBorder(loweredBevelBorder);
    		versionPanel.setBorder(loweredBevelBorder);		
    
    		this.setLayout(new BorderLayout());
    		
    		JPanel accountPanel = new JPanel();
    		accountPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    		accountPanel.add(userPanel);
    		accountPanel.add(groupPanel);		
    		accountPanel.add(serverPanel);
    				
    		JPanel processPanel = new JPanel();
    		processPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    		processPanel.add(statusPanel);		
    		processPanel.add(versionPanel);
    				
    		add(accountPanel, BorderLayout.WEST);
    		add(emptyPanel, BorderLayout.CENTER);
    		add(processPanel, BorderLayout.EAST);		
    	}
    	
    	public static void main (String[] args) {
    		JFrame frame = new JFrame("PROVA");
    		frame.getContentPane().setLayout(new BorderLayout());
    		frame.getContentPane().add(new JTextArea("bla bla bla"), BorderLayout.CENTER);
    		frame.getContentPane().add(new StatusBar(), BorderLayout.SOUTH);
    		frame.setSize(800, 600);
    		frame.setVisible(true);
    	}
    
    
    }
    Quando l'utente ridimensiona il frame, viene ridimensionato l'emptypanel

    Il problema che mi si verifica con il grdibaglayout (prima soluzione proposta) non si verifica con sdk 1.5.0 quindi suppongo sia un bug

    Se qualcuno trova altre soluzioni sono sempre interessato

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.