Reputation: 830
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.
Upvotes: 0
Views: 61
Reputation: 23015
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).
Upvotes: 4