【问题标题】:JFrame without frame border, maximum button, minimum button and frame iconJFrame无边框、最大按钮、最小按钮和边框图标
【发布时间】:2010-01-06 08:03:58
【问题描述】:

我想创建一个无边框、最大按钮、最小按钮和边框图标。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    在您的JFrame 上致电setUndecorated(true)

    此方法只能在框架不可显示时调用(参见JavaDoc)。

    【讨论】:

    • 好的。我会设置一个可以接受的答案,它可以解决我的问题。谢谢!
    • @Peter:如何使框架无法显示?我创建了 JFrame 并使用 Netbeans 添加了一些控件。在主函数中我编码: MyJframe form = new MyJframe(); form.setUndecorated(true); form.setVisible(true);但它抛出异常
    • @Chan - MyJFrame 构造函数是否调用 pack()?这将使 JFrame 可显示
    • @Chan Pye:将setUndecorated(true); 放在构造函数MyJframe() 的开头。
    【解决方案2】:

    此代码说明了如何实现它。

    注意:setUndecorated(true);构造函数中的语句。

    当框架已经显示时,您不能取消装饰。

    public class MyFrame extends JFrame {
    
    private JPanel contentPane;
    private JTextField textField;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
    
    }
    /**
     * Create the frame.
     */
    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBackground(Color.ORANGE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    
    
        /* important Statement */
        setUndecorated(true);
    }
    

    }

    【讨论】:

      【解决方案3】:

      在构造函数中你可以输入代码 setUndecorated(true) 它会消失 Frame.

      例如: //这是构造函数

      public freak() {    
          super("Images");
          panel = new JPanel();
          ImageIcon image = new ImageIcon("C:\\Users\\shilu.shilu-PC\\Desktop\\2.jpg");
          label = new JLabel(image);
          add(panel);
          add(label);
      
          //Main thing is this one
          setUndecorated(true);
          //Just add this code in your constructor
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用java.awt.Window 类。 Window 类似于 JFrame,但没有边界。

        请注意,Window 类构造函数需要 Frame (java.awt.Frame) 作为参数,但您可以将其设置为 null。您还可以扩展 Window 类来自定义它,如下所示:

        public class MyWindow extends Window{
           public MyWindow(){
              super(null); // creates a window with no Frame as owner
              setBounds(x, y, width, height);
              setVisible(true);
           }
        }
        

        main 中,您可以创建MyWindow 的实例而不是Window

        public static void main (String[] args) {
            Window window = new MyWindow();
            // Other stuff in main
        }
        

        我希望这会有所帮助!

        【讨论】:

        • 注意 2 件事:1) 这个问题应用了swing 标签。 2) Swing 提供了一个JWindow。如果您删除或(最好)编辑您的答案,您可能会避免一些反对票。 ;)
        • Window 或 Jwindow 不支持某些鼠标事件,也没有任务栏控制。
        【解决方案5】:

        使用方法frame.getContentPane(); 此方法返回任何帧的内部内容。 但是您需要将其转换为 JPanel。 PrintUI 使用 JPanel 而不是 JFrame....

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-30
          • 2013-08-24
          • 1970-01-01
          • 2012-02-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多