0

How do I make my textfields and button centered?

Edit: Updated my post with SSCE. Hopefully that helps.

By the way, I want the left image to look like the right.

package pkg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;

public class Game {

private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel();
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");

public Game() {
    panel.add(username);
    panel.add(password);
    panel.add(login);
    panel.add(status);

    game.add(logout);
    frame.add(panel);
    frame.setSize(806, 553);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public static void main(String[] args) {
    new Game();
}

public void loadGame() {
    frame.remove(panel);
    frame.revalidate();
    frame.add(game);
    frame.revalidate();

    logout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.remove(game);
            frame.revalidate();
            frame.add(panel);
            frame.revalidate();
            try {
                Client.socketOut.writeUTF("logout");
                Client.socketOut.writeUTF(Client.username);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}
}

What I want it to look like: enter image description here

I've tried to use many different layout styles but I can't seem to make it work...

user3130731
  • 655
  • 2
  • 9
  • 12
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). See also [this answer](http://stackoverflow.com/a/7181197/418556).. – Andrew Thompson Dec 29 '13 at 15:10
  • In your image you have 2, what is the one you want to look like, left or right? – nachokk Dec 29 '13 at 15:10
  • I want the left image to look like the right. – user3130731 Dec 29 '13 at 15:12
  • 1
    Take a loo to [How to use BoxLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html) trail. Note there's no call to [frame.pack()](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack%28%29) method and you should also take a look to [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – dic19 Dec 29 '13 at 15:17
  • 1
    Don't use frame.remove() and frame.add() to replace an entire panel on a frame. Instead you should use `CardLayout`. The tutorial has an example for this. – camickr Dec 29 '13 at 16:30

1 Answers1

3

enter image description here

import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import javax.swing.*;
    import javax.swing.border.*;

    public class Game {

    private static JPanel panel = new JPanel();
    public static JTextField username = new JTextField(20);
    public static JPasswordField password = new JPasswordField(20);
    JButton login = new JButton("Login");
    JLabel status = new JLabel();
    private static JPanel game = new JPanel(new FlowLayout(FlowLayout.CENTER));
    private JButton logout = new JButton("Logout");
    private static JFrame frame = new JFrame("RuneShadows");

    public Game() {
        panel.setLayout(new GridLayout(0,1,15,15));
        panel.setBorder(new EmptyBorder(50,100,50,100));
        panel.add(username);
        panel.add(password);
        panel.add(status);

        JPanel logoutConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER));
        logoutConstrain.add(logout);
        panel.add(logoutConstrain);
        frame.setLayout(new GridBagLayout());
        frame.add(panel);
        //frame.setSize(806, 553);  // forget this nonsense, instead..
        frame.pack();  // best!
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Game();
    }

    public void loadGame() {
        frame.remove(panel);
        frame.revalidate();
        frame.add(game);
        frame.revalidate();

        logout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.remove(game);
                frame.revalidate();
                frame.add(panel);
                frame.revalidate();
            }
        });
    }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433