Pagina 1 di 1

[JAVA] Contenuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 17:37
da ulisse89
Ciao. E' tutto il pomeriggio che sto perdendo la testa a capire perchè in questa applicazione grafica, quando creo MainFrame , i componenti si vedono solo se ridimensiono la finestra (anche di pochissimo, sia se l'allargo che se la restringo). Posto tutto il codice della classe MainFrame, anche se l'errore probabilmente sarà nel metodo initGUI().

Grazie a chi mi darà una mano. :thumbright:

Codice: Seleziona tutto

package gui;
import javax.swing.*;
import model.StopFactory;
import routing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class MainFrame extends JFrame implements ActionListener {
	
	static final long serialVersionUID = 1L;
	
	private FullRouteCalculator routeCalculator;
	private JLabel partenzaLabel;
	private JLabel arrivoLabel;
	private JComboBox partenzaComboBox;
	private JComboBox arrivoComboBox;
	private JPanel selectionPanel;
	private JPanel partenzaArrivoPanel;
	private JPanel searchPanel;
	private JLabel changeLabel;
	private JComboBox changeComboBox;
	private JButton searchButton;
	private JPanel outputPanel;
	private JTextArea outputArea;
	
	public MainFrame()
	{
		super();
		this.routeCalculator = new FullRouteCalculator();
		initGUI();
	}
	
	public void initGUI()
	{
		this.setLayout(new BorderLayout());
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(screenSize.width/2, screenSize.height/2);
		this.setLocation((int)(screenSize.width - getWidth()) / 2, (int)(screenSize.height - this.getHeight())/2);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		selectionPanel = new JPanel();
		selectionPanel.setLayout(new GridLayout(2, 1));
		{
			partenzaArrivoPanel = new JPanel();
			FlowLayout layout = new FlowLayout();
			layout.setHgap(15);
			partenzaArrivoPanel.setLayout(layout);
			{
				partenzaLabel = new JLabel("Partenza: ");
				partenzaComboBox = new JComboBox();
				partenzaArrivoPanel.add(partenzaLabel);
				partenzaArrivoPanel.add(partenzaComboBox);

				arrivoLabel = new JLabel("Arrivo: ");
				arrivoComboBox = new JComboBox();
				partenzaArrivoPanel.add(arrivoLabel);
				partenzaArrivoPanel.add(arrivoComboBox);
				
				changeLabel = new JLabel("Cambi: ");
				changeComboBox = new JComboBox();
				changeComboBox.addItem("0");
				changeComboBox.addItem("1");
				changeComboBox.addItem("2");
				changeComboBox.addItem("3");
				partenzaArrivoPanel.add(changeLabel);
				partenzaArrivoPanel.add(changeComboBox);
			}
			
			populateComboBoxes();
			
			searchPanel = new JPanel();
			searchPanel.setLayout(new FlowLayout());
			{
				searchButton = new JButton("Cerca");
				searchButton.addActionListener(this);
				searchPanel.add(searchButton);
			}
		}
		selectionPanel.add(partenzaArrivoPanel);
		selectionPanel.add(searchPanel);
		
		outputPanel = new JPanel();
		outputArea = new JTextArea(50, 100);
		outputPanel.add(outputArea);

		this.getContentPane().add(selectionPanel, BorderLayout.NORTH);
		this.getContentPane().add(outputPanel, BorderLayout.WEST);
	}
		
	public void populateComboBoxes()
	{
		StopFactory factory = StopFactory.getInstance();
		for(String a : factory.getStopsNames())
		{
			partenzaComboBox.addItem(a);
		}
		for(String a : factory.getStopsNames())
		{
			arrivoComboBox.addItem(a);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == searchButton)
		{
			try
			{
				ArrayList<Route> routeSet = routeCalculator.calculate(partenzaComboBox.getSelectedItem().toString(), arrivoComboBox.getSelectedItem().toString(), Integer.parseInt((String)changeComboBox.getSelectedItem()));
				for(Route a : routeSet)
				{
					outputArea.append(a.toString());
					outputArea.append("\n");
				}
			}
			catch(Exception ex)
			{}
		}
	}

}

Re: [JAVA] Contentuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 17:56
da d4z_c0nf
ulisse89 ha scritto:Ciao. E' tutto il pomeriggio che sto perdendo la testa a capire perchè in questa applicazione grafica, quando creo MainFrame , i componenti si vedono solo se ridimensiono la finestra (anche di pochissimo, sia se l'allargo che se la restringo). Posto tutto il codice della classe MainFrame, anche se l'errore probabilmente sarà nel metodo initGUI().

Grazie a chi mi darà una mano. :thumbright:

Codice: Seleziona tutto

