再看java的書,照著裡面的一個執行個體編寫代碼,功能是點擊不同的按鈕可以修改表單左上方的表徵圖。
但是運行代碼顯示錯誤java.lang.NullPointerException。發了提問,有人回答說是圖片設定的路徑有問題,我自己也做了測試,路徑確實是null。
我上網查了下,自己瞎改了改,最後程式可以正常運行了。
下面是代碼和我的一些想法。注釋部分就是原代碼,被我修改和測試的部分,大家主要看那一塊就好了。
package thirdthChapter;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.Image;import java.awt.Toolkit;//import java.net.URL;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.UIManager;public class FrameIcon extends JFrame { /** * */ private static final long serialVersionUID = 317032021100672658L; private JPanel contentPane; private JButton button1; private JButton button2; private JButton button3; private JButton button4; //private BackgroundPanel backgroundPanel; /** * Launch the application. */ public static void main(String[] args) { try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run(){ try { //System.out.println("@@@@@@@@@"); FrameIcon frame = new FrameIcon(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public FrameIcon() { setResizable(false); setTitle("\u6307\u5B9A\u7A97\u4F53\u6807\u9898\u680F\u56FE\u6807"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 535, 348); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); System.out.println("##########"); BackgroundPanel backgroundPanel = new BackgroundPanel(); //URL resource = this.getClass().getResource("image/background.jpg");這一句就是導致無指標的罪魁 //System.out.println(resource);測試上一句有無指標的,結果返回時null。後來被我注釋掉 Image image = (new ImageIcon("image/background.jpg")).getImage();//這一句本來是new ImageIcon(resource),就是上面被注釋掉的resource,只這一句就可以
<span style="white-space:pre"></span>//獲得圖片資訊了,不知道為什麼要兩句代碼,而且會出錯了~ _ ~。 System.out.println("xxxxxxxx"); backgroundPanel.setImage(image); contentPane.add(backgroundPanel, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setOpaque(false); panel.setBounds(45, 212, 447, 54); backgroundPanel.add(panel); button1 = new JButton("\u56FE\u68071"); button1.setIcon(new ImageIcon("image/icon1.png")); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); panel.add(button1); button2 = new JButton("\u56FE\u68072"); panel.add(button2); button2.setIcon(new ImageIcon("image/icon2.png")); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button3 = new JButton("\u56FE\u68073"); panel.add(button3); button3.setIcon(new ImageIcon("image/icon3.png")); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button4 = new JButton("\u56FE\u68074"); panel.add(button4); button4.setIcon(new ImageIcon("image/icon4.png")); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); } protected void do_button_actionPerformed(ActionEvent e) { String resource = "";// 定義表徵圖檔案名稱變數 //Image image = null; if (e.getSource() == button1) resource = "image/icon1.png";// 確定按鈕對應的表徵圖檔案 if (e.getSource() == button2) resource = "image/icon2.png"; if (e.getSource() == button3) resource = "image/icon3.png"; if (e.getSource() == button4) resource = "image/icon4.png"; //System.out.println("fffffffffffff");測試用 //System.out.println(resource); //backgroundPanel.setImage(image); //Image image = (new ImageIcon(resource)).getImage(); // backgroundPanel.setImage(image); //URL url = this.getClass().getResource(resource);// 擷取表徵圖檔案路徑(這句話不知道幹什麼用的,但是確實是導致程式跑不通的原因) setIconImage(Toolkit.getDefaultToolkit().getImage(resource));// 設定表單的表徵圖 }}
整體來講,問題就出在URL那一句,我不知道問題的原因出在哪,但是我改的一句代碼就可以完成想要的效果了,不知道書上的代碼是怎麼想的