[英]setLocation on a JButton doesn't seem to work as expected
Here is my shortened version of my code:这是我的代码的缩短版本:
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
is added to a JFrame of sized using my_frame.setSize(400, 300);
card1
是使用加入到大小的一个JFrame my_frame.setSize(400, 300);
. .
The above code works as expected.上面的代码按预期工作。 But when I uncomment the comment piece of code, the
JButton
appears to the right side of the images( JLabel
s) instead of appearing below the images.但是当我取消注释代码的注释时,
JButton
出现在图像( JLabel
s)的右侧,而不是出现在图像下方。 What could be the problem?可能是什么问题呢?
Additional information:附加信息:
final static int TOPSWITCH = 150;
final static int LEFTSWITCH1 = 65 ;
final static int LEFTSWITCH2 = 155;
final static int LEFTSWITCH3 = 245;
switch1.jpg
is of dimensions 91 x 150 switch1.jpg
的尺寸为 91 x 150
Note: I want to do this without adding more JPanels
.注意:我想在不添加更多
JPanels
情况下执行此操作。
What could be the problem?
可能是什么问题呢?
Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的 PLAF 在不同的区域设置。 As such, they are not conducive to pixel perfect layout.
因此,它们不利于像素完美布局。 Instead use layout managers, or combinations of them along with layout padding and borders for white space .
而是使用布局管理器,或它们的组合以及用于空白的布局填充和边框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.