package gui;
import javax.swing.*;
import model.StopFactory;
import routing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class MainFrame extends JFrame implements ActionListener {
	
	static final long serialVersionUID = 1L;
	
	private FullRouteCalculator routeCalculator;
	private JLabel partenzaLabel;
	private JLabel arrivoLabel;
	private JComboBox partenzaComboBox;
	private JComboBox arrivoComboBox;
	private JPanel selectionPanel;
	private JPanel partenzaArrivoPanel;
	private JPanel searchPanel;
	private JLabel changeLabel;
	private JComboBox changeComboBox;
	private JButton searchButton;
	private JPanel outputPanel;
	private JTextArea outputArea;
	
	public MainFrame()
	{
		super();
		this.routeCalculator = new FullRouteCalculator();
		initGUI();
	}
	
	public void initGUI()
	{
		this.setLayout(new BorderLayout());
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(screenSize.width/2, screenSize.height/2);
		this.setLocation((int)(screenSize.width - getWidth()) / 2, (int)(screenSize.height - this.getHeight())/2);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		selectionPanel = new JPanel();
		selectionPanel.setLayout(new GridLayout(2, 1));
		{
			partenzaArrivoPanel = new JPanel();
			FlowLayout layout = new FlowLayout();
			layout.setHgap(15);
			partenzaArrivoPanel.setLayout(layout);
			{
				partenzaLabel = new JLabel("Partenza: ");
				partenzaComboBox = new JComboBox();
				partenzaArrivoPanel.add(partenzaLabel);
				partenzaArrivoPanel.add(partenzaComboBox);

				arrivoLabel = new JLabel("Arrivo: ");
				arrivoComboBox = new JComboBox();
				partenzaArrivoPanel.add(arrivoLabel);
				partenzaArrivoPanel.add(arrivoComboBox);
				
				changeLabel = new JLabel("Cambi: ");
				changeComboBox = new JComboBox();
				changeComboBox.addItem("0");
				changeComboBox.addItem("1");
				changeComboBox.addItem("2");
				changeComboBox.addItem("3");
				partenzaArrivoPanel.add(changeLabel);
				partenzaArrivoPanel.add(changeComboBox);
			}
			
			populateComboBoxes();
			
			searchPanel = new JPanel();
			searchPanel.setLayout(new FlowLayout());
			{
				searchButton = new JButton("Cerca");
				searchButton.addActionListener(this);
				searchPanel.add(searchButton);
			}
		}
		selectionPanel.add(partenzaArrivoPanel);
		selectionPanel.add(searchPanel);
		
		outputPanel = new JPanel();
		outputArea = new JTextArea(50, 100);
		outputPanel.add(outputArea);

		this.getContentPane().add(selectionPanel, BorderLayout.NORTH);
		this.getContentPane().add(outputPanel, BorderLayout.WEST);
	}
		
	public void populateComboBoxes()
	{
		StopFactory factory = StopFactory.getInstance();
		for(String a : factory.getStopsNames())
		{
			partenzaComboBox.addItem(a);
		}
		for(String a : factory.getStopsNames())
		{
			arrivoComboBox.addItem(a);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == searchButton)
		{
			try
			{
				ArrayList<Route> routeSet = routeCalculator.calculate(partenzaComboBox.getSelectedItem().toString(), arrivoComboBox.getSelectedItem().toString(), Integer.parseInt((String)changeComboBox.getSelectedItem()));
				for(Route a : routeSet)
				{
					outputArea.append(a.toString());
					outputArea.append("\n");
				}
			}
			catch(Exception ex)
			{}
		}
	}

}
Vado a ricordi vaghi, prova a mettere il setVisible(true), dopo aver caricato tutto il contenuto. Quindi come ultima istruzione di initGui.
rock

Re: [JAVA] Contentuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 18:18
da neongen
d4z_c0nf ha scritto:Vado a ricordi vaghi, prova a mettere il setVisible(true), dopo aver caricato tutto il contenuto. Quindi come ultima istruzione di initGui.
rock
giusto: setVisible(true) nel codice sopra è messo invece prima di inizializzare il contenuto del frame, per questo ti fa vedere una finestra vuota.

edit: io personalmente non lo metto quasi mai dentro il codice del frame; preferisco da fuori

Codice: Seleziona tutto

MioFrame f = new MioFrame();
f.setVisible(true);

Re: [JAVA] Contentuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 18:30
da m0rdr3d
Lo scopo di setVisible è quello di mostrare la finestra solo al termine della sua costruzione, ed evitare che venga mostrata un pezzo alla volta.
Di fatto richiamare il metodo e poi aggiungere altre cose al frame non ha molto senso.
Dovrebbe essere l'ultima invocazione o, come ti è stato suggerito, essere richiamato all'esterno del codice di inizializzazione.

Re: [JAVA] Contentuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 19:17
da ulisse89
Ho capito ragazzi. Non lo sapevo anche perchè la grafica di java ci è stata spiegata in una lezione. Grazie mille.

Re: [JAVA] Contentuto Frame compare solo se ridimensionata

Inviato: gio 18 giu 2009, 19:40
da m0rdr3d
ulisse89 ha scritto:Non lo sapevo anche perchè la grafica di java ci è stata spiegata in una lezione.
Sì, purtroppo è uno scenario che conosco :D