[英]how to create own file with icon that inherit from JFrame icon, that I set it, in java and my own file use FileOutputStream and ObjectOutputStream
我想用繼承自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");
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”)是您的工作,但文件的最終確定取決於操作系統,並賦予它什么圖標。
可以在一些文件中嵌入圖標(許多可執行文件通常具有自己的圖標),但最終還是取決於操作系統。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.