简体   繁体   中英

How to move components in a jframe to a certain spot

so im having trouble moving components to a certain part of the jframe, ive tried using BorderLayout and setBounds() but both of those don't seem to work.

    // 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);

I also have the some problem with a panel im using in a different part of my program

    // 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);

I'm trying to move the components horizontally

Then maybe you can use a Swing Border . An EmptyBorder will allow you to add extra space around the components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM