-3

I want to add a JLabel to my JPanel but it wont position it where I want it to go. I set my layoutManager to null and that didn't work.

I am trying to put the JLabel in the lower left hand corner of the JFrame. The KeyListener for the window is so that I can close the window with the escape key.

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Window extends Canvas {
    private static final long serialVersionUID = 1L;
    public Window(int width, int height, String title, Game game) {
        // Start: Code For Window Elements
        // Create Font That Is Used In Elements
        Font font = new Font("SansSerif", Font.PLAIN, 12);
        // JLabel For Author And Game Name
        JLabel name = new JLabel();
        name.setVisible(true);
        name.setText("\"Cube\" By: Toxicminibro");
        name.setFont(font);
        name.setForeground(Color.white);
        name.setBounds(0, 0, width, height);
        name.setLocation(5, 0);
        // End: Code For Window Elements
        // Create The JPanel, Set Its Attributes, And Add Elements To JPanel
        JPanel panel = new JPanel();
        panel.setVisible(true);
        panel.setLayout(null);
        panel.setBounds(0, 0, width, height);
        panel.setBackground(Color.DARK_GRAY);
        panel.add(name);
        panel.validate();
        // Create The Window And Set Its Attributes
        JFrame frame = new JFrame(title);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));
        frame.setLayout(null);
        frame.validate();
        frame.add(panel);
        frame.pack();
        // Window KeyListener
        KeyAdapter listener = new KeyAdapter() {
            @Override public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    frame.dispose();
                }
            }
        };
        frame.addKeyListener(listener);
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
DavidShefcik
  • 45
  • 2
  • 7
  • 4
    *`panel.setLayout(null);`* By adding this to your code you state: "get away will all the stuff of how to place and organize elements in a GUI, I am the ultimate master and know better than any one else!". Since this is rarely true I recommend to learn about LayoutMangers and how to use them bye going through the public tutorials: https://docs.oracle.com/javase/tutorial/uiswing/ – Timothy Truckle Jan 01 '17 at 17:19
  • 4
    Your code has many issues that suggests that you are not using Swing to its maximum advantage, including use of `null` layouts, use of KeyListener where Key Bindings is preferred, mixing Swing and AWT (Canvas) components, ... please do consider going through the Swing tutorials to learn how to better use this library. You can't lose by doing this. – Hovercraft Full Of Eels Jan 01 '17 at 17:26
  • Posible duplicaste of [*Layering Objects (That extend JComponet)…*](http://stackoverflow.com/q/13764942/230513) – trashgod Jan 02 '17 at 01:19

1 Answers1

-1

Here you give to the background (the container) of your label the same size as your Jframe that is why it covers the whole screen and the Alignment comes to the left-center by default, the solution :

//Set a new size to your Label (A size large enough to display the entire text)
name.setSize(AnotherWidth,AnotherHeight);
Bo Halim
  • 1,716
  • 2
  • 17
  • 23
  • 2
    Note that you should _avoid_ `setBounds` at all costs. It encourages you to work without a Layout Manager, and that never ends well for Swing programs. – byxor Jan 01 '17 at 18:04
  • 1
    I do not say the opposite but here I just put the point on the cause of the problem, but I thank you for this information ! – Bo Halim Jan 01 '17 at 18:13