codice:
import java.awt.*;
import javax.swing.*;
public class ImgGridBag extends JFrame {
public ImgGridBag() {
Container c = getContentPane();
c.setLayout( new BorderLayout() );
c.add(getPannelloPrincipale(), BorderLayout.CENTER);
setTitle("Titolo");
setSize(800, 600);
setDefaultCloseOperation( EXIT_ON_CLOSE );
setLocationRelativeTo( null );
setVisible( true );
}
private Component getPannelloPrincipale() {
JPanel jp = new JPanel();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
jp.setLayout( layout );
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
JLabel lbl = new JLabel( new ImageIcon("immagine.png") );
layout.setConstraints(lbl, constraints);
jp.add( lbl );
return jp;
}
public static void main(String[] args) {
ImgGridBag igb = new ImgGridBag();
}
}
Esempio funzionante.
Ciao.