首页 > 解决方案 > 如何在 JFrame 标题栏上放置一个按钮

问题描述

我想在 JFrame 标题栏上放置一个按钮,如下图所示。

预期窗口



当我使用 button.setBounds() 设置按钮的边界时,按钮隐藏在标题栏下方,如下所示。

窗口出现


我在下面给出了我尝试过的代码。

public class SetBoundsTest {
       public static void main(String arg[]) {
          JFrame frame = new JFrame("Test Frame");
          frame.setSize(500, 250);
          // Setting layout as null
          frame.setLayout(null);
          
          // Creating Button
          JButton button = new JButton("Test");
          // Setting position and size of a button
          button.setBounds(150,-20,120,40);

          button.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3));
          button.setBackground(Color.MAGENTA);
          frame.add(button);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }

任何帮助是极大的赞赏。

标签: javaswinguser-interfacejframesetbounds

解决方案


您可以像这样将框架设置为未装饰

frame.setUndecorated(true);

注意:在前面加上这个语句frame.setVisiblle(true);


推荐阅读