简体   繁体   中英

GUI doesn't appear in IntelliJ IDEA

I'm learning Java along with IntelliJ IDEA . I want to try the Celsius converter explained in Oracle tutorial so I did the following steps:

  • Created a new GUI form.

  • Created a class called CelsiusConverterGUI .

It says that the form is automatically bind to the CelsiusConverterGUI.java .

Below is the CelsiusConverterGUI class declaration:

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


public class CelsiusConverterGUI extends Frame{
  private JTextField celsiusDegree;
  private JButton convertButton;
  private JLabel fahrenheitDeg;
  private JPanel panel;

  public CelsiusConverterGUI(){
    setVisible(true);
    convertButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
        fahrenheitDeg.setText(fahrDegree+"Fahrenheit");
      }
    });
  }

  public static void main(String[] args){
    new CelsiusConverterGUI();
  }
}

When I run it, a Java application window pops up hodling only the menu bar default icons ( x - + ), and doesn't show the panel or buttons I created in the GUI form.

Can anyone explain why does it behave so?

Problems:

  • You're extending the AWT Frame class not the Swing JFrame class
  • You've added nothing to your GUI. In your constructor you should add components to your JPanel and add that to your JFrame before setting it visible.
  • You need to go through the Swing tutorials which you can find here .
  • My recommendation when learning a new library -- avoid code generation tools and instead learn to code it by hand as this will give you a better foundation for understanding the libraries internals.
  • You're learning a new library and it will be difficult while you're just starting out, but keep at it, avoid guessing by reading the tutorials, and it will come.

@Hovercraft has already tackled the main issues, and I would only add that you are missing your GUI component initialization that will certainly lead to a NullPointerException once you get your frame visual and functional:

  • You are calling the getText() method on your celsiusDegree text field:

     int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())); 
  • While you have only declared that instance field without initialization:

     private JTextField celsiusDegree; 

Here down a correct version of your class that fixes down main issues from @Hovercraft answer and my additional note:

package org.wisebrains.thirdparty.gui;

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

public class CelsiusConverterGUI extends JFrame{
  private JTextField celsiusDegree;
  private JButton convertButton;
  private JLabel fahrenheitDeg;
  private JPanel panel;

  public CelsiusConverterGUI(){
    // GUI Components Initialization
    convertButton = new JButton("Convert");
    celsiusDegree = new JTextField("");
    panel = new JPanel(new BorderLayout());
    fahrenheitDeg = new JLabel();
    //...
    convertButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
        fahrenheitDeg.setText(fahrDegree + " Fahrenheit");
      }
    });
    // Adding your GUI Components to the main panel then to the frame
    panel.add(convertButton, BorderLayout.CENTER);
    panel.add(celsiusDegree, BorderLayout.NORTH);
    panel.add(fahrenheitDeg, BorderLayout.SOUTH);
    this.add(panel);
    this.setVisible(true);
    this.setSize(300,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }


  public static void main(String[] args){
    new CelsiusConverterGUI();
  }
}

you miss add content panel

Below is the CelsiusConverterGUI class declaration:

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


public class CelsiusConverterGUI extends Frame{
  private JTextField celsiusDegree;
  private JButton convertButton;
  private JLabel fahrenheitDeg;
  private JPanel panel;

  public CelsiusConverterGUI(){
    super("CelsiusConverterGUI");//name of you program
    setSize(400,500);//size of this
    setContentPane(panel);//to show you content(you miss it)
    pack();//set content the same design in GUI
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close form to exte
    setVisible(true);
    convertButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
        fahrenheitDeg.setText(fahrDegree+"Fahrenheit");
      }
    });
  }

  public static void main(String[] args){
    new CelsiusConverterGUI();
  }
}

I just looked into the tutorial of IntelliJ IDEA for GUI form, and I found the solution to my problem, by editing the main() method.

Here's the code:

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


    public class CelsiusConverterGUI{
        private JTextField celsiusDegree;
        private JButton convertButton;
        private JLabel fahrenheitDeg;
        private JPanel panel;
        private JLabel celsiusLabel;


        public CelsiusConverterGUI(){


            convertButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())*1.8+32);
                    fahrenheitDeg.setText(fahrDegree+" Fahrenheit");
                }
            });

        }


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

    }

Compared to my original code, the new one adds the JFrame frame, the setContentPane, and the default close operation.

Now my understanding is that, I indeed don't need to initialise the components that I created in the GUI form. But I do need to create the JFrame frame to make the GUI form functional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM