2

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

前もって感謝します

4

2 に答える 2

4

@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();
            }
        });
    }
}
于 2012-04-28T03:40:26.440 に答える
2

オペレーティングシステムは、ファイルが表示されるアイコンを選択します。ファイルにデータを書き込んでファイル拡張子(この場合は「ckl」)を付けるのはあなたの仕事ですが、どのアイコンが付けられるかはOS次第です。

一部のファイルにアイコンを埋め込むことは可能ですが(多くの実行可能ファイルには独自のアイコンがあることがよくあります)、最終的にはOS次第です。

于 2012-04-28T01:51:19.380 に答える