简体   繁体   English

在Java中隐藏标题栏上的按钮

[英]Hide buttons on title bar in Java

In Jinternal Frame(java), i want to hide max, min, close button (not disable max, min, close properties), but when I used this code : 在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);

It made all the buttons and the title bar disappeared (imagine the internalframe is a retangle, so only 3sides(down, left and right) visible). 它使所有按钮和标题栏消失了(假设内部框架是一个矩形,所以只有3面(上下左右)可见)。

So, how could I hide only 3buttons max, min and close without hiding all the title bar? 那么,如何隐藏最大,最小和关闭三个按钮而不隐藏所有标题栏? Thanks. 谢谢。

..want to hide max, min, close button ..想要隐藏最大,最小,关闭按钮

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);
            }
        });
    }
}

If you are using netbeans, its easy. 如果您使用的是netbeans,则很容易。 Simply create a new JInternalFrameForm by right clicking on any of your package. 只需右键单击任何一个包,即可创建一个新的JInternalFrameForm。

Simply add this JInternalFrameForm to any of you container (say a desktop pane for example). 只需将此JInternalFrameForm添加到您的任何容器中(例如,使用桌面窗格)。

My JInternalFrameForm name is internal1 and my desktop pane name is desk. 我的JInternalFrameForm名称是internal1,我的桌面窗格名称是desk。

// pseudo code : //伪代码:

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

Unfortunately, you cannot hide these buttons. 不幸的是,您无法隐藏这些按钮。 I tried this as well and was not successful. 我也尝试过,但没有成功。 However, there is a work around for this, which was to create a custom title bar. 但是,有一种解决方法,那就是创建一个自定义标题栏。 It is sorta tedious but it works. 这有点乏味,但可以。

The following steps should help you: 以下步骤应为您提供帮助:

1) invoke the setUndecorated(true) method. 1)调用setUndecorated(true)方法。 This will, unfortunately, remove the title bar completely but will allow you to do step 2. 不幸的是,这将完全删除标题栏,但允许您执行步骤2。

2) Then, create a class that will allow you to make a title bar using JFrame . 2)然后,创建一个类,使您可以使用JFrame制作标题栏。 Keep in mind that the window buttons appear on the right-hand side for Windows OS and on the left for Mac OS. 请记住,对于Windows OS,窗口按钮显示在右侧,对于Mac OS,窗口按钮显示在左侧。 The title text is also centered on Mac and left aligned on Windows. 标题文本也在Mac上居中,而在Windows上则保持对齐。

3) Use JLabel to display your title text and JButton to display the, minimize, maximize, and close buttons. 3)使用JLabel显示标题文本,并使用JButton显示,最小化,最大化和关闭按钮。 I would also recommend grouping the buttons and positioning the title text to make title bar look similar to the one that the OS on your computer displays 我还建议对按钮进行分组并放置标题文本,以使标题栏看起来类似于计算机上的操作系统显示的标题栏

4) [Optional] you can attach an ActionListener to the buttons to render the window behavior. 4)[可选]您可以将ActionListener附加到按钮上以呈现窗口行为。 This includes setState() for minimizing and setExtendedState for maximizing. 这包括用于最小化的setState()和用于最大化的setExtendedState Closing a window gives you two options System.exit(0) for applications and dispose() for applets 关闭窗口可为您提供两个选项: System.exit(0)用于应用程序, dispose()用于小程序

5) [Also optional] to disable the buttons, simply use the setEnabled(false) method. 5)[也可选]禁用按钮,只需使用setEnabled(false)方法。 In your case, to hide these buttons, you would use setVisible(false) 在您的情况下,要隐藏这些按钮,可以使用setVisible(false)

The following code snippet demonstrates this: 以下代码段演示了这一点:

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);
            }
        });
    }
}

This class constructs the title bar in the Windows appearance. 此类在Windows外观中构造标题栏。 Note that the Icon class that was used used here is not given, however, you can create a class that can display one. 请注意,此处未提供使用过的Icon类,但是,您可以创建一个可以显示一个的类。

The MouseEvent listeners are a must if you want window dragging. 如果要拖动窗口,必须使用MouseEvent侦听器。

The ActionEvent listener for the close button is also a requirement if you wish to close the window. 如果您想关闭窗口,则还需要关闭按钮的ActionEvent侦听器。 Hiding and disabling this button will naturally disable this feature 隐藏和禁用此按钮自然会禁用此功能

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM