簡體   English   中英

更改JLabel的位置

[英]Changing a JLabel's position

經過大量研究,我只能使用標簽獲得該板的圖像。 現在,我無法更改其位置。 我嘗試了很多功能。 我需要什么確切的功能

public class Board extends JFrame {
    private ImageIcon image;
    private JLabel label;

    public Board() {
        image = new ImageIcon(getClass().getResource("board.png"));
        label = new JLabel(image);
        label.setLocation(200, 0);  //This is the one that I expected to do the thing
        add(label);
    }

    public static void main(String [] args) {
        Board b = new Board();
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.setVisible(true);
        b.setSize(1280, 1000);
    }
}

不要嘗試手動設置組件的位置。 Swing旨在與布局管理器一起使用。 閱讀Swing教程中有關使用布局管理器的部分,以獲取更多信息。

放置組件的一種方法是為組件設置Border 因此,您可以執行以下操作:

label.setBorder( new EmptyBorder(200, 0, 0, 0) );

本教程還包含有關How to Use Borders

首先:您不應該手動移動組件。 這應該留給layoutmanager。 但是你可以。 您面臨的基本問題是-或至少我認為如此-:您的電路板上仍然有一個layoutmanager設置,它將繼續對電路板進行布局,並且由於這種移動(並處理其大小),您將成為組件。 只需調用setLayout(null); 在放置標簽並指定尺寸之前,它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM