Reputation: 123
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?
Upvotes: 1
Views: 10458
Reputation: 223
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();
}
}
Upvotes: 1
Reputation: 123
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.
Upvotes: 0
Reputation: 16384
@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();
}
}
Upvotes: 1
Reputation: 285450
Problems:
Upvotes: 2