# Esempio di disposizioni più sofisticate: BorderLayout e JPanel

- Inseriremo nel file MyFrameBorder.java il seguente codice:

      package framebordertest;

      import java.awt.BorderLayout;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JTextField;

      public class MyFrameBorder extends JFrame implements ActionListener {

      	private JButton closeButton;
      	private JButton helloButton;
      	private JTextField nameField;
      	private JLabel replay

      	private static final long serialVersionUID = 1L;

      	public MyFrameBorder () {
      		setTitle("Finestra con form di prova");

      		closeButton = new JButton("Ok");
      		helloButton = new JButton ("Saluti...");
      		nameField = new JTextField();
      		nameField.setColumns(10);
      		replay = new JLabel("Attendo");

      		closeButton.addActionListener(this);
      		helloButton.addActionListener(this);

      		setLayout(new BorderLayout());
      		JPanel p = new JPanel();
      		p.setLayout(new FlowLayout());
      		p.add(new JLabel("Scrivi il tuo nome"));
      		p.add(nameField);
      		add(p, BorderLayout.NORTH);
      		add(replay, BorderLayout.CENTER);
      		p = new JPanel();
      		p.setLayout(new FlowLayout());
      		p.add(helloButton);
      		p.add(closeButton);
      		add(p, BorderLayout.SOUTH);
      		pack();
      	}

      	@Override
      	public void actionPerformed(ActionEvent evt) {
      		if (evt.getSource() == closeButton) {
      			dispose();
      		} else if (evt.getSource() == helloButton) {
      			replay.setText("Ciao " + nameField.getText());
      		}
      	}
      }

- Come al solito in main va solo modificato il nome della classe, quindi non la riporto.

© Ing. Stefano Salvi - released under FDL licence