簡體   English   中英

JButton 上的 setLocation 似乎沒有按預期工作

[英]setLocation on a JButton doesn't seem to work as expected

這是我的代碼的縮短版本:

JPanel card1 = new JPanel();

JLabel switch1 = new JLabel(new ImageIcon("switch1.jpg"));
switch1.setLocation(TOPSWITCH, LEFTSWITCH1);
JLabel switch2 = new JLabel(new ImageIcon("switch1.jpg"));
switch2.setLocation(TOPSWITCH, LEFTSWITCH2);
JLabel switch3 = new JLabel(new ImageIcon("switch1.jpg"));
switch3.setLocation(TOPSWITCH, LEFTSWITCH3);

card1.add(switch1);
card1.add(switch2);
card1.add(switch3);

card1.setBackground(Color.BLACK);

/* JButton goToRoom = new JButton("TEST");

goToRoom.setLocation(180, 270);

card1.add(goToRoom); */

card1是使用加入到大小的一個JFrame my_frame.setSize(400, 300); .

上面的代碼按預期工作。 但是當我取消注釋代碼的注釋時, JButton出現在圖像( JLabel s)的右側,而不是出現在圖像下方。 可能是什么問題呢?


附加信息:

final static int TOPSWITCH   = 150;

final static int LEFTSWITCH1 = 65 ;
final static int LEFTSWITCH2 = 155;
final static int LEFTSWITCH3 = 245;

switch1.jpg的尺寸為 91 x 150


注意:我想在不添加更多JPanels情況下執行此操作。

可能是什么問題呢?

  1. 想要設置明確的位置。
  2. 與布局管理器戰斗。

Java GUI 必須在不同的操作系統、屏幕尺寸、屏幕分辨率等上使用不同的 PLAF 在不同的區域設置。 因此,它們不利於像素完美布局。 而是使用布局管理器,或它們的組合以及用於空白的布局填充和邊框。

暫無
暫無

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

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