title: Java Swing - Center a JLabel using BorderLayout
body: <p>I have an assignment, and the instructions specify that I need two labels to be centered on the screen (both horizontally and vertically). <br>
  I know this question has been asked many times, but whenever I have searched for this question, the solution given is often to use a different layout manager, like BoxLayout. However, the instructions also specify that I must use BorderLayout.<br>
  Originally I did not add the labels to a panel and instead added them directly to the frame, but this didn't work, so I was advised to put them both on a JPanel, which hasn't worked either.</p>
  
  <p>My code looks like this currently:</p>
  
  <pre><code>    JButton butSubmit = new JButton("Submit");
      JButton butReset = new JButton("Reset");
  
      JLabel nameAndReg = new JLabel("My details", SwingConstants.CENTER);
      JLabel errorMsg = new JLabel("The error message", SwingConstants.CENTER);
      nameAndReg.setForeground(Color.blue);
  
      JTextField redVal = new JTextField(3);
      JTextField greenVal = new JTextField(3);
      JTextField blueVal = new JTextField(3);
  
      JPanel butPanelSouth = new JPanel();
      JPanel butPanelNorth = new JPanel();
      JPanel labsPanel = new JPanel();
  
      butPanelSouth.add(redVal);
      butPanelSouth.add(greenVal);
      butPanelSouth.add(blueVal);
      butPanelSouth.add(butSubmit);
      butPanelNorth.add(butReset);
      labsPanel.add(nameAndReg, BorderLayout.CENTER);
      labsPanel.add(errorMsg, BorderLayout.CENTER);
  
      add(labsPanel, BorderLayout.CENTER);
      add(butPanelNorth, BorderLayout.NORTH);
      add(butPanelSouth, BorderLayout.SOUTH);
  </code></pre>
  
  <p>And this is what it produces:
  <a href="https://i.stack.imgur.com/qVL8E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qVL8E.png" alt="Current result"></a>
  <br>
  How can I get 'My details' and 'The error message' to be vertically and horizontally in the middle of the window, one under the other, and still use BorderLayout?</p>
  
  <p>Edit: Sorry, I realise I wasn't very clear. The advice I was given was 'perhaps nest your layout managers i.e. put a JPanel in the centre of a BorderLayout on  a JFrame.'</p>