Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>Avoid <code>null</code> layouts. You don't control how fonts are rendered on different system so unless you intent to only run you application on the machine it was developed for, avoid <code>null</code> layouts. Learn to use and understand how different layout managers can be used to achieve what you want.</li> <li>Use a <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html" rel="nofollow noreferrer"><code>CardLayout</code></a>, this is what it was designed to do, allow you to easily change groups of visible components.</li> </ol> <p>This will require you to break down you application into separate components/panels but will allow you focus on the individual needs of each section independently </p> <p><strong>Update with example</strong></p> <p><img src="https://i.stack.imgur.com/bf0JR.png" alt="enter image description here"><img src="https://i.stack.imgur.com/xDNw2.png" alt="enter image description here"><img src="https://i.stack.imgur.com/wAYiC.png" alt="enter image description here"></p> <pre><code>import java.awt.CardLayout; import java.awt.Container; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TestCardLayout100 { public static void main(String[] args) { new TestCardLayout100(); } public TestCardLayout100() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } LoginPane loginPane = new LoginPane(); RegisterPane registerPane = new RegisterPane(); JPanel startPane = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; startPane.add(loginPane, gbc); gbc.gridx = 2; startPane.add(registerPane, gbc); gbc.gridx = 1; gbc.fill = GridBagConstraints.VERTICAL; startPane.add(new JSeparator(JSeparator.VERTICAL), gbc); JPanel loggedInPane = new JPanel(new GridBagLayout()); loggedInPane.add(new JLabel("Logged In...")); JPanel registeredPane = new JPanel(new GridBagLayout()); registeredPane.add(new JLabel("Registered...")); final JFrame frame = new JFrame("Testing"); final CardLayout cardLayout = new CardLayout(); loginPane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.show(frame.getContentPane(), "loggedIn"); } }); registerPane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.show(frame.getContentPane(), "registered"); } }); Container contentPane = frame.getContentPane(); contentPane.setLayout(cardLayout); contentPane.add(startPane, "startPane"); contentPane.add(loggedInPane, "loggedIn"); contentPane.add(registeredPane, "registered"); cardLayout.show(contentPane, "startPane"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class LoginPane extends JPanel { private final JLabel logusrlbl = new JLabel("Enter Username"); private final JLabel logpwdlbl = new JLabel("Enter Password"); private final JTextField logusr = new JTextField(10); private final JTextField logpwd = new JPasswordField(10); private final JButton login = new JButton("Login"); public LoginPane() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = GridBagConstraints.REMAINDER; add(new JLabel("Login"), gbc); gbc.gridy++; gbc.gridwidth = 1; add(logusrlbl, gbc); gbc.gridy++; add(logpwdlbl, gbc); gbc.gridx++; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridy = 1; add(logusr, gbc); gbc.gridy++; add(logpwd, gbc); gbc.gridx = 0; gbc.gridy++; gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.fill = GridBagConstraints.NONE; add(login, gbc); } public void addActionListener(ActionListener listener) { login.addActionListener(listener); } } public class RegisterPane extends JPanel { private final JLabel logusrlbl = new JLabel("Enter Username"); private final JLabel logpwdlbl = new JLabel("Enter Password"); private final JTextField logusr = new JTextField(10); private final JTextField logpwd = new JPasswordField(10); private final JButton register = new JButton("Register"); public RegisterPane() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = GridBagConstraints.REMAINDER; add(new JLabel("Register"), gbc); gbc.gridy++; gbc.gridwidth = 1; add(logusrlbl, gbc); gbc.gridy++; add(logpwdlbl, gbc); gbc.gridx++; gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridy = 1; add(logusr, gbc); gbc.gridy++; add(logpwd, gbc); gbc.gridx = 0; gbc.gridy++; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; gbc.gridwidth = GridBagConstraints.REMAINDER; add(register, gbc); } public void addActionListener(ActionListener listener) { register.addActionListener(listener); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload