Java 图形用户界面不';不要出现在IntelliJ IDEA中

Java 图形用户界面不';不要出现在IntelliJ IDEA中,java,swing,user-interface,intellij-idea,jframe,Java,Swing,User Interface,Intellij Idea,Jframe,我正在学习Java和IntelliJ理念。我想尝试Oracle教程中介绍的摄氏度转换器,因此我执行了以下步骤: 创建了一个新的GUI表单 创建了一个名为CelsiusConverterGUI的类 它表示表单自动绑定到CelsiusConverterGUI.java 以下是CelsiusConverterGUI类声明: import javax.swing.*; 导入java.awt.*; 导入java.awt.event.ActionEvent; 导入java.awt.event.Actio

我正在学习Java和IntelliJ理念。我想尝试Oracle教程中介绍的摄氏度转换器,因此我执行了以下步骤:

  • 创建了一个新的GUI表单

  • 创建了一个名为
    CelsiusConverterGUI
    的类

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

以下是CelsiusConverterGUI类声明:

import javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类CelsiusConverterGUI扩展框架{
私人JTextField celsiusDegree;
私有JButton转换按钮;
私人JLabel fahrenheitDeg;
私人JPanel小组;
公共Celsiuconvertergui(){
setVisible(真);
convertButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
int fahrDegree=(int)(Double.parseDouble(celsiusDegree.getText());
华氏温度设定值(华氏度+华氏度);
}
});
}
公共静态void main(字符串[]args){
新的Celsiuconvertergui();
}
}
当我运行它时,会弹出一个Java应用程序窗口,只显示菜单栏的默认图标(x-+),而不显示我在GUI表单中创建的面板或按钮

有人能解释一下为什么会这样吗?

问题:

  • 您正在扩展AWT框架类,而不是Swing JFrame类
  • 您没有向GUI添加任何内容。在构造器中,您应该将组件添加到JPanel中,并将其添加到JFrame中,然后再将其设置为可见
  • 您需要阅读您可以找到的Swing教程
  • 学习新图书馆时,我建议不要使用代码生成工具,而是学会用手工编码它,这将为理解库内部结构提供更好的基础。
  • 您正在学习一个新的库,刚开始学习时会很困难,但请继续学习,通过阅读教程避免猜测,它会来的
问题:

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

    • @Hovercraft已经解决了主要问题,我只想补充一点,您缺少GUI组件初始化,一旦您的框架视觉化和功能化,肯定会导致出现
      NullPointerException

      • 您正在调用
        celsiusDegree
        文本字段上的
        getText()
        方法:

        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
        
      • 虽然您只声明了该实例字段,但未进行初始化:

        private JTextField celsiusDegree;
        
      下面是您的课程的正确版本,它修复了@Hovercraft answer和我的补充说明中的主要问题:

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

      @气垫船已经解决了主要问题,我只想补充一点,就是您缺少GUI组件初始化,一旦您的框架视觉化和功能化,肯定会导致
      NullPointerException

      • 您正在调用
        celsiusDegree
        文本字段上的
        getText()
        方法:

        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText()));
        
      • 虽然您只声明了该实例字段,但未进行初始化:

        private JTextField celsiusDegree;
        
      下面是您的课程的正确版本,它修复了@Hovercraft answer和我的补充说明中的主要问题:

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

      我刚刚查阅了IntelliJ IDEA for GUI表单的教程,通过编辑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表单正常工作。

      我刚刚查阅了IntelliJ IDEA for GUI表单教程,通过编辑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表单正常工作。

      您错过了添加内容面板

      以下是CelsiusConverterGUI类声明:

      import javax.swing.*;
      导入java.awt.*;
      导入java.awt.event.ActionEvent;
      导入java.awt.event.ActionListener;
      公共类CelsiusConverterGUI扩展框架{
      私人JTextField celsiusDegree;
      私有JButton转换按钮;
      私人JLabel fahrenheitDeg;
      私人JPanel小组;
      公共Celsiuconvertergui(){
      super(“CelsiusConverterGUI”);//您的程序名
      setSize(400500);//此文件的大小
      setContentPane(面板);//显示内容(您错过了)
      pack();//在GUI中设置相同设计的内容
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//将表单关闭到exte
      setVisible(真);
      convertButton.addActionListener(新ActionListener(){
      @凌驾
      已执行的公共无效操作(操作事件e){
      内华氏度