[JAVA] Problemi con GridBagLayout e AddActionListener

fpertegato

Nuovo Utente
26 Lug 2010
16
12
0
Vicenza
Ciao a tutti!:byebye:
Ho scritto il codice per una classe che usa il GridBagLayout...ma ora non so più dove e come dichiarare e introdurre gli ActionListner, se qualcuno ne capisce qualcosa più di me (facile, visto che ho provato oggi per la prima volta il GridBagLayout, e comunque in java sono nato ieri) mi può aiutare? Posto il codice quì sotto:
Codice:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;

public class Calcola {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button;

	pane.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();

	if (shouldFill) {
	c.fill = GridBagConstraints.HORIZONTAL;
	}

	button = new JButton("2");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,0);
	c.gridx = 1;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("1");
	if (shouldWeightX) {
	c.weightx = 0.5;
	}
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.insets = new Insets(5,5,0,0);
	c.gridx = 0;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("3");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,5);
	c.gridx = 2;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("4");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("5");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("6");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("7");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 3;
	pane.add(button, c);

	button = new JButton("8");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 3;
	pane.add(button, c);

	button = new JButton("9");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 3;	//Righe
	pane.add(button, c);

	button = new JButton("+");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 4;
	pane.add(button, c);

	button = new JButton("0");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 4;
	pane.add(button, c);

	button = new JButton("-");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 4;	//Righe
	pane.add(button, c);

	button = new JButton("*");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 5;
	pane.add(button, c);

	button = new JButton("/");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 5;
	pane.add(button, c);

	button = new JButton(".");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 5;	//Righe
	pane.add(button, c);

//	Display della calcolatrice	Receiver1 receiver1 = new Receiver1();
	Receiver2 receiver2 = new Receiver2();	//	ActionListener(s) 
	Receiver3 receiver3 = new Receiver3();	//- da implementare dopo questa classe -
	Receiver4 receiver4 = new Receiver4();
	Receiver5 receiver5 = new Receiver5();
	Receiver6 receiver6 = new Receiver6();

	JTextField finestrino = new JTextField(10);
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipady = 40;      //Altezza del componente
	c.insets = new Insets(5,5,0,5);
	c.weightx = 0.0;
	c.gridwidth = 3;//lungo 3 colonne
	c.gridx = 0;
	c.gridy = 0;
	pane.add(finestrino, c);

	button = new JButton("=");
	c.fill = GridBagConstraints.VERTICAL;
	c.ipadx = 20;      //larghezza del componente
	c.gridheight = 5;
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,5);
	c.gridx = 3;
	c.gridy = 1;
	pane.add(button, c);
	}

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Crea e mostra la window.
        JFrame frame = new JFrame("Calcolatrice");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Seta il content pane.
        addComponentsToPane(frame.getContentPane());

        //Mostro la window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Vi ringrazio!!
 
Scusa, ma sei sicuro che il tuo codice funzioni? secondo me non l'hai provato. A colpo d'occhio vedo che dichiari un solo pulsante "JButton button;" e poi fai una serie di new sempre sulla stessa istanza. In pratica stai istanziando sempre lo stesso button. Se hai bisogno di più pulsanti devi istanziare un JButton per ciascuno di essi.
Per quanto riguarda la tua domanda specifica invece, per ogni pulsante istanziato ti basta fare:

Codice:
buttonX.addActionListener(receiverX);
 
Ciao satifal, grazie per la risposta!:byebye:
Beh, a dir la verità mi fa vedere l'output...però anchio sono propenso a dichiararmi un'array di bottoni, un'array di stringhe e con quelle "nominare" i bottoni...ora lo faccio e dopo dico com'è andata...
 
L'output te lo fa vedere ma tutti sono lo stesso pulsante per cui fanno tutti la stessa cosa.
 
Ok, allora io ho fatto così:
Codice:
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;

public class Calcola {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

	button = new JButton[15];

	String[] nomeBottone = {"1", "2", "3", "4", "5", "6", "7", "8",
				"9", "0", "+", "-", "*", "/", ".", "="};

	for (int i = 0; i < bott.length; i++) {
	    button[i] = new JButton(nomeBottone[i]);
	    c.add(button[i]);}
 
	pane.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();

	if (shouldFill) {
	c.fill = GridBagConstraints.HORIZONTAL;
	}

	button = new JButton("2");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,0);
	c.gridx = 1;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("1");
	if (shouldWeightX) {
	c.weightx = 0.5;
	}
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.insets = new Insets(5,5,0,0);
	c.gridx = 0;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("3");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipadx = 40;      //larghezza del componente
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,5);
	c.gridx = 2;
	c.gridy = 1;
	pane.add(button, c);

	button = new JButton("4");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("5");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("6");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;
	c.gridy = 2;
	pane.add(button, c);

	button = new JButton("7");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 3;
	pane.add(button, c);

	button = new JButton("8");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 3;
	pane.add(button, c);

	button = new JButton("9");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 3;	//Righe
	pane.add(button, c);

	button = new JButton("+");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 4;
	pane.add(button, c);

	button = new JButton("0");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 4;
	pane.add(button, c);

	button = new JButton("-");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 4;	//Righe
	pane.add(button, c);

	button = new JButton("*");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,5,0,0);
	c.gridx = 0;
	c.gridy = 5;
	pane.add(button, c);

	button = new JButton("/");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,0);
	c.gridx = 1;
	c.gridy = 5;
	pane.add(button, c);

	button = new JButton(".");
	c.fill = GridBagConstraints.HORIZONTAL;
	c.weightx = 0.5;
	c.insets = new Insets(0,0,0,5);
	c.gridx = 2;	//Colonne
	c.gridy = 5;	//Righe
	pane.add(button, c);

//	Display della calcolatrice:
	JTextField finestrino = new JTextField(10);
	c.fill = GridBagConstraints.HORIZONTAL;
	c.ipady = 40;      //Altezza del componente
	c.insets = new Insets(5,5,0,5);
	c.weightx = 0.0;
	c.gridwidth = 3;//lungo 3 colonne
	c.gridx = 0;
	c.gridy = 0;
	pane.add(finestrino, c);

	button = new JButton("=");
	c.fill = GridBagConstraints.VERTICAL;
	c.ipadx = 20;      //larghezza del componente
	c.gridheight = 5;
	c.weightx = 0.5;
	c.insets = new Insets(5,0,0,5);
	c.gridx = 3;
	c.gridy = 1;
	pane.add(button, c);
	}

    private static void createAndShowGUI() {
        //Crea e mostra la window.
        JFrame frame = new JFrame("Calcolatrice");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Setta il content pane.
        addComponentsToPane(frame.getContentPane());

        //Mostro la window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
..però devo aver sbagliato qualcosa in questo punto:
Codice:
	String[] nomeBottone = {"1", "2", "3", "4", "5", "6", "7", "8",
				"9", "0", "+", "-", "*", "/", ".", "="};

	for (int i = 0; i < bott.length; i++) {
	    button[i] = new JButton(nomeBottone[i]);
	    c.add(button[i]);}
non capisco perchè non riesco ad assegnargli i nomi?
 

Discussioni simili