Hello World swing app will not display.

Answered

I am just trying to learn the GUI designer on Intellij and cannot get this basic app to run for the life of me. Here is the code...

package com.codebind;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App {

   private JButton buttonMsg;
   private JPanel panelMain;

   public App() {
      buttonMsg.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
           JOptionPane.showMessageDialog(null, "Hello World");
           }
      });
    }

    public static void main(String[] args) {
       JFrame frame = new JFrame ("App");
          frame.setContentPane(new App().panelMain);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
    }
}

This is the exception I get when run...


Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class sun.font.SunFontManager
at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264)
at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1113)
at javax.swing.JComponent.getFontMetrics(JComponent.java:1626)
at javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:276)
at javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:376)
at javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:366)
at javax.swing.JComponent.getMinimumSize(JComponent.java:1744)
at com.intellij.uiDesigner.core.Util.getMinimumSize(Util.java:27)
at com.intellij.uiDesigner.core.DimensionInfo.getMinimumSize(DimensionInfo.java:239)
at com.intellij.uiDesigner.core.HorizontalInfo.getMinimumWidth(HorizontalInfo.java:40)
at com.intellij.uiDesigner.core.GridLayoutManager.getMinOrPrefSizes(GridLayoutManager.java:645)
at com.intellij.uiDesigner.core.GridLayoutManager.getPrefSizes(GridLayoutManager.java:630)
at com.intellij.uiDesigner.core.GridLayoutManager.preferredLayoutSize(GridLayoutManager.java:274)
at java.awt.Container.preferredSize(Container.java:1796)
at java.awt.Container.getPreferredSize(Container.java:1780)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:920)
at java.awt.Container.preferredSize(Container.java:1796)
at java.awt.Container.getPreferredSize(Container.java:1780)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
at java.awt.Container.preferredSize(Container.java:1796)
at java.awt.Container.getPreferredSize(Container.java:1780)
at java.awt.Window.pack(Window.java:809)
at com.codebind.App.main(App.java:24)

0
3 comments

What JDK vendor/version/OS do you use to run this project? It looks like your JDK installation is damaged. Try with a different freshly installed JDK version that can run Swing apps on your system (verify it with JEdit or some other app first).

0
Avatar
Permanently deleted user

Just tried to instal JDK 13.0.1 but that has not worked. I am on Windows 10. I will try to do a fresh installation again, as it works fine on my laptop running the same code on Windows 10. Thanks for the quick response!

0

Check the PATH environment variable, you may have some old JDK directories at the top there.

0

Please sign in to leave a comment.