Precedente Indice Successiva

Esempio di Disegno figure e testo - La finestra

  • La finestra va modificata per aggiungere due bottoni, per le due azioni. A b1 assceremo il testo Prossimo e a b2 il testo Precedente.
  • Nel file MyFrameForme,java scriveremo:
    package frameformetest;

    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.JPanel;

    public class MyFrameForme extends JFrame implements ActionListener {
    private JButton fine;
    private JButton b1;
    private JButton b2;
    private MyPanelForme disegno;

    private static final long serialVersionUID = 1L;

    public MyFrameForme () {
    setTitle("Finestra con area con disegno");

    setLayout(new BorderLayout());

    fine = new JButton("Fine");
    b1 = new JButton("Prossima");
    b2 = new JButton("Precedente");
    disegno = new MyPanelForme();

    fine.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);

    add(disegno, BorderLayout.CENTER);

    JPanel bottoni = new JPanel();
    bottoni.setLayout(new FlowLayout());
    bottoni.add(b1);
    bottoni.add(b2);
    bottoni.add(fine);
    add(bottoni, BorderLayout.SOUTH);

    pack();
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == b1) {
    disegno.eseguiAzione1();
    } else if (evt.getSource() == b2) {
    disegno.eseguiAzione2();
    } else if (evt.getSource() == fine) {
    dispose();
    }
    }
    }
  • Come sempre, nel main c'è solo da modificare il nome della classe caricata, quindi non lo riporto.
© Ing. Stefano Salvi - released under FDL licence

Valid XHTML 1.0! Valid CSS!