我还是 Java 的新手,我很喜欢玩弄它,所以请在这件事上取笑我。我知道布局管理器,但我对摆动的机制很感兴趣,所以我使用的是绝对定位。任何见解将不胜感激。
因此,当我将 JLabel 添加到 JPanel 时,我注意到了一个令人讨厌的行为。例如,如果我这样设置:
JFrame frame=new JFrame("Test Frame");
frame.setSize(800,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable (false);
JPanel conPane=new JPanel();
conPane.setLayout(null);
frame.setContentPane(conPane);
conPane.setBounds(0,0,1600,1000);
conTP.setFocusable(true);
frame.setVisible(true);
所以现在我移动 conPane 面板......
conPane.setBounds(-200,-200,1600,1000);
然后制作一个新的 JLabel....
ImageIcon ico=new ImageIcon("directory/testimage.gif");
JLabel myLabel=new JLabel(ico);myLabel.setLayout(null);
myLabel.setBounds(300,300,200,200);
并将其添加到 conPane...
conPane.add(myLabel);
并且它将 conPane 重置回它的起始位置。基本上是在未经我许可的情况下做 setBounds(0,0,1600,1000) ;
如果我移动标签、更改它的图标或几乎做任何涉及更改 conPane 的子级的事情,也会发生这种情况。
我怎样才能阻止它?