简体   繁体   English

如何移动卡住且不会移动的JLabel?

[英]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. 我尝试过setLocation和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. 好了,您将布局管理器设置为BorderLayout,因此您需要利用BorderLayout的优势。 Read the section from the Swing tutorial on How to Use BorderLayout for working examples. 阅读Swing教程中有关如何使用BorderLayout的部分, 获取工作示例。

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. 如果图像周围需要额外的空间,则可以在标签上添加Border Using the above link read the section from the tutorial on How to Use Borders . 使用上面的链接阅读教程中有关How to Use Borders You can use an EmptyBorder to give the extra space. 您可以使用EmptyBorder来提供额外的空间。

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: 结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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