簡體   English   中英

在Java中隱藏標題欄上的按鈕

[英]Hide buttons on title bar in Java

在Jinternal Frame(java)中,我想隱藏max,min,close按鈕(不禁用max,min,close屬性),但是當我使用此代碼時:

javax.swing.plaf.InternalFrameUI ifu= jif.getUI(); //jif : finternalframe//
((javax.swing.plaf.basic.BasicInternalFrameUI)ifu).setNorthPane(null);

它使所有按鈕和標題欄消失了(假設內部框架是一個矩形,所以只有3面(上下左右)可見)。

那么,如何隱藏最大,最小和關閉三個按鈕而不隱藏所有標題欄? 謝謝。

..想要隱藏最大,最小,關閉按鈕

RemoveControls

import java.awt.*;
import javax.swing.*;

class RemoveControls {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel p = new JPanel(new GridLayout());
                p.setPreferredSize(new Dimension(300,120));

                JDesktopPane dtp = new JDesktopPane();
                p.add(dtp);

                JInternalFrame jif = new JInternalFrame("JIF",
                    false, //resizable
                    false, //closable
                    false, //maximizable
                    false); //iconifiable
                jif.setVisible(true);
                jif.setSize(200,100);
                dtp.add(jif);

                JOptionPane.showMessageDialog(null, p);
            }
        });
    }
}

如果您使用的是netbeans,則很容易。 只需右鍵單擊任何一個包,即可創建一個新的JInternalFrameForm。

只需將此JInternalFrameForm添加到您的任何容器中(例如,使用桌面窗格)。

我的JInternalFrameForm名稱是internal1,我的桌面窗格名稱是desk。

//偽代碼:

    InternalFrame mboxFrame = new internal1();
    mboxFrame.setResizable(false);
    mboxFrame.setSize(desk.getWidth(), desk.getHeight());
    mboxFrame.setLocation(0, 0);
    mboxFrame.setVisible(true);
    desk.add(mboxFrame);

不幸的是,您無法隱藏這些按鈕。 我也嘗試過,但沒有成功。 但是,有一種解決方法,那就是創建一個自定義標題欄。 這有點乏味,但可以。

以下步驟應為您提供幫助:

1)調用setUndecorated(true)方法。 不幸的是,這將完全刪除標題欄,但允許您執行步驟2。

2)然后,創建一個類,使您可以使用JFrame制作標題欄。 請記住,對於Windows OS,窗口按鈕顯示在右側,對於Mac OS,窗口按鈕顯示在左側。 標題文本也在Mac上居中,而在Windows上則保持對齊。

3)使用JLabel顯示標題文本,並使用JButton顯示,最小化,最大化和關閉按鈕。 我還建議對按鈕進行分組並放置標題文本,以使標題欄看起來類似於計算機上的操作系統顯示的標題欄

4)[可選]您可以將ActionListener附加到按鈕上以呈現窗口行為。 這包括用於最小化的setState()和用於最大化的setExtendedState 關閉窗口可為您提供兩個選項: System.exit(0)用於應用程序, dispose()用於小程序

5)[也可選]禁用按鈕,只需使用setEnabled(false)方法。 在您的情況下,要隱藏這些按鈕,可以使用setVisible(false)

以下代碼段演示了這一點:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class TitleBar extends JPanel
{
    private JLabel titleLabel; //create this to hold the title text

    JFrame frame    = new JFrame();

    int pX, pY; //used for window dragging

    private JButton closeBtn, minBtn, maxBtn; //create these for the window buttons

    public TitleBar(String title)
    {
        setPreferredSize(new Dimension(1000, 28));

        titleLabel = new JLabel(title);
        titleLabel.setOpaque(true);

        JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); //define this to hold the title text and window buttons

        closeBtn    = new JButton(); //define the Close button
        closeBtn.setBorderPainted(false);

        //set the icons for the states
        closeBtn.setIcon(new WindowButtonIcon().new CloseIcon(true, false));
        closeBtn.setPressedIcon(new WindowButtonIcon().new CloseIcon(true, true));
        closeBtn.setDisabledIcon(new WindowButtonIcon().new CloseIcon(false, false));

        //Apply the more fine adjustments
        closeBtn.setPreferredSize(new Dimension(17, 17));
        closeBtn.setRolloverEnabled(false);
        closeBtn.setFocusPainted(false);

        //Attach this action listener to render the "close window" function
        closeBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        minBtn      = new JButton(); // define the Minimize button
        minBtn.setBorderPainted(false);

        //set the icons for the selection states
        minBtn.setIcon(new WindowButtonIcon().new MinimizeIcon(true, false));
        minBtn.setPressedIcon(new WindowButtonIcon().new MinimizeIcon(true, true));
        minBtn.setDisabledIcon(new WindowButtonIcon().new MinimizeIcon(false, false));

        //Apply the more fine adjustments
        minBtn.setPreferredSize(new Dimension(17, 17));
        minBtn.setRolloverEnabled(false);
        minBtn.setFocusPainted(false);

        maxBtn      = new JButton(); //define the Maximize button
        maxBtn.setBorderPainted(false);

        //set the icons for the selection states
        maxBtn.setIcon(new WindowButtonIcon().new MaximizeIcon(true, false));
        maxBtn.setPressedIcon(new WindowButtonIcon().new MaximizeIcon(true, true));
        maxBtn.setDisabledIcon(new WindowButtonIcon().new MaximizeIcon(false, false));

        //Apply the more fine adjustments
        maxBtn.setPreferredSize(new Dimension(17, 17));
        maxBtn.setRolloverEnabled(false);
        maxBtn.setFocusPainted(false);

        //This JPanel will set up the title text and window buttons
        controls.setBackground(null);
        controls.add(minBtn);
        controls.add(Box.createRigidArea(new Dimension(4, 0)));
        controls.add(maxBtn);
        controls.add(Box.createRigidArea(new Dimension(4, 0)));
        controls.add(closeBtn);

        setLayout(new FlowLayout(FlowLayout.LEFT, 0, 3));

        //construct the custom title bar
        add(titleLabel);
        add(Box.createRigidArea(new Dimension(790, 0)));
        add(controls);

        //These render window dragging
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)
            {
                // Get x,y and store them
                pX = me.getX();
                pY = me.getY();
            }

            public void mouseDragged(MouseEvent me)
            {
                frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY);
            }
        });

        addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent me)
            {
                frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY);
            }
        });
    }
}

此類在Windows外觀中構造標題欄。 請注意,此處未提供使用過的Icon類,但是,您可以創建一個可以顯示一個的類。

如果要拖動窗口,必須使用MouseEvent偵聽器。

如果您想關閉窗口,則還需要關閉按鈕的ActionEvent偵聽器。 隱藏和禁用此按鈕自然會禁用此功能

我希望這有幫助。

暫無
暫無

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

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