Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My java code has an error, I want to know how to solve it. This is a simple Java GUI code that shows some basic

My java code has an error, I want to know how to solve it.

This is a simple Java GUI code that shows some basic information and two GIF images.

It seems like can not invoking two gif images successfully.

code:

import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Simple Java GUI Example. This example is a modification of Example 2. It uses a a resource locator to indicate the location of the image files (see line 26 and line 119) @version 1.16.2 */ public class SimpleSwingGUIe2R extends JFrame implements ActionListener { //default serial version ID - Swing components implement the Serializable interface private static final long serialVersionUID = 1L; private JButton button; private JPanel pane; private JLabel label; private final static String FRAME_TITLE = "Simple Swing GUI - E2R"; private final String frameImageName = "duke.gif"; //using a resource locator to indicate the location of the image files private final Icon buttonImage = new ImageIcon(getClass().getResource("happy.gif")); private final String labelText = "Number of happy button clicks: "; private int mouseClickCounter; /** Default constructor. Sets the frame properties. */ public SimpleSwingGUIe2R(){ super(FRAME_TITLE); setFrameProperties(); } /** Sets the GUI makes it visible */ public void setAndLaunch() { // Create and set up containers and components. // Create a button with an image and text button = new JButton("A Happy New Semester JButton",buttonImage); // Create a keyboard shortcut: Pressing ALT-I will act as a mouse click on the button button.setMnemonic('i'); // Create a Tool Tip. Will show up when the mouse hovers over the button button.setToolTipText("Please Click Me"); // Set the frame to be the event handler for the button click button.addActionListener(this); label = new JLabel(labelText + "0"); pane = new JPanel(); // set a border around the JPanel pane.setBorder(BorderFactory.createEmptyBorder(25, 25, 10, 25)); // change the default Layout Manager // use GridLayout with 2 rows and 1 column pane.setLayout(new GridLayout(2, 1));

// Add components to JPanel container. pane.add(button); pane.add(label); // Install JPanel as a content pane Container contentPane = getContentPane(); contentPane.add(pane);

//Handle the UI events. // Create a handler class for the frame closing event // Set up the Close button (X) of the frame. // Since a in Java class can extend one class only // anonymous inner class which inherits from WindowAdapter and overrides the WindowClosing() method // is used to handle the frame closing event WindowListener wl = new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { //Releases all of the native screen resources used by this frame (Window) //Check if all user documents have been saved SimpleSwingGUIe2R.this.dispose(); //terminate the application and the Java VM System.exit(0); } }; // add the event handler wl to the frame addWindowListener(wl); // The frame is ready. Make it visible on the screen. // pack(); setVisible(true); }

// Handle the button clicks. // Override and Implement the actionPerformed() method of the Action Listener interface @Override public void actionPerformed(ActionEvent e) { // Check which button was clicked and process the event Object eventSource = e.getSource(); if (eventSource == button){ mouseClickCounter++; label.setText(labelText + mouseClickCounter); } else {/*check for another component*/} }

/** Set the frame properties. */ public void setFrameProperties(){ //get the current screen size Toolkit tkit = Toolkit.getDefaultToolkit(); Dimension screenSize = tkit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width;

// set frame size - width and height setSize(screenWidth / 2, screenHeight / 5); // and let platform pick screen location // setLocationByPlatform(true);//ByPlatform //Display the frame in the center of the screen //Since Java 1.7 the same can be achieved with a single call. Try it. //setLocationRelativeTo(null); setLocation(screenWidth / 4, screenHeight / 4); // set frame icon and title Image img = tkit.getImage(getClass().getResource(frameImageName)); setIconImage(img); }

/** The main method.The GUI will have the default Look and Feel - Metal Look and Feel @param args not used */ public static void main(String[] args) { new SimpleSwingGUIe2R().setAndLaunch(); } }

error picture:

image text in transcribed

run: Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:217) at SimpleSwingGUIe2R.(SimpleSwingGUIe2R.java:26) at SimpleSwingGUIe2R.main(SimpleSwingGUIe2R.java:129) C:\Users\56983\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second)

Start Page X Stopwatch.java X SimpleSwing Gule 2.java X SimpleSwingGUIe 2.java X FXMLCSSButton.java X Source History X Files Services Example 1 SimpleSwingGule 1.java Example2 SimpleSwing Gule 2.java duke.gif happy.gif Example 2R SimpleSwingGUIe 2.java duke.gif happy.gif Example3 SimpleSwingGUIe 3.java Example 3L SimpleSwingGUIe3L.java Example 4 IBO 19 private static final long serialVersionUID = IL; 20 private JButton button; 21 private JPanel pane; 22 private JLabel label; 23 private final static String FRAME_TITLE = "Simple Swing GUI E2R"; 24 private final String frame ImageName = "duke.gif"; 25 //using a resource locator to indicate the location of the image files 26 private final Icon button Image = new ImageIcon (getClass().getResource ("happy.gif")) = 27 private final String labelText = "Number of happy button clicks: "; 28 private int mouseClickCounter; 29 30 Default constructor. Sets the frame properties. 31 32 public SimpleSwingGUIL2R() { 33 super (FRAME_TITLE) ; 2. setFrame Properties(); 35 } 36 37 Sets the GUI makes it visible > Output x 56983-C:\Users\56983 Stopwatch (run) X SimpleSwingGUIE ZR :: JFrame SimpleSwingGUIE ZRO actionPerformed(ActionEve main(String] args) setAndLaunch setFrameProperties FRAME_TITLE : String button : JButton button Image: Icon frameImageName : String label : JLabel labelText: String run: Q Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon. (ImageIcon.java:217) at SimpleSwingGUIe2R.(SimpleSwingGUIe2B.java:26) at SimpleSwingGUIe2R.main(SimpleSwingGUIe2r.java:129) C:\Users\56983\AppData\Local\NetBeans Cache\8.2\executor-snippets un.xml:53: Java returned: 1 BUILD FAILED (total time: 2 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Evaluation is ongoing and used to improve the system.

Answered: 1 week ago

Question

6. Effectively perform the managers role in career management.

Answered: 1 week ago