You can retrieve this Image object via the ImageIcon's getImage() method. JLabel lbl = new JLabel("The Duke", iconDuke, JLabel.CENTER) Īn ImageIcon uses an object to hold the image data. URL imgURL = getClass().getClassLoader().getResource(imgFilename) String imgFilename = "images/duke.gif" // relative to project root (or bin) Many JComponents (such as JLabel, JButton) accepts an ImageIcon in its constructor, or via the setIcon() method. Using URL is more flexible as it can access resources in a JAR file, and produces an error message if the file does not exist (which results in a null URL). Filename always relative to the root of the project (i.e., bin) imgURL = getClass().getClassLoader().getResource(imgFilename) Construct an ImageIcon via an image URL (in the form of file://path/filename) ImageIcon iconDuke = new ImageIcon(imgFilename) // OR Can use an absolute filename such as "c:/project/images/nought.gif" For example, // Construct an ImageIcon from an image filename Image file type of GIF, PNG, JPG and BMP are supported. To construct an ImageIcon, provide the image filename or URL. The ImageIcon class implements interface, and hence, often upcasted and referenced as Icon. An ImageIcon is a fixed-size picture, typically small, and mainly used for decorating GUI components. For example, the figure shows three buttons: one with text label, one with an image icon, and one with both text and icon. Many Swing's JComponents (such as JLabel and JButton) support a text label and an image icon. JComponent and its descendants are lightweight components. The class hierarchy of Swing's JComponents is as follows. These top-level Swing containers are heavyweight, that rely on the underlying windowing subsystem of the native platform. The class hierarchy of Swing's top-level containers ( JFrame, JDialog, JApplet) are as follows. The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.JDK demo includes a folder " jfc", which has many interesting demo on Swing and Java2D. checkNumber() method to check niven number Taking the number as input from the user using scanner classĢ020 is a Niven Number Method-3: Java Program to Check Niven Number By Using Niven Number import (num+" is Not a Niven Number") Ģ020 is a Niven Number Method-2: Java Program to Check Niven Number By Using User Input Value import Loop that iterates all digits and adds them Temp is used to preserve orignal variable Method-1: Java Program to Check Niven Number By Using Static Value import Let’s see different ways to check niven number. After coming out of loop if the resultant sum is equal to the number then the number is a Niven number, else it is not.We then use a while loop to iterate digits in the number and calculate the sum.We ask the user to enter a number which we store in an integer variable num.In the above examples the numbers 20 both are Niven numbers while 9 is not.Īre you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples Niven numbers are integers in a given number that is divisible by the sum of its digits when written in that base.
In this article we are going to understand what Niven number is and how we can check whether a number is Niven or not in Java with examples. In the previous article, we have have discussed Java Program to Check Neon Number