不幸的是,您无法隐藏这些按钮。我也试过这个,但没有成功。但是,有一个解决方法是创建一个自定义标题栏。这有点乏味,但它有效。
以下步骤应该可以帮助您:
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
如果您希望关闭窗口,关闭按钮的侦听器也是必需的。隐藏和禁用此按钮自然会禁用此功能
我希望这有帮助。