-2

I am trying to make the JLabel (point) to move location. It isn't working and when I add frame.setLayout(null) I just get a blank dark screen.

 JFrame  frame = new JFrame( "That snake game");
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel point = new JLabel("|eat this|");
        point.setForeground(Color.blue);

        frame.getContentPane().add(point);

        frame.getContentPane().setBackground(new Color(0,3,0));
        frame.setSize(400, 400);

        point.setLocation(340, 34);

        frame.setVisible(true);

How can I make the JLabel (point) to move location?

chickenman
  • 728
  • 2
  • 9
  • 29
  • 3
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) BTW - do you have a *question?* – Andrew Thompson Aug 02 '16 at 14:45
  • sorry if I was unclear. My question was how to make the Jlabel move. – chickenman Aug 02 '16 at 14:58
  • If you've read both the links you should already have an answer! – Andrew Thompson Aug 02 '16 at 15:16
  • That's true. Figuring how to do it is another headache... thanks though! – chickenman Aug 02 '16 at 15:28
  • *"Figuring how to do it is another headache..."* Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Aug 02 '16 at 21:44

2 Answers2

0

There is a very basic tutorial here on that could give you hints.

At least you need to

  • Set the container's layout manager to null : setLayout(null).
  • Call the Component class's setbounds method for each child component
  • Call the Component class repaint method.
  • Manage mouse events to move components (change components bounds)

your problems may be that you missed the repaint, and setBounds for label ?

However, as said by Andrew, swing is tricky and this swing layout subject is complex so this might not be the way to go at all.

May be you should redesign some parts of your game such that the graphic part does not interfere too much with gui elements For example -just an idea- restraining all your graphics interaction in a custom painted and fully managed component ? including the painting of a moving text if this is part of the game so yo uwill not move JLabel and disturb the layout part. (just an idea)

ARA
  • 1,296
  • 10
  • 18
0

I see that you want to create a Snake game. You are using the wrong API. For this, you should use the painting API of the Swing. Here is a Snake game that I have created: Snake game tutorial. JLabel and other components are used to create the user interface of a typical desktop application; they are not used to create games.

Now as for why your example did not work: You also need to set size of the JLabel. So if you are using setLocation(), you should also use setSize(), or use one setBounds() call.

package com.zetcode;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Demonstrating setLocation method
 * @Author Jan Bodnar
 * Website zetcode.com
 */

public class SetLocationEx extends JFrame {

    public SetLocationEx() {

        initUI();
    }

    private void initUI() {

        setLayout(null);

        JLabel lbl1 = new JLabel("JLabel");
        lbl1.setBorder(BorderFactory.createEtchedBorder());
        lbl1.setSize(80, 30);
        lbl1.setLocation(50, 100);

        add(lbl1);

        setSize(350, 300);
        setTitle("SetLocation example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            SetLocationEx ex = new SetLocationEx();
            ex.setVisible(true);
        });
    }
}

Screenshot:

Screenshot of the example

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77