热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java隐藏标题栏_隐藏Java中标题栏上的按钮

不幸的是,你无法隐藏这些按钮。我也尝试了这个并没有成功。但是,有一个解决方法,即创建一个自定义标题栏。这有点乏味,但它确实有

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

以下步骤可以帮助您:

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

2)然后,创建一个允许您使用JFrame创建标题栏的类。请记住,窗口按钮显示在Windows操作系统的右侧,左侧显示在Mac OS中。标题文本也以Mac为中心,在Windows上左对齐。

3)使用JLabel显示标题文字,使用JButton显示,最小化,最大化和关闭按钮。

我还建议将按钮分组并定位标题文本,使标题栏看起来类似于计算机上显示的操作系统

4)[可选]您可以将ActionListener附加到按钮以呈现窗口行为。这包括setState()用于最小化和setExtendedState用于最大化。关闭窗口会为应用程序提供两个选项System.exit(0),为applet提供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侦听器。隐藏和禁用此按钮将自然禁用此功能

我希望这会有所帮助。



推荐阅读
author-avatar
老海文
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有