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:
Vi ringrazio!!
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();
}
});
}
}