0

現在、netbeans 7.3 を使用して Java スイング プロジェクトに取り組んでいます。スイング フォームのアイコンを変更したいのですが、変更できません。私は多くの方法を試しましたが、うまくいきません。アイコンの変更方法を教えてください。コードもお願いします。

/*
 * 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                   
}
4

1 に答える 1

0

このステートメントを呼び出すことによって

setIconImages(null);

null Listアプリケーション ウィンドウのイメージを設定することで、以前のアイコン イメージを効果的にクリアしています。行を削除またはコメントアウトします。

のアイコンを設定するために必要なステートメントは 1 つだけですJFrame

setIconImage(ImageIO.read(imageFile));

またはそれ以上

プラットフォームの機能に応じて、setIconImagesはいくつかのアイコンの 1 つをImagesウィンドウのアイコンとして使用します。

したがって、これをリソースとしての画像の読み取りと組み合わせます。

List<Image> imageList = new ArrayList<>();
Image image1 = ImageIO.read(getClass().getResource("/images/MyImage.png"));
imageList.add(image1);
// add more images
setIconImages(imageList);
于 2013-06-06T11:37:25.777 に答える