Сначала я использовал Grid layout, затем я понял, что хочу вместо этого использовать Spring layout.Когда я добавляю Spring Layout Panel, ничего не хочет показывать
public class ComplexWindow extends JFrame {
public ComplexWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 300, 420);
JPanel mainPanel = (JPanel) getContentPane();
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout());
SpringLayout layout = new SpringLayout();
JPanel textPanel = new JPanel(layout);
JLabel lblName = new JLabel("Ime:");
textPanel.add(lblName);
JTextField txtName = new JTextField();
txtName.setColumns(10);
textPanel.add(txtName);
JLabel lblSurname = new JLabel("Prezime:");
textPanel.add(lblSurname);
JTextField txtSurname = new JTextField();
txtSurname.setColumns(10);
textPanel.add(txtSurname);
layout.putConstraint(SpringLayout.EAST, lblName, 5,
SpringLayout.WEST, txtName);
layout.putConstraint(SpringLayout.SOUTH, lblName, 5,
SpringLayout.NORTH, lblSurname);
layout.putConstraint(SpringLayout.SOUTH, txtName, 5,
SpringLayout.NORTH, txtSurname);
layout.putConstraint(SpringLayout.EAST, lblSurname, 5,
SpringLayout.WEST, txtSurname);
mainPanel.add(textPanel, BorderLayout.NORTH);
}
public static void main(String [] args) {
SwingUtilities.invokeLater(() -> {
ComplexWindow window = new ComplexWindow();
window.pack();
window.setVisible(true);
});
}
}