【问题标题】:Not able to set location of JLabel on a JPanel无法在 JPanel 上设置 JLabel 的位置
【发布时间】:2012-03-12 18:38:00
【问题描述】:

我已经为 JLabel 相对于 JPanel 设置了位置 (0,0)。但它出现在中心和顶部。我犯了什么错误?

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*;
public class Main extends JFrame 
{ 
private JPanel panel;
private JLabel label1;
public Main() 
{ 
    panel = new JPanel(); 
    panel.setBackground(Color.YELLOW); 

    ImageIcon icon1 = new ImageIcon("3.png"); 
    label1 = new JLabel(icon1); 
    label1.setLocation(0,0); 
    panel.add(label1);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().add(panel); 
    this.setSize(500,500); 
    this.setVisible(true); 

} 

public static void main (String[] args) {
    new Main(); 
} 
} 

【问题讨论】:

标签: java swing jpanel jlabel


【解决方案1】:

您不能设置Component 的绝对位置,而它的Container 具有LayoutManager;您必须先在JPanel 上致电setLayout(null),然后才能使用。

【讨论】:

  • 或者你应该在这里了解哪些布局可用Oracle layout tutorial
  • 为什么:LayoutManager 在布局组件内容时总是会覆盖位置(例如,当组件可见时)
  • 我想他以一种笨拙的方式说的是,使用布局管理器比使用绝对定位更好,就像@AlexR 的回答一样。在这种特殊情况下,我只是回答了实际提出的问题,但当然这不是最好的做事方式。
  • "call setLayout(null) .. before this will work." 告诉我null 布局确实的情况 工作(在 x-plat.、x-PLAF.、可调整大小的 UI 中)。
  • 好的,@AndrewThompson -- 在 GUI 构建器应用程序中,您可以通过拖动来定位组件。
【解决方案2】:

您不能这样做,因为默认布局管理器关心将元素呈现到您的面板中。

如果您真的想在您选择的位置设置元素,请说panel = new JPanel(null);

但是等一下!你真的想这样做吗?我相信你没有。我们(Java 程序员)通常使用布局管理器提供的服务在屏幕上呈现元素。你必须花一个小时来学习和理解这个概念,然后我向你保证,你会使用这种技术,而忘记在屏幕上手动渲染元素是一个非常糟糕的梦想。

【讨论】:

  • 基本上,我希望图像跟随鼠标位置。然后我必须将布局设置为null,对吗?
【解决方案3】:

通过写作

面板 = 新的 JPanel();

您强制面板使用内联添加元素的 FlowLayout。你可以使用

面板 = 新 JPanel(null);

如果您想通过禁用 FlowLayout 管理器来获得更多性能,但这样做您必须注意一些令人讨厌的事情,例如组件位置、尺寸等。我觉得用layout managers比较好

【讨论】:

  • 请建议,始终考虑将布局设置为 null 作为最后一步,这实际上可能永远不会发生。由于幕后有人正在努力使布局满足用户的每一个特定需求。为这个编辑过的信息 +1 :-)
  • 在您的个人资料中,您似乎想写计算机科学学生,但从学生开始就写了计算机。如果我是对的,请纠正:-)
  • 大声笑,我的意思是我是计算机科学专业的学生。
【解决方案4】:

将布局管理器设置为null 对我不起作用。试试这个:

// setLocation(0,0); //remove line.
panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // change from 'centered'

瞧! :) 我建议您查看新的RelativeLayout 经理。

【讨论】:

  • 不是 jpanel 默认的 flowlayout 吗?那我为什么要重新设置呢?
  • @JohnWatson:是的,是的,不,你没有。
  • 默认的 FlowLayout 是居中对齐的。请注意传递给上述构造函数的参数,该参数将其设置为左对齐。
  • @AndrewThompson 如果我需要图像跟随鼠标,应该使用什么布局?
  • "..需要图像跟随鼠标" 自定义绘画。布局(或缺少布局)无关紧要。问一个单独的问题,我将展示如何将自定义绘制的跟随鼠标的 BG 图像与放置在其顶部的组件结合起来。
【解决方案5】:

使用布局!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;

public class Main extends JFrame
{
    private JPanel panel;
    private JLabel label1;

    public Main() throws Exception
    {
        // Do use layouts (with padding & borders where needed)!
        panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.setBackground(Color.YELLOW);

        URL url = new URL("http://pscode.org/media/starzoom-thumb.gif");
        ImageIcon icon1 = new ImageIcon(url);

        label1 = new JLabel(icon1);
        // Don't use null layouts, setLocation or setBounds!
        //label1.setLocation(0,0);
        panel.add(label1);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().add(panel);
        setSize(400,200);
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Main();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

更新

对这个帖子的回复似乎有很多混乱,所以澄清一个问题:

  • JPanel 默认有一个FlowLayout
  • 它得到的FlowLayout 来自“无参数”构造函数,例如new FlowLayout()1
  • FlowLayout 的无参数构造函数产生..

    (1) ..具有居中对齐和默认 5 个单位的水平和垂直间隙的新 FlowLayout。

【讨论】:

  • 为什么默认为流式布局时需要设置面板​​布局?
  • 如果我想把图片放在面板的中心呢?
  • 1) 查看编辑。 2) 使用无参数构造函数(例如默认面板)。
【解决方案6】:
setLayout(null); //set your frame layout to null for abs. positioning
yourPanel = new JPanel(); // create your JPanel
yourPanel.setLayout(null); // set the layout null for this JPanel !

text1 = new JLabel("SB"); // create some stuff
text2 = new JLabel("BB");

yourPanel.add(text1); // add the stuff to the JPanel
yourPanel.add(text2);
yourPanel.getComponent(0).setBounds(0,0, 20, 20); // set your position of your elements inside your JPanel
yourPanel.setBorder(BorderFactory.createLineBorder(Color.black)); // set a testing border to help you position the elements better
yourPanel.setBounds(30, 200, 750, 220); // set the location of the JPanel

//给我回复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多