【问题标题】: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 图标继承的图标创建自己的文件,我设置它,在 java 和我自己的文件中使用 FileOutputStream 和 ObjectOutputStream
【发布时间】:2012-04-28 01:44:43
【问题描述】:

我想用继承自 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());
}

提前谢谢你

【问题讨论】:

    标签: java swing icons jframe


    【解决方案1】:

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

    【讨论】:

    • 谢谢...它对我也很有用,但这不是我需要的
    • 啊,对不起。获得对图标的引用后,可以将其呈现为 BufferedImage 并将其保存到文件中,如 here 所示。大多数操作系统都可以从那里获取它。
    【解决方案2】:

    操作系统选择显示文件的图标。将数据写入文件并为其指定文件扩展名(在本例中为“ckl”)是您的工作,但文件最终位于的操作系统决定了它给出的图标。

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

    【讨论】:

      猜你喜欢
      • 2016-06-27
      • 1970-01-01
      • 2016-09-16
      • 2010-12-15
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多