2

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

它使所有按钮和标题栏都消失了(想象内部框架是一个矩形,所以只有 3sides(下、左和右)可见)。

那么,如何在不隐藏所有标题栏的情况下仅隐藏 3 个最大、最小和关闭按钮?谢谢。

4

4 回答 4

3

..想要隐藏最大、最小、关闭按钮

移除控件

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);
            }
        });
    }
}
于 2012-02-20T17:19:53.473 回答
1

看到这个...... http://www.roseindia.net/java/example/java/swing/minimize-maximize.shtml

关闭按钮问题....

如何禁用(或隐藏)JFrame 上的关闭 (x) 按钮?

于 2012-02-20T15:24:29.420 回答
0

如果您使用的是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);
于 2014-10-10T10:22:10.417 回答
0

不幸的是,您无法隐藏这些按钮。我也试过这个,但没有成功。但是,有一个解决方法是创建一个自定义标题栏。这有点乏味,但它有效。

以下步骤应该可以帮助您:

1)调用setUndecorated(true)方法。不幸的是,这将完全删除标题栏,但允许您执行第 2 步。

2) 然后,创建一个允许您使用JFrame. 请记住,Windows 操作系统的窗口按钮出现在右侧,Mac 操作系统的窗口按钮出现在左侧。标题文本也在 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如果您希望关闭窗口,关闭按钮的侦听器也是必需的。隐藏和禁用此按钮自然会禁用此功能

我希望这有帮助。

于 2015-06-05T06:07:46.150 回答