如何使用inheritance自JFrame图标的图标创建自己的文件,我设置它,在java和我自己的文件中使用FileOutputStream和ObjectOutputStream

我想创建自己的文件,其图标inheritance自JFrame图标,我设置它,在java和我自己的文件中使用FileOutputStream和ObjectOutputStream

try { ObjectOutputStream oos; //I create own file with own extension in drive D: FileOutputStream fos = new FileOutputStream("D:/myFile.ckl"); oos = new ObjectOutputStream(fos); //Write Document in JTextPane to File oos.writeObject(jTextPane.getStyledDocument()); oos.close(); fos.close(); } catch (Exception exp) { JOptionPane.showMessageDialog(null, "" + exp.getStackTrace()); } 

先谢谢你

@David是正确的,主机平台拥有JFrame装饰,但您可以利用JInternalFrame图标,这通常概括了平台的图标。 例如,

 private static final Icon ICON = (Icon) UIManager.get("InternalFrame.closeIcon"); 

其他装饰默认值在此列举。

SSCCE :

在此处输入图像描述

 import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; /** @see http://stackoverflow.com/a/10360374/230513 */ public class InternalFrameIcons extends JPanel { public InternalFrameIcons() { this.setLayout(new GridLayout(0, 1, 5, 5)); this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); this.add(createLabel("InternalFrame.closeIcon")); this.add(createLabel("InternalFrame.maximizeIcon")); this.add(createLabel("InternalFrame.minimizeIcon")); } private JLabel createLabel(String name) { Icon icon = (Icon) UIManager.get(name); JLabel label = new JLabel(name, icon, JLabel.CENTER); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); return label; } private void display() { JFrame f = new JFrame("InternalFrameIcons"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new InternalFrameIcons().display(); } }); } } 

操作系统选择显示文件的图标。 将数据写入文件并为其提供文件扩展名(在本例中为’ckl’)是您的工作,但是文件最终会根据操作系统确定给出的图标。

可以在某些文件中嵌入图标(许多可执行文件通常都有自己的图标),但最终还是由操作系统决定。