I'm trying to create a simple calculator with Java. For that purpose I created an array of JButton and added them to the JPanel.
The issue: the buttons are not visible.
I also added a single JLabel and a single JButton for testing, and they show up correctly.
The code:
package test;
import java.awt.BorderLayout;
import javax.swing.*;
public class Test {
JLabel testLabel = new JLabel("Test label", SwingConstants.CENTER);
JButton testButton = new JButton("Test button");
JButton buttons[];
Test() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
for (int i = 0; i > 15; i++) {
buttons[i] = new JButton(Integer.toString(i));
panel.add(buttons[i], BorderLayout.CENTER);
}
panel.add(testButton, BorderLayout.CENTER);
panel.add(testLabel, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
Test cTest = new Test();
}
}
What am I doing wrong?
JPanelisFlowLayout, notBorderLayout.BorderLayout, you still would only see the last component since all are being added withBorderLayout.CENTER