我想用继承自JFrame icon的icon创建我自己的文件,并在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");


其他装饰性默认值列举为here

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

10-05 22:06