Results 1 to 2 of 2
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Rep Power

    Default need help setting up GUI objects

    Well, what I have is a very nooby program that you input two numbers and it adds them together and outputs the sum. It works (horray) but what I am having trouble with now is moving the objects around in the JFrame so they are more organized. Here is my current code:

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    public class GUI2 extends JFrame {
        private double textField1;
        private double textField2;
        private JTextField field2;
        private JTextField field3;
        private JLabel label1;
        private JLabel label2;
        private JLabel resultLabel;
        private JButton button;
        private double resultNumber;
        public GUI2() {
            setLayout(new FlowLayout());
            label1 = new JLabel("Enter first number");
            field2 = new JTextField(20);
            label2 = new JLabel("Enter second number");
            field3 = new JTextField(20);
            button = new JButton("Calculate");
            resultLabel = new JLabel("");
            thehandler handler = new thehandler();
        private class thehandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                textField1 = Double.parseDouble(field2.getText());
                textField2 = Double.parseDouble(field3.getText());
                resultNumber = textField1 + textField2;
                resultLabel.setText("Result: " + resultNumber);
        public static void main(String args[]) {
            GUI2 theGui = new GUI2();
    Here is a picture of my program for reference:

    Basically, I want to use the setLocation(x, y) to move the objects around and line them up in a more orderly manner.

    I tried
    Java Code:
    label1.setLocation(50, 50);
    but it remained in its spot :(

    I think it might have something to do with the FlowLayout ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    To use setLocation or setBounds, your Container needs to use null layout, not FlowLayout, but having said that, using null and trying to directly set your components' placement is a bad idea. Instead use different layouts to see what works.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SignUp {
      private static void createAndShowUI() {
        JFrame frame = new JFrame("Sign Up Test");
        frame.getContentPane().add(new SuMainGUI().getComponent());
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
    class SuMainGUI {
      private JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
      SuLogInPanel logIn = new SuLogInPanel();
      public SuMainGUI() {
        mainPanel.add(logIn.getComponent(), BorderLayout.CENTER);
      public JComponent getComponent() {
        return mainPanel;
    class SuLogInPanel {
      private static final String SIGNUP = "Sign Up";
      private static final String LOGIN = "Log In";
      private JPanel mainPanel = new JPanel();
      private JTextField userNameField = new JTextField(15);
      private JPasswordField passwordField = new JPasswordField(15);
      private JButton signupBtn = new JButton(SIGNUP);
      private JButton loginBtn = new JButton(LOGIN);
      public SuLogInPanel() {
        JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
        JPanel innerPanel = new JPanel(new GridBagLayout());
        int eb = 25;
        innerPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc = calcGbc(0, 0, 1, 1, gbc.WEST, gbc.BOTH);
        innerPanel.add(new JLabel("User Name:", SwingConstants.LEFT), gbc);
        gbc = calcGbc(1, 0, 1, 1, gbc.EAST, gbc.HORIZONTAL);
        innerPanel.add(userNameField, gbc);
        gbc = calcGbc(0, 1, 1, 1, gbc.WEST, gbc.BOTH);
        innerPanel.add(new JLabel("Password:", SwingConstants.LEFT), gbc);
        gbc = calcGbc(1, 1, 1, 1, gbc.EAST, gbc.HORIZONTAL);
        innerPanel.add(passwordField, gbc);
        gbc = calcGbc(0, 2, 1, 1, gbc.WEST, gbc.BOTH);
        innerPanel.add(new JLabel(""), gbc);
        gbc = calcGbc(1, 2, 1, 1, gbc.EAST, gbc.HORIZONTAL);
        innerPanel.add(btnPanel, gbc);
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createTitledBorder("Log In"));
      public void addSignUpActionListener(ActionListener actionListener) {
      private GridBagConstraints calcGbc(int gridx, int gridy, int width, int height, int anchor,
          int fill) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.weightx = gridx;
        gbc.weighty = gridy;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.anchor = anchor;
        gbc.fill = fill;
        int insetGap = 5;
        if (anchor == GridBagConstraints.WEST) {
          gbc.insets = new Insets(insetGap, insetGap, insetGap, 4 * insetGap);
        } else {
          gbc.insets = new Insets(insetGap, insetGap, insetGap, insetGap);
        return gbc;
      public JComponent getComponent() {
        return mainPanel;
    Last edited by Fubarable; 05-07-2010 at 03:11 AM.

Similar Threads

  1. Moving objects - setting different speed
    By Antigol in forum Java 2D
    Replies: 2
    Last Post: 08-14-2009, 01:14 AM
  2. setting up javadocs?
    By PersonUsingEclipse in forum Eclipse
    Replies: 0
    Last Post: 01-29-2009, 03:42 AM
  3. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  4. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 09:55 PM
  5. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts