gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:15:26 25 4
gpt4 key购买 nike

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

最佳答案

诀窍在于 PLAFsetDefaultLookAndFeelDecorated(true) (Specifying Window Decorations)。

EG

Metal Windows

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

关于java - JFrame 上的关闭/最小化按钮的自定义设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com