0

I am playing around with Java GUIs, and I came across JLabel.setVerticalAlignment. I have set something up so that curlLeft and curlRight would go to the corners. However, it does not seem to have ny effect. Why is that so?

private void prepareGUI() throws IOException {

        mainFrame = new JFrame("Holy Bible");
        mainFrame.setSize(700, 500);
        mainFrame.setLayout(new GridLayout(1, 2));
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setIconImage(new ImageIcon(getClass().getResource("/assets/bible/textures/icon.png")).getImage());
        mainFrame.getContentPane().setBackground(Color.WHITE);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        mainFrame.addKeyListener(this);

        leftPanel = new JPanel();
        leftPanel.setBackground(Color.WHITE);
        leftPanel.setLayout(new FlowLayout());

        rightPanel = new JPanel();
        rightPanel.setBackground(Color.WHITE);
        rightPanel.setLayout(new FlowLayout());

        leftLabel = new JLabel("", JLabel.CENTER);
        leftLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
        leftPanel.add(leftLabel);

        rightLabel = new JLabel("", JLabel.CENTER);
        rightLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
        rightPanel.add(rightLabel);

        leftCurl = new JLabel();
        leftCurl.setHorizontalAlignment(JLabel.LEFT);
        leftCurl.setVerticalAlignment(JLabel.BOTTOM);
        leftCurl.setIcon(new ImageIcon(getClass().getResource("/assets/bible/textures/curlleft15.png")));
        leftPanel.add(leftCurl);

        rightCurl = new JLabel();
        rightCurl.setHorizontalAlignment(JLabel.RIGHT);
        rightCurl.setVerticalAlignment(JLabel.BOTTOM);
        rightCurl.setIcon(new ImageIcon(getClass().getResource("/assets/bible/textures/curlright15.png")));
        rightPanel.add(rightCurl, BorderLayout.SOUTH);

        mainFrame.add(leftPanel);
        mainFrame.add(rightPanel);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Maximizes frame
        mainFrame.setUndecorated(fullScreen);
        mainFrame.setVisible(true);
    }

All the variables needed are initialized at class level.

CaffeineToCode
  • 830
  • 3
  • 13
  • 25
  • I believe this is a possible duplicate of [this topic](http://stackoverflow.com/questions/7115065/jlabel-vertical-alignment-not-working-as-expected). – Voqus Jan 06 '15 at 02:13
  • what effect do you get, what effect do you want? – arcy Jan 06 '15 at 02:49
  • You are using a grid layout, the grid layout decides where objects go as they are added to the frame. however there is standard spacing between the grid and the outside of the frame that you can set to 0 – David Coler Jan 06 '15 at 02:55

1 Answers1

4

Your JLabels are not going to the corners because you are adding them to JPanels that have a FLowLayout. With FlowLayout your components don't occupy the 100% of the space of the JPanel, they only occupy the necessary.

I changed the 2 FlowLayouts to GridLayouts and now I can see the different orientations.

(Also, as your objective is learning how this works, I recommend you set a border on each component so you can see where their bounds are. This is quite good for understanding Swing's layout management).

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • 2
    I was wavering about whether to give this an up vote until I saw the last paragraph in brackets. That's sure worth an up vote on its own. – Andrew Thompson Jan 06 '15 at 05:03