简体   繁体   English

GUI没有出现在IntelliJ IDEA中

[英]GUI doesn't appear in IntelliJ IDEA

I'm learning Java along with IntelliJ IDEA . 我正在与IntelliJ IDEA一起学习Java I want to try the Celsius converter explained in Oracle tutorial so I did the following steps: 我想尝试Oracle教程中解释的Celsius转换器,所以我执行了以下步骤:

  • Created a new GUI form. 创建了一个新的GUI表单。

  • Created a class called CelsiusConverterGUI . 创建了一个名为CelsiusConverterGUI的类。

It says that the form is automatically bind to the CelsiusConverterGUI.java . 它说该表单会自动绑定到CelsiusConverterGUI.java

Below is the CelsiusConverterGUI class declaration: 以下是CelsiusConverterGUI类声明:

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. 当我运行一个Java应用程序窗口弹出hodling只在菜单栏的默认图标(X - +),并且不显示我在GUI形式创建的面板或按钮。

Can anyone explain why does it behave so? 谁能解释为什么会这样呢?

Problems: 问题:

  • You're extending the AWT Frame class not the Swing JFrame class 您在扩展AWT Frame类而不是Swing JFrame类
  • You've added nothing to your GUI. 您未向GUI添加任何内容。 In your constructor you should add components to your JPanel and add that to your JFrame before setting it visible. 在构造函数中,应将组件添加到JPanel并将其添加到JFrame, 然后再将其设置为可见。
  • You need to go through the Swing tutorials which you can find here . 您需要阅读Swing教程,您可以在这里找到。
  • 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: @Hovercraft已经解决了主要问题,我只想补充一下,您缺少GUI组件的初始化,一旦使框架外观和功能正常,肯定会导致NullPointerException

  • You are calling the getText() method on your celsiusDegree text field: 您正在celsiusDegree文本字段上调用getText()方法:

     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: 这里是您的班级的正确版本,可以解决@Hovercraft答案和我的附加说明中的主要问题:

package org.wisebrains.thirdparty.gui; 软件包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: 以下是CelsiusConverterGUI类声明:

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. 我只是研究了IntelliJ IDEA for GUI形式的教程,并通过编辑main()方法找到了解决问题的方法。

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. 与我的原始代码相比,新代码添加了JFrame框架,setContentPane和默认的close操作。

Now my understanding is that, I indeed don't need to initialise the components that I created in the GUI form. 现在我的理解是,我确实不需要初始化在GUI表单中创建的组件。 But I do need to create the JFrame frame to make the GUI form functional. 但是我确实需要创建JFrame框架以使GUI表单起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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