Hide buttons on title bar in Java

天大地大妈咪最大 提交于 2020-01-04 09:21:28

问题


In Jinternal Frame(java), i want to hide max, min, close button (not disable max, min, close properties), but when I used this code :

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).

So, how could I hide only 3buttons max, min and close without hiding all the title bar? Thanks.


回答1:


..want to hide max, min, close button

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



回答2:


See this... http://www.roseindia.net/java/example/java/swing/minimize-maximize.shtml

Close Button Problem....

How to disable (or hide) the close (x) button on a JFrame?




回答3:


If you are using netbeans, its easy. Simply create a new JInternalFrameForm by right clicking on any of your package.

Simply add this JInternalFrameForm to any of you container (say a desktop pane for example).

My JInternalFrameForm name is internal1 and my desktop pane name is 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);



回答4:


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. This will, unfortunately, remove the title bar completely but will allow you to do step 2.

2) Then, create a class that will allow you to make a title bar using JFrame. Keep in mind that the window buttons appear on the right-hand side for Windows OS and on the left for Mac OS. The title text is also centered on Mac and left aligned on Windows.

3) Use JLabel to display your title text and JButton to display the, minimize, maximize, and close buttons. 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. This includes setState() for minimizing and setExtendedState for maximizing. Closing a window gives you two options System.exit(0) for applications and dispose() for applets

5) [Also optional] to disable the buttons, simply use the setEnabled(false) method. In your case, to hide these buttons, you would use 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. Note that the Icon class that was used used here is not given, however, you can create a class that can display one.

The MouseEvent listeners are a must if you want window dragging.

The ActionEvent listener for the close button is also a requirement if you wish to close the window. Hiding and disabling this button will naturally disable this feature

I hope this helps.



来源:https://stackoverflow.com/questions/9363529/hide-buttons-on-title-bar-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!