Then look at the Java book, according to an example inside the code, the function is to click on a different button to modify the top left corner of the form of the icon.
However, the run code displays the error java.lang.NullPointerException. Sent a question, someone replied that there is a problem with the path of the picture set, I also did the test, the path is indeed null.
I checked the Internet, I have been blindly changed, the final program can run normally.
Here's the code and some of my ideas. The annotation section is the original code, is I revise and the test part, everybody mainly looks that piece is good.
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 = 3170
32021100672658L;
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 ("C
Om.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"); This sentence is the culprit that led to the absence of pointers//SYSTEM.OUT.P Rintln (Resource); Test the previous sentence has no pointer, the result returns NULL. I later commented out the image image = (new ImageIcon ("Image/background.jpg")). GetImage ()//This sentence was new ImageIcon (Resource), which was commented out above Resource, that's all you have to say.
<span style= "White-space:pre" > </span>//get picture information, do not know why two code, and will be wrong ~ _ ~.
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 = "";//define Icon file name variable//
Image image = null; if (e.getsource () = = button1) resource = "image/icon1.png";//OK button corresponding icon file 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) for testing;
Backgroundpanel.setimage (image);
Image image = (new ImageIcon (Resource)). GetImage ();
Backgroundpanel.setimage (image); URL url = this.getclass (). getresource (Resource);//Get Icon file path (This sentence doesn't know what to do, but it does cause the program to run) Seticonimage (toolkit.ge
Tdefaulttoolkit (). GetImage (Resource));//Set the icon for the form}}
On the whole, the problem is in the URL that sentence, I do not know the cause of the problem, but I changed a code can be done to achieve the desired effect, do not know what the book code is how to think