codice:
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
public class Calcola {
private static GridBagConstraints setConstraints(GridBagConstraints c, int fill, int ipadx,
double weightx, int gridx, int gridy, Insets insets) {
c.fill = fill;
c.ipadx = ipadx;
c.weightx = weightx;
c.insets = insets;
c.gridx = gridx;
c.gridy = gridy;
return c;
}
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton[] buttons = new JButton[16];
String[] nomeBottone = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "-", "*", "/", ".", "="};
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(nomeBottone[i]);
}
pane.add(buttons[0], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 0, 1, new Insets(5, 5, 0, 0)));
pane.add(buttons[1], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 1, 1, new Insets(5, 0, 0, 0)));
pane.add(buttons[2], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 2, 1, new Insets(5, 0, 0, 5)));
pane.add(buttons[3], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 0, 2, new Insets(0, 5, 0, 0)));
pane.add(buttons[4], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 1, 2, new Insets(0, 0, 0, 0)));
pane.add(buttons[5], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 2, 2, new Insets(0, 0, 0, 5)));
pane.add(buttons[6], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 0, 3, new Insets(0, 5, 0, 0)));
pane.add(buttons[7], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 1, 3, new Insets(0, 0, 0, 0)));
pane.add(buttons[8], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 2, 3, new Insets(0, 0, 0, 5)));
pane.add(buttons[9], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 0, 4, new Insets(0, 5, 0, 0)));
pane.add(buttons[10], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 1, 4, new Insets(0, 0, 0, 0)));
pane.add(buttons[11], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 2, 4, new Insets(0, 0, 0, 5)));
pane.add(buttons[12], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 0, 5, new Insets(0, 5, 0, 0)));
pane.add(buttons[13], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 1, 5, new Insets(0, 0, 0, 0)));
pane.add(buttons[14], setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.5, 2, 5, new Insets(0, 0, 0, 5)));
JTextField finestrino = new JTextField(10);
c = setConstraints(c, GridBagConstraints.HORIZONTAL, 40, 0.0, 0, 0, new Insets(5, 5, 0, 5));
c.ipady = 40;
c.gridwidth = 3;
pane.add(finestrino, c);
c = setConstraints(c, GridBagConstraints.VERTICAL, 40, 0.5, 3, 1, new Insets(5, 0, 0, 5));
c.ipadx = 20;
c.gridheight = 5;
pane.add(buttons[15], c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Calcolatrice");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
ma ti consiglio di capire ciò che stai facendo piuttosto che copiare alla cieca codice già fatto. Non è il modo di imparare questo, va bene anche il copia e incolla, che personalmente detesto, ma almeno fatto con cognizione di causa.