【问题标题】:Can I create a Window in Java with no caption buttons?我可以在 Java 中创建一个没有标题按钮的窗口吗?
【发布时间】:2012-08-30 00:34:55
【问题描述】:

是否可以在 Java 中创建某种类型的 Window 对象,它具有框架和边框,但没有标题按钮(最小化、恢复、关闭)。

当然,我不能使用undecorated 设置。此外,窗口需要:

  • 有一个平台渲染的边框
  • 有一个标题栏
  • 没有字幕按钮。如果需要,我会以编程方式处理窗口。
  • 使用默认值,或 System 外观和感觉

这是一个例子:

【问题讨论】:

标签: java swing window jframe jwindow


【解决方案1】:

这是关于

  1. 不错的How to Create Translucent and Shaped Windows

  2. 未修饰 JDialogCompound Borders,然后您可以创建来自 Native OS 的类似或更好的边框

  3. GradientPaint创建JPanel(或JLabel#opaque(true)

  4. 或(更好的non_focusable == 我的观点)JLabel 准备好的Icon

  5. 将@camickr 添加到JPanel / JLabel Component Mover / Component Resize(注意,不要将这两个代码混合在一起)

  6. Alpha Transparency 设置为JPanel / JLabel 为伟大的look and feel 绘画

  7. 最简单的方法是JMenuBar

【讨论】:

    【解决方案2】:

    简短的回答是否定的。

    较长的答案可能是,但您需要调查 JNI/JNA 实现

    【讨论】:

    • ...总而言之...在大多数情况下可能不值得努力
    • 不确定(正如你提到的你是 Java1.3 恐龙,我来自 Java1.6.09)到 Java1.4 是否可以在不受顶级容器限制的情况下更改装饰类型 +1跨度>
    【解决方案3】:

    试试这个小例子。它将从 JFrame 中移除(不仅是禁用)最小化、最大化和关闭按钮。

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class Example {
    
        public void buildGUI() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame();
            frame.setResizable(false);
            removeButtons(frame);
            JPanel panel = new JPanel(new GridBagLayout());
            JButton button = new JButton("Exit");
            panel.add(button,new GridBagConstraints());
            frame.getContentPane().add(panel);
            frame.setSize(400,300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent a){
                    System.exit(0);
                }
            });
        }
    
        public void removeButtons(Component comp) {
            if(comp instanceof AbstractButton) {
                comp.getParent().remove(comp);
            }
            if (comp instanceof Container) {
                Component[] comps = ((Container)comp).getComponents();
                for(int x=0, y=comps.length; x<y; x++) {
                    removeButtons(comps[x]);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    new Example().buildGUI();
                }
            });
        }
    }
    

    【讨论】:

    • 不错的尝试。但是,窗口不可调整大小。此外,一个要求是它使用系统外观。更改为系统外观会破坏它。结果:左边是你的例子,同样的东西被修改为使用默认的外观:i.imgur.com/pFSRy.png
    猜你喜欢
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2011-04-02
    • 1970-01-01
    • 2013-06-11
    相关资源
    最近更新 更多