java 如何在 JFrame 中设置图像的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16660132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:35:02  来源:igfitidea点击:

How set location of an image in a JFrame

javaswingjframelayout-manager

提问by java

I'm trying to set the the location of an image in a JFrame. I thought label.setLocation(100, 100);would set the location of the image to 100, 100 (top left corner), but it doesn't seem to do anything no matter were I put it. I even tried panal.setLocation(100, 100). Both do nothing, I get no errorsand the image does appearsbut at 0, 0. What am I doning wrong? Here's my code:

我正在尝试在 JFrame 中设置图像的位置。我以为label.setLocation(100, 100); 会将图像的位置设置为 100, 100(左上角),但无论我放置它似乎都没有任何作用。我什至试过 panal.setLocation(100, 100)。两者都什么都不做,我没有收到任何错误图像确实出现但在 0, 0。我做错了什么?这是我的代码:

import javax.swing.*;  

public class DisplayImage {  
    public DisplayImage() {  
        JFrame frame = new JFrame("Display Image");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        JPanel panel = (JPanel)frame.getContentPane();  

        JLabel label = new JLabel();  
        label.setIcon(new ImageIcon("src/img/school.png"));
        label.setLocation(100, 100);
        panel.add(label);  

        frame.setLocationRelativeTo(null);  
        frame.pack();  
        frame.setVisible(true);  
    }  

    public static void main (String args[]) {  
        SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                new DisplayImage();  
            }  
        });  
    }  
}  

回答by camickr

By default a JFrame uses a BorderLayout. When you add your label to the frame the label is added to the CENTER of the BorderLayout. The layout manager will override the location.

默认情况下,JFrame 使用 BorderLayout。当您将标签添加到框架时,标签将添加到 BorderLayout 的 CENTER。布局管理器将覆盖该位置。

One simple solution is to add an EmptyBorder to the label with the top/left insets being 100. Then instead of add the label to the center you would add the label to the NORTH. The code would be something like:

一个简单的解决方案是向标签添加一个 EmptyBorder,顶部/左侧的插图为 100。然后将标签添加到 NORTH,而不是将标签添加到中心。代码类似于:

label.setBorder( new EmptyBorder(...) );
panel.add(label, BorderLayout.SOUTH);

As a general rule you should not be trying to specify exact location of a component. Let the layout managers to their jobs.

作为一般规则,您不应尝试指定组件的确切位置。让布局经理开始他们的工作。

回答by MadProgrammer

The JFrame, by default, uses a BorderLayoutas it's layout manager. This will override any settings you supply to the setLocationmethod.

JFrame默认情况下,采用的是BorderLayout因为它的布局管理器。这将覆盖您提供给该setLocation方法的任何设置。

You have a number of options...

你有很多选择...

  • Use something like JLayeredPane, which does not, but default, have layout manager of it's own set by default. See How to use layered panesfor more details
  • Create a custom component capable of renderering the image where you want it. Check out Performing Custom Paintingfor more details
  • Create your own layout manager that performs the operations you want...
  • 使用类似的东西JLayeredPane,它没有,但默认情况下,默认设置它自己的布局管理器。有关更多详细信息,请参阅如何使用分层窗格
  • 创建一个能够在您想要的位置渲染图像的自定义组件。查看执行自定义绘画以获取更多详细信息
  • 创建您自己的布局管理器来执行您想要的操作...

回答by 7stud

This works for me:

这对我有用:

import java.awt.Dimension;
import javax.swing.*;  

public class DisplayImage {  
    public DisplayImage() {  
        JFrame frame = new JFrame("Display Image");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        JPanel panel = (JPanel)frame.getContentPane();  
        panel.setLayout(null);

        JLabel label = new JLabel();  
        label.setIcon(new ImageIcon("rails.png"));
        panel.add(label);
        Dimension size = label.getPreferredSize();
        label.setBounds(100, 100, size.width, size.height);

        frame.setSize(300, 200);
        frame.setVisible(true);  
    }  

    public static void main (String args[]) {  
        SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                new DisplayImage();  
            }  
        });  
    }  
}  

But you should read this:

但你应该阅读这个:

http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html