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);
}
// JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton[15];
String[] nomeBottone = {"1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", "+", "-", "*", "/", ".", "="};
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(nomeBottone[i]);
c.add(bott[i]);}
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("2"), 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("1"), 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("3"), 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("4"), 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("5"), 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("6"), 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("7"), 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("8"), 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("9"), 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("0"), 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);
//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();
}
});
}
}
Grazie!!