You must use GridBagConstraints. Change the gridYvalue of the constraints when adding a second label, and it will be placed under the first.
Try the following:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.CENTER;
JLabel terminalLabel = new JLabel("No reader connected!");
frame.add(terminalLabel,constraints);
constraints.gridy = 1;
JLabel cardlabel = new JLabel("No card presented");
frame.add(cardlabel,constraints);
frame.setVisible(true);
Also read this: How to use GridBagLayout
source
share