java - 如何将jframe中的组件移动到某个位置

标签 java eclipse swing jframe

所以我在将组件移动到 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);

最佳答案

I'm trying to move the components horizontally

那么也许你可以使用Swing BorderEmptyBorder 将允许您在组件周围添加额外的空间。

关于java - 如何将jframe中的组件移动到某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958944/

相关文章:

android - 为什么大多数 UI 框架都是单线程的?

java - 是否有一种干净的方法来检索 AWT 事件分派(dispatch)线程

java - 如果从下拉列表中进行选择,如何在选择后放置 if 语句

java - 函数不会改变java中变量的值?

java - IntelliJ无法识别camelCase吗?

java - 正则表达式中带有可选 block 的组编号

java - 具有多个参数的方法迭代数组

Java mysql 查询 ("SELECT * FROM movies"仅返回 1 行

java - Antlr - 控制流图

python - 如何处理 UnresolvedImport Eclipse (Python)