Centering a Jpanel on screen
Hi.
I'd like to center my JPanel to the center of the screen.
I've looked around, however i can't find out how to do it.
I found about setting the "bounds", however that is based on x, y Co-ordinates, and i'd like it to be centered for people on 800 x 600 resolutions, as well as 1024 x 768 (for example).
The other way i found is by:
Login.setVerticalAlignment(JFrame.CENTER);
However that results in an error during compilation:
Compiling 1 source file to W:\Java\Covenant\build\classes
W:\Java\Covenant\src\covenant\Login.java:81: cannot find symbol
symbol : variable CENTER
location:class javax.swing.JFrame
Login.setHorizontalAlignment(JFrame.CENTER);
W:\Java\Covenant\src\covenant\Login.java:82: cannot find symbol
symbol : variable CENTER
location:class javax.swing.JFrame
Login.setVerticalAlignment(JFrame.CENTER);
2 errors
BUILD FAILED (total time: 0 seconds)
So my question is, how can i do it?
Thanks for any help.
[1118 byte] By [
allydma] at [2007-11-26 21:38:54]

# 5
I havn't got another setLocation() or setBounds() in my code, and i havn't sized my frame.
The code is:
public Login() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
Title = new javax.swing.JLabel();
Username = new javax.swing.JTextField();
UsernameLabel = new javax.swing.JLabel();
PasswordLabel = new javax.swing.JLabel();
Password = new javax.swing.JPasswordField();
LoginButton = new javax.swing.JButton();
FooterText = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Login");
setBounds(new java.awt.Rectangle(0, 0, 0, 0));
setLocationRelativeTo(null);
setResizable(false);
Title.setFont(new java.awt.Font("Tahoma", 1, 12));
Title.setText("The Covenant Farm Program");
Username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
UsernameActionPerformed(evt);
}
});
UsernameLabel.setText("Username:");
PasswordLabel.setText("Password:");
LoginButton.setText("Login");
LoginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
logincheck(evt);
}
});
FooterText.setText("You must be a Covenant Member to use this Program");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(UsernameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(PasswordLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(LoginButton)
.addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(FooterText))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(Title)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Title, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(UsernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addComponent(LoginButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(FooterText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
Thanks for the help :)