[英]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: 问题:
@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.