JFrame上关闭/最小化按钮的自定义设计

我想应用我自己的关闭和最小化按钮。 有没有办法改变JFrame设计?

诀窍在于PLAF和setDefaultLookAndFeelDecorated(true) ( 指定窗口装饰 )。

例如

金属视窗

 import java.awt.BorderLayout; import javax.swing.*; public class FrameCloseButtonsByLookAndFeel { FrameCloseButtonsByLookAndFeel() { String[] names = { UIManager.getSystemLookAndFeelClassName(), UIManager.getCrossPlatformLookAndFeelClassName() }; for (String name : names) { try { UIManager.setLookAndFeel(name); } catch (Exception e) { e.printStackTrace(); } // very important to get the window decorations. JFrame.setDefaultLookAndFeelDecorated(true); JFrame f = new JFrame(UIManager.getLookAndFeel().getName()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel gui = new JPanel(new BorderLayout()); f.setContentPane(gui); JTree tree = new JTree(); tree.setVisibleRowCount(4); gui.add(tree, BorderLayout.LINE_START); gui.add(new JScrollPane(new JTextArea(3,15))); JToolBar toolbar = new JToolBar(); gui.add(toolbar, BorderLayout.PAGE_START); for (int ii=1; ii<5; ii++) { toolbar.add(new JButton("Button " + ii)); if (ii%2==0) { toolbar.addSeparator(); } } f.pack(); f.setLocationByPlatform(true); f.setVisible(true); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new FrameCloseButtonsByLookAndFeel(); } }); } } 

以为你是在追随JWindow

http://docs.oracle.com/javase/7/docs/api/javax/swing/JWindow.html

然后,您可以创建自己的按钮,这些按钮可以最小化/关闭窗口

我唯一能意识到的是将WindowListener添加到JFrame并处理该侦听器中的关闭事件。 您几乎可以制作任何内容,例如显示对话框甚至取消关闭JFrame

有关如何编写此类侦听器的更多详细信息,请参阅本教程 。

至于最小化:据我所知,没有办法控制或修改这种行为,它完全由操作系统控制。

更改最小化/关闭/最大化按钮方面的唯一方法是使用自定义LookAndFeel并设置JFrame.setDefaultLookAndFeelDecorated (true);

  1. 设置jframe undecorated
  2. 为每个按钮放置一个jlabel
  3. 为每个Btn自己的图标。
  4. 为每个jlabel放置mouseListeners并指定代码,例如, System.exit(0);/set ICONIFIED option