Search code examples
javauser-interfacelabelalignmentawt

Improper Alignment Error with Java AWT. Cannot get my label to move


I have put a label into my frame, but it refuses to move. SetBounds() is not working, and I get an improper alignment error if I put any argument past "Result" below that isn't 0, 1, or 2, none of which put it in the correct place. Here's where I declare the Label:

Label result = new Label("Result.", 3);

Here's the SetBounds statement:

result.setBounds(0, 1500, 100, 20);

This program I am writing, I simply just want to have the user input 2 numbers, add them, and print the result using GUI components. The result is the label which refuses to change. The code of the entire program is below, and the program is still not done yet, but if you compile it, result is always stuck to the left and I want it to be at the same level as the TextFields. This problem is actually happening with the other labels, Help1, and Help2. Please don't tell me I have to use swing! I dislike swing.

I have yet to change the event to where it adds the user inputs. I copied the event from a previous program.

The code: (Sorry for no comments, but it's not a huge program)

import java.awt.*;
import java.awt.event.*;

public class MouseClick {
    TextField number1;
    TextField number2;

    public static void main(String[] args) {
        MouseClick MC = new MouseClick();
    }

    public MouseClick() {
        Frame f = new Frame("Addition Time!");
        Button button = new Button("Click Here To Add The Two Numbers.");
        button.setBounds(175, 250, 230, 30);
        button.addMouseListener(new MyMouseListener());
        f.add(button);
        Label help1 = new Label("Enter the first number below.");
        Label help2 = new Label("Enter the second number below.");
        Label exprsn1 = new Label("+", 0);
        Label exprsn2 = new Label("=", 0);
        Label result = new Label("Result.", 3);
        number1 = new TextField("TextField1", 100);
        number2 = new TextField("TextField2", 100);
        help1.setBounds(50, 80, 150, 20);
        help2.setBounds(250, 80, 150, 20);
        exprsn1.setBounds(00, 80, 30, 30);
        exprsn2.setBounds(00, 80, 30, 30);
        number1.setBounds(50, 100, 100, 20);
        number2.setBounds(250, 100, 100, 20);
        result.setBounds(0, 1500, 100, 20);
        f.add(number1);
        f.add(number2);
        f.add(help1);
        f.add(help2);
        f.add(result);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        f.setSize(600, 300);
        f.setVisible(true);
    }

    public class MyMouseListener extends MouseAdapter {
        public void mouseClicked(MouseEvent me) {
            String S = number1.getText();
            number2.setText(S);
        }
    }
}

Solution

  • The error is because 3 is not an allowed value for parameter "alignment" in the constructor

    Label(String text, int alignment) 
    

    Are you getting confused with TextField() which has a similar constructor?

    TextField(String text, int columns)
    

    The reason the label is not appearing in the correct location is because you've not specified your using null layout explicitly. You need this line:

    public MouseClick() {
        Frame f = new Frame("Addition Time!");
        f.setLayout(null);  // add this line