简体   繁体   中英

How do I move a JLabel that is stuck and won't move?

Here is the code, when the image is put onto the background it just stays stuck to the left wall no matter what code I add to move it. I have tried setLocation and setBounds. All I want to do is move the image to the bottom left but, not fully on the walls of the frame.

JFrame window = new JFrame();
window.setSize(800,480);
window.setTitle("Battle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
JLabel background = new JLabel();
ImageIcon icon = new ImageIcon("background2.png");     
background.setIcon(icon);
background.setLayout(new BorderLayout());
window.setContentPane(background);
JLabel p1 = new JLabel();
p1 = a.getImage();
background.add(p1);
p1.setLocation(500,500);
p1.setVisible(true);
window.setVisible(true);
window.setLayout(new BorderLayout());
...
window.setContentPane(background);

The first statement doesn't do anything because the second statement replaces the content pane of the frame, to the layout manager will be whatever layout manager you set for the "background" component.

All I want to do is move the image to the bottom left,

Well you set the layout manager to a BorderLayout so you need to take advantage of the BorderLayout. Read the section from the Swing tutorial on How to Use BorderLayout for working examples.

So the first thing you need to do is specify the proper constraint to have the component displayed at the bottom:

//background.add(p1); // defaults to BorderLayout.CENTER if no constraint is specified
background.add(p1, BorderLayout.PAGE_END);

Test this and the image will be at the bottom, but it is still centered.

So now you need to set a property on the label to tell the label to paint itself left aligned:

label.setHorizontalAlignment(JLabel.LEFT);

not fully on the walls of the frame

If you need extra space around the image then you can add a Border to the label. Using the above link read the section from the tutorial on How to Use Borders . You can use an EmptyBorder to give the extra space.

One way to do this would be to add extra "left panel" container so we can position your label on the left and bottom

public static void main(String[] args) {
    JFrame window = new JFrame();
    window.setSize(800, 480);
    window.setTitle("Battle");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel background = new JLabel();
    background.setBackground(Color.gray);
    background.setLayout(new BorderLayout());
    background.setOpaque(true);
    window.setContentPane(background);

    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout());
    background.add(leftPanel, BorderLayout.WEST); // stick our left side bard to the left side of frame

    JLabel p1 = new JLabel();
    p1.setText("Im here");
    p1.setLocation(500, 500);
    p1.setVisible(true);
    p1.setBackground(Color.black);
    p1.setOpaque(true);
    leftPanel.add(p1, BorderLayout.SOUTH); // add our label to the bottom
    window.setVisible(true);
}

Results: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM