Start by trying different layout managers, other than GridLayout
, like GridBagLayout
for example:
backPanel = new JPanel(new BorderLayout());
midPanel = new JPanel(new GridBagLayout());
bottomPanel = new JPanel(new GridBagLayout());
//bottomPanel.setLayout(new BorderLayout());
//final FrameTestBase = new FrameTestBase();
loginButton = new JButton(" Login ");
bottomPanel.add(loginButton);
usernameLabel = new JLabel(" Username : ");
passwordLabel = new JLabel(" Password : ");
usernameText = new JTextField(20);
passwordField = new JPasswordField(20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
midPanel.add(usernameLabel, gbc);
gbc.gridx++;
midPanel.add(usernameText, gbc);
gbc.gridx = 0;
gbc.gridy++;
midPanel.add(passwordLabel, gbc);
gbc.gridx++;
midPanel.add(passwordField, gbc);
bottomPanel.add(loginButton);
backPanel.add(midPanel);
backPanel.add(bottomPanel, BorderLayout.SOUTH);
Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details.
You can easily display a window in the middle of the screen by using frame.setLocationRelativeTo(null);
, for example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LoginExample {
public static void main(String[] args) {
new LoginExample();
}
public LoginExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoginPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPane extends JPanel {
private final JPanel backPanel;
private final JPanel midPanel;
private final JPanel bottomPanel;
private final JButton loginButton;
private final JLabel usernameLabel;
private final JLabel passwordLabel;
private final JTextField usernameText;
private final JPasswordField passwordField;
public LoginPane() {
backPanel = new JPanel(new BorderLayout());
midPanel = new JPanel(new GridBagLayout());
bottomPanel = new JPanel(new GridBagLayout());
loginButton = new JButton(" Login ");
bottomPanel.add(loginButton);
usernameLabel = new JLabel(" Username : ");
passwordLabel = new JLabel(" Password : ");
usernameText = new JTextField(20);
passwordField = new JPasswordField(20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
midPanel.add(usernameLabel, gbc);
gbc.gridx++;
midPanel.add(usernameText, gbc);
gbc.gridx = 0;
gbc.gridy++;
midPanel.add(passwordLabel, gbc);
gbc.gridx++;
midPanel.add(passwordField, gbc);
bottomPanel.add(loginButton);
backPanel.add(midPanel);
backPanel.add(bottomPanel, BorderLayout.SOUTH);
setLayout(new BorderLayout());
add(backPanel);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…