[英]How to move components in a jframe to a certain spot
因此,在將組件移動到jframe的特定部分時遇到麻煩,我嘗試使用BorderLayout和setBounds(),但兩者似乎都不起作用。
// sets the start button
start = new JButton(imgStart);
start.setPreferredSize(new Dimension(150, 55));
start.setFocusable(false);
start.addActionListener(this);
// sets the controls button
controls = new JButton(imgControl);
controls.setPreferredSize(new Dimension(150, 55));
controls.setFocusable(false);
controls.addActionListener(this);
controls.setBounds(151, 56, 0, 0);
JFrame frame = new JFrame();
frame.setContentPane(this);
frame.add(start);
frame.add(controls, BorderLayout.WEST);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground.getIconWidth(), imgBackground.getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
我在程序的不同部分中使用的面板即時消息也存在一些問題
// creates a panel called Info and and adds components to them
JPanel Info = new JPanel();
//Info.setBorder(BorderFactory.createLineBorder(Color.black, 5));
Info.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10));
//Info.setBounds(290, 180, -10, 10);
Info.setOpaque(false);
Info.add(lblTime);
Info.add(lblTimer);
Info.add(lblScore);
Info.add(lblShowScore);
addKeyListener(this);
setFocusable(true);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setContentPane(this);
frame.add(Info, BorderLayout.SOUTH);
frame.add(lblLevel);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
我正在嘗試水平移動組件
然后,也許您可以使用Swing Border 。 一個EmptyBorder
將允許您在組件周圍添加額外的空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.