我可以在Java中创建一个没有标题按钮的窗口吗?

时间:2012-09-05 10:02:03

标签: java swing window jframe jwindow

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

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

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

以下是一个例子:

captionless window

3 个答案:

答案 0 :(得分:5)

这是关于

  1. 体面的How to Create Translucent and Shaped Windows

  2. 带有Compound Borders的未修饰JDialog,然后您可以创建来自Native OS的similair或更好的边框

  3. 使用JPanel

  4. 创建JLabel#opaque(true)(或GradientPaint
  5. 或(更好non_focusable ==我的观点)JLabel已准备好Icon

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

  7. 设置Alpha Transparency以便在JPanel / JLabel中进行绘画,以获得精彩的look and feel

  8. 最简单的方法是JMenuBar

答案 1 :(得分:4)

简短的回答是否定的。

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

答案 2 :(得分:2)

试试这个小例子。它将从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();
            }
        });
    }
}
相关问题