簡體   English   中英

我可以在Java中創建一個沒有標題按鈕的窗口嗎?

[英]Can I create a Window in Java with no caption buttons?

是否可以在Java中創建某種具有框架和邊框但沒有標題按鈕(最小化,恢復,關閉)的Window對象。

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

  • 擁有平台渲染的邊框
  • 有一個標題欄
  • 沒有標題按鈕 如果需要,我會以編程方式處理窗口。
  • 使用默認值或System外觀

這是一個例子:

無標題窗口

這是關於

  1. 體面的如何創建半透明和形狀的Windows

  2. 使用復合邊框進行未修飾的JDialog ,然后您可以創建來自Native OS的similair或更好的邊框

  3. 使用GradientPaint創建JPanel (或JLabel#opaque(true)

  4. 或者(更好的non_focusable ==我的觀點) JLabel准備了Icon

  5. 添加到JPanel / JLabel 組件移動器/組件調整大小 (注意,不要永遠不要將這兩個代碼混合在一起)@camickr

  6. Alpha Transparency設置為在JPanel / JLabel繪畫,以獲得出色的look and feel

  7. 最簡單的方法就是JMenuBar

最簡潔的答案是不。

可能更長的答案是,但您需要調查JNI / JNA實現

試試這個小例子。 它將從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();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM