Java 仅在未装饰的JFrame上显示关闭按钮
我想知道是否有可能取消装饰一个JFrame,然后在顶部只显示close按钮或类似的东西。。任何帮助都将不胜感激。 详情:Java 仅在未装饰的JFrame上显示关闭按钮,java,swing,Java,Swing,我想知道是否有可能取消装饰一个JFrame,然后在顶部只显示close按钮或类似的东西。。任何帮助都将不胜感激。 详情: 实际上,我正在做一个项目,这是一个基于GUI的软件,我在其中使用了JdesktopPane,我使用了JInternalFrames,所以在顶部显示标题栏不仅仅适合我。有什么办法可以解决我的问题。基本上,一旦你删除了框架边框,你就要对它的功能负责,包括移动 本例基本上使用JPanel作为“标题”窗格的占位符,并使用BorderLayout布局标题和内容 实际的关闭按钮是一个简单
实际上,我正在做一个项目,这是一个基于GUI的软件,我在其中使用了JdesktopPane,我使用了JInternalFrames,所以在顶部显示标题栏不仅仅适合我。有什么办法可以解决我的问题。基本上,一旦你删除了框架边框,你就要对它的功能负责,包括移动 本例基本上使用
JPanel
作为“标题”窗格的占位符,并使用BorderLayout
布局标题和内容
实际的关闭按钮是一个简单的JButton
,它使用一些图像来表示关闭操作。显然,这只会在Windows上看起来正确,这些按钮是操作系统本身正常生成的…我们没有访问该层的权限
您可能应该为此创建一个自定义按钮。我可以创建,但它不会显示在右上角。可以在那里显示吗?是的,可能是使用布局管理器。好的,我不确定如何继续,但感谢您的帮助。@nsthethunderbolt:您可能能够在本文中利用内部框架图标。
public class TestUndecoratedFrame {
public static void main(String[] args) {
new TestUndecoratedFrame();
}
public TestUndecoratedFrame() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
TitlePane titlePane = new TitlePane();
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
((JComponent)frame.getContentPane()).setBorder(new LineBorder(Color.BLACK));
frame.add(titlePane, BorderLayout.NORTH);
frame.add(new JLabel("This is your content", JLabel.CENTER));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TitlePane extends JPanel {
public TitlePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.EAST;
add(new CloseControl(), gbc);
}
}
public class CloseControl extends JButton {
public CloseControl() {
setBorderPainted(false);
setContentAreaFilled(false);
setFocusPainted(false);
setOpaque(false);
setBorder(new EmptyBorder(0, 0, 8, 12));
try {
setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Highlighted.png"))));
setRolloverEnabled(true);
setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Normal.png"))));
} catch (IOException ex) {
Logger.getLogger(TestUndecoratedFrame.class.getName()).log(Level.SEVERE, null, ex);
}
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.getWindowAncestor(CloseControl.this).dispose();
}
});
}
}
}