如何更改jframe的图标?

68bkxrlz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(340)

这个问题在这里已经有答案了

如何将图标设置为jframe(12个答案)
四年前关门了。
我目前正在使用netbeans7.3开发一个javaswing项目,我想更改swing窗体的图标,但是我不能这样做。我试过很多方法,但都不管用。请告诉我如何更改图标。请同时提供代码。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Rahul
 */
public class SMS extends javax.swing.JFrame {

    /**
     * Creates new form SMS
     */
    public SMS() {
        super.setResizable(false);
        super.setLocation((582)/2, (367)/2);

        File imageFile = new File("d:/images.gif");
        BufferedImage i=null;
        try {
            i = ImageIO.read(imageFile);
        } catch (IOException ex) {
            Logger.getLogger(SMS.class.getName()).log(Level.SEVERE, null, ex);
        }
        setIconImage(i);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        msg = new javax.swing.JTextArea();
        mob = new javax.swing.JTextField();
        send = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        output = new javax.swing.JLabel();
        output.setVisible(false);
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Message Sending Through JAVA");
        setIconImage(new ImageIcon("d:/images.gif").getImage());
        setIconImages(null);
        setResizable(false);

        jLabel1.setText("Mobile No.");

        jLabel2.setText("Message");

        msg.setColumns(20);
        msg.setRows(5);
        jScrollPane1.setViewportView(msg);

        mob.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mobActionPerformed(evt);
            }
        });
        mob.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                mobKeyTyped(evt);
            }
        });

        send.setText("Send Message");
        send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });

        reset.setText("Reset");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        output.setFont(new java.awt.Font("Comic Sans MS", 3, 18)); // NOI18N
        output.setForeground(new java.awt.Color(0, 255, 51));
        output.setText("jLabel3");

        jLabel3.setForeground(new java.awt.Color(255, 51, 51));
        jLabel3.setText("*Enter only 10 digits");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(mob, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(33, 33, 33)
                                .addComponent(jLabel3))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(send)
                        .addGap(129, 129, 129)
                        .addComponent(reset))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(214, 214, 214)
                        .addComponent(output)))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(mob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(send)
                    .addComponent(reset))
                .addGap(46, 46, 46)
                .addComponent(output)
                .addContainerGap(101, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void mobActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
    }                                   

    private void mobKeyTyped(java.awt.event.KeyEvent evt) {                             
        if (mob.getText().length() < 10) {
            char c = evt.getKeyChar();
            if (!((c >= '0') && (c <= '9'))) {
                evt.consume();
            }
        } else {
            evt.consume();
        }

    }                            

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {                                      
        mob.setText("");
        msg.setText("");
    }                                     

    private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                     
        }                            

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SMS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SMS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SMS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SMS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SMS().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField mob;
    private javax.swing.JTextArea msg;
    private javax.swing.JLabel output;
    private javax.swing.JButton reset;
    private javax.swing.JButton send;
    // End of variables declaration                   
}
oxcyiej7

oxcyiej71#

通过调用此语句

setIconImages(null);

通过设置
null List 应用程序窗口的图像。删除或注解掉该行。
您只需要一个语句就可以设置 JFrame :

setIconImage(ImageIO.read(imageFile));

或者更好
根据平台功能的不同,seticonimages将使用以下几种方法之一 Images 作为窗口的图标。
因此,将此与作为资源读取图像相结合:

List<Image> imageList = new ArrayList<>();
Image image1 = ImageIO.read(getClass().getResource("/images/MyImage.png"));
imageList.add(image1);
// add more images
setIconImages(imageList);

相关问题