Aligning JLabels in BoxLayout

charlieshades picture charlieshades · Jul 22, 2012 · Viewed 12.4k times · Source

I've been searching around for days trying to find the answer to this, and I can't find out what's wrong. What I want to do is make it so the top JLabel (called display) align to the right and the bottom JLabel (called notice) to align to the left. Neither seems to want to do either. From what I've read, what I have should work, but it doesn't. Help?

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class Calculator {

    private static JButton clear, add, subtract, multiply, divide, equals, point, zero, one, two, three, four, five, six, seven, eight, nine;
    private static JLabel display, notice, blank1, blank2, blank3;
    private static JPanel mainPanel, buttonPanel, topLabel, bottomLabel;

    public static void goGUI() {

        JFrame frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(600,300));
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        frame.setContentPane(mainPanel);
        Border empty = BorderFactory.createEmptyBorder(10,10,10,10);
        mainPanel.setBorder(empty);

        buttonPanel = new JPanel(new GridLayout(5,4, 5,5));
        Border buttonBorder = BorderFactory.createEmptyBorder(10,0,10,0);
        buttonPanel.setBorder(buttonBorder);

        topLabel = new JPanel();
        bottomLabel = new JPanel();

        clear = new JButton("C");
        add = new JButton("+");
        subtract = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("/");
        equals = new JButton("=");
        point = new JButton(".");
        zero = new JButton("0");
        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");

        // Here I added ActionListeners to all the buttons...

        display = new JLabel("0");
        display.setAlignmentX(Component.RIGHT_ALIGNMENT);
        notice = new JLabel("*Maximum 19 digits - Order of operations not taken into account*");
        notice.setAlignmentX(Component.LEFT_ALIGNMENT);
        blank1 = new JLabel();
        blank2 = new JLabel();
        blank3 = new JLabel();

        buttonPanel.add(clear);
        buttonPanel.add(blank1);
        buttonPanel.add(blank2);
        buttonPanel.add(blank3);
        buttonPanel.add(seven);
        buttonPanel.add(eight);
        buttonPanel.add(nine);
        buttonPanel.add(divide);
        buttonPanel.add(four);
        buttonPanel.add(five);
        buttonPanel.add(six);
        buttonPanel.add(multiply);
        buttonPanel.add(one);
        buttonPanel.add(two);
        buttonPanel.add(three);
        buttonPanel.add(subtract);
        buttonPanel.add(zero);
        buttonPanel.add(point);
        buttonPanel.add(equals);
        buttonPanel.add(add);

        topLabel.add(display);
        bottomLabel.add(notice);

        mainPanel.add(topLabel);
        mainPanel.add(buttonPanel);
        mainPanel.add(bottomLabel);

        frame.pack();
        frame.setVisible(true);

    } //end goGUI

    //ActionListener classes went here...

    public static void main(String[] args) {

        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } 
        catch (Exception e) {}

        goGUI();

    } //end main
} //end Calculator

I removed all ActionListener stuff for clarity. But this is the layout that I can't fix.

Answer

Hovercraft Full Of Eels picture Hovercraft Full Of Eels · Jul 22, 2012

Consider having topLabel not use the default FlowLayout but rather something that makes its contents fill it up such as BorderLayout.

topLabel.setLayout(new BorderLayout());

Next make sure that you set the display's horizontal alignment, not its alignmentX to the right:

display.setHorizontalAlignment(SwingConstants.RIGHT);

Similar changes should be made for your notice JLabel and its container.