Unlike AWT, Java Swing provides platform-independent and lightweight components. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Public static void main(String.Java Swing Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window- based applications. JPanel c3 = new JPanel() c3.add(frozen) Ĭenter.add(c0) center.add(c1) center.add(c2) center.add(c3)
JPanel c2 = new JPanel() c2.add(direct_3) JPanel c1 = new JPanel() c1.add(direct_2) JPanel c0 = new JPanel() c0.add(direct_1) JPanel center = new JPanel(new GridLayout(4, 0)) JLabel frozen = new JLabel("This hourglass is frozen") įtIcon(new ImageIcon((new java.io.File("hourglass.gif")))) JLabel direct_3 = new JLabel("Direct 3: This hourglass is animating") ĭirect_3.setIcon(new ImageIcon("hourglass.gif")) JLabel direct_2 = new JLabel("Direct 2: This hourglass is animating") ĭirect_2.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/hourglass.gif")))) JLabel direct_1 = new JLabel("Direct 1: This hourglass is animating") ĭirect_1.setIcon(new ImageIcon(getClass().getResource("/hourglass.gif"))) SetIconImage((new ImageIcon((getClass().getResourceAsStream("/java.gif")))).getImage()) setIconImage((new ImageIcon(getClass().getResource("/java.gif"))).getImage()) this way causes Swing to be hung for JFrame Icon SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) SetTitle("Swing & Icons - Joe Nartca (C) -") The following show you how SWING with images works import javax.swing.* If you try with direct loading your app would hang. For the JFrame Icon it works only with the Indirect Way. narrowing or converting from format A to format B). Indirect way is only useful if you have to modify an image (e.g. ImageIcon icon = new ImageIcon((this.getClass().getResourceAsStream("/hourglass.gif"))) ImageIcon icon = new ImageIcon((new java.io.File("hourglass.gif"))) For example, the animation could get lost. This way could kill some characteristics of an image. The indirect way is the opposite of direct way where the image is extracted (or read) from a file by (). Or this superfluous way: ImageIcon icon = new ImageIcon(().getImage(getClass().getResource("/hourglass.gif"))) Example: ImageIcon icon = new ImageIcon(this.getClass().getResource("/hourglass.gif")) If your app is a JAR with jarred Images you have to load it in the "direct" form with the specific class and resource.
If an image is animating the icon is animating. This way always preserves all the characteristics of an image.
ImageIcon(URL location, String description): Creates an ImageIcon from the specified URL. ImageIcon(URL location): Creates an ImageIcon from the specified URL. ImageIcon(String filename, String description): Creates an ImageIcon from the specified file. Example: ImageIcon ImageIcon(String filename): Creates an ImageIcon from the specified file. It depends on the way how you load the icon from a file. However Icons or images have to be correctly loaded and applied, otherwise the image effects could become weird. SWING app without Graphics User Interface or GUI with icons/images is boring and uninteresting. SWING developers usually confuse GIF image (Graphics Interchange Format) with other formatted images (JPG, PNG, etc.) GIF image is usually animating (or dynamic) while the others are static.