Взялся за изучение Java, пишу калькулятор. Не могу понять в чем проблема: при использовании массивов для хранения номеров и ярлыков кнопок ActionListener не срабатывает и при запуске программы, при нажатии кнопок ничего не происходит.
А должно появляться окно:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class CalculatorEngine implements ActionListener {
public void actionPerformed (ActionEvent e){
JOptionPane.showConfirmDialog(null,
"Something happend...",
"Just a test",
JOptionPane.PLAIN_MESSAGE);
}
}
Графическая часть:
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;
public class CalculatorB {
//Declaration of all calculator components
JPanel windowContent;
JTextField displayField;
static JButton button[];
JPanel p1;
JPanel p2;
String buttonNames[] = {"1","2","3","4","5","6","7","8","9","0",".","=","+","-","*","/"};
// Constructor creates the components in memory
// and adds them to the frame, using combination
// of BorderLayout and GridLayout
CalculatorB () {
windowContent = new JPanel();
// Set the layout manager for this panel
GridBagLayout gb1 = new GridBagLayout();
windowContent.setLayout (gb1);
//Create an instance of the GrifBagConstraints
//You'll have to repeat these lines for each component
// that you'd like to add to the grid cell
GridBagConstraints constr = new GridBagConstraints ();
//setting coordinates for calculators display field:
//x coordinate on the grid
constr.gridx=0;
//y coordinate on the grid
constr.gridy=0;
//this cell has the same height as other cells
constr.gridheight=0;
//this cell has wide as 6 other ones
constr.gridwidth=6;
//fill all space in the cell
constr.fill=GridBagConstraints.BOTH;
//proportion of the horizontal space taken by this component
constr.weightx = 1.0;
//proportion of the vertical space taken by this component
constr.weighty = 1.0;
//Position of the component within the cell
constr.anchor= GridBagConstraints.CENTER;
// Create the display field
displayField = new JFormattedTextField ("0");
displayField.setHorizontalAlignment(JFormattedTextField.RIGHT);
//set constraints for this field
gb1.setConstraints(displayField,constr);
windowContent.add (displayField);
//Create the panel with the GridLayout
// that will contain 12 buttons - 10 numeric ones,
// and buttons with point and equal sign
p1 = new JPanel();
GridLayout g1 = new GridLayout ();
p1.setLayout(g1);
// Add the panel p1 to the center area of
// the window
windowContent.add("Center",p1);
// Create buttons using constructor of the class
//JButton that takes the label of the button as a parameter
for (int i=0;i<8;i++){
button[i]= new JButton (buttonNames[i]);
p1.add(button[i]);
}
//Create the panel with the GridLayout
// that will contain 12 buttons - 10 numeric ones,
// and buttons with point and equal sign
p2 = new JPanel();
GridLayout g2 = new GridLayout (4,1);
p2.setLayout(g2);
for (int i=9;i<15;i++){
button[i]= new JButton (buttonNames[i]);
p2.add(button[i]);
}
// Add the panel p2 to the east area of
// the window
windowContent.add("East",p2);
// Create the frame and set its content pane
JFrame frame = new JFrame ("Calculator");
frame.setContentPane (windowContent);
// Set the size the size of the window to be big
// enough to accommodate all controls
frame.pack();
// Finally, display the window
frame.setVisible(true);
}
public static void main (String[] args) {
Calculator calc = new Calculator ();
CalculatorEngine calcEngine = new CalculatorEngine ();
for (int i=0;i<15;i++){
button[i].addActionListener(calcEngine);
}
}
}
При этом если описывать и размещать каждую кнопку отдельно - окошко появляется. Помогите разобраться где ошибка.
Перемещено post-factum из general