GUI未出现在IntelliJ IDEA中

时间:2014-12-13 13:48:45

标签: java swing user-interface intellij-idea jframe

我正在学习 Java 以及 IntelliJ IDEA 。我想尝试在Oracle教程中解释的Celsius转换器,所以我做了以下步骤:

  • 创建了一个新的GUI表单。

  • 创建了一个名为CelsiusConverterGUI的课程。

它表示表单会自动绑定到 CelsiusConverterGUI.java

以下是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();
  }
}

当我运行它时,会弹出 Java 应用程序窗口,仅显示菜单栏默认图标( x - + < / strong>),并且不显示我在GUI表单中创建的面板或按钮。

任何人都可以解释为什么会这样吗?

4 个答案:

答案 0 :(得分:2)

问题:

  • 您正在扩展AWT Frame类而不是Swing JFrame类
  • 您没有在GUI中添加任何内容。在构造函数中,您应该将组件添加到JPanel并将其添加到JFrame ,然后再将其设置为
  • 您需要浏览Swing教程,您可以找到here
  • 我在学习新库时的建议 - 避免使用代码生成工具,而是学会手工编写代码,因为这样可以为理解库内部提供更好的基础。
  • 你正在学习一个新的图书馆,当你刚开始学习它时会很困难,但要坚持下去,不要通过阅读教程来猜测,它会来。

答案 1 :(得分:1)

@Hovercraft已经解决了主要问题,我只想补充说你错过了你的GUI组件初始化,一旦你的框架可视化和正常运行,肯定会导致NullPointerException

  • 您在getText()文字字段中调用celsiusDegree方法:

    int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
    
  • 虽然您只声明了没有初始化的实例字段:

    private JTextField celsiusDegree;
    

这是你的课程的正确版本,它修复了来自@Hovercraft的主要问题以及我的附加说明:

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();
  }
}

答案 2 :(得分:1)

您错过了添加内容面板

以下是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();
  }
}

答案 3 :(得分:0)

我刚刚查看了用于GUI表单的IntelliJ IDEA教程,我通过编辑main()方法找到了解决问题的方法。

以下是代码:

    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);
        }

    }

与原始代码相比,新代码添加了JFrame框架,setContentPane和默认关闭操作。

现在我的理解是,我确实不需要初始化我在GUI表单中创建的组件。但我确实需要创建JFrame框架以使GUI形式起作用。