0

I am fairly new to using JFrame and other components such as JTextField, JLabel. I was trying to create a little program where the user enters the Fahrenheit Temperature and it converts it to celsius. This is the code that i have so far

TempConverter.java

public static void main(String[] args){
    JFrame frame = new JFrame("Temperature Converter");
    Temperature temp = new Temperature();

    JLabel temperature_label;
    JTextField farenheit_temperature;

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(400,400));
    frame.setMaximumSize(new Dimension(400,400));
    frame.setMinimumSize(new Dimension(400,400));
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);

    frame.setLayout(new FlowLayout());

    temperature_label = new JLabel("Farenheit Converter");
    temperature_label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 18));
    frame.add(temperature_label);

    farenheit_temperature = new JTextField("Enter Farenheit Temperature", 50);
    frame.add(farenheit_temperature);
    frame.setVisible(true);
}

I have no idea what i did wrong with the code. Some help and explaination what i did wrong would be appreciated. Thank you.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tyler Obier
  • 378
  • 1
  • 2
  • 15
  • 1
    you need to add to the frames container, frame.getContentPane().add(Component); – Chains Nov 06 '16 at 20:22
  • 6
    1) `frame.setVisible(true);` should be ***last.*** 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The size of a `JFrame` should be suggested by the content (along with padding and borders) and revealed on a call to `pack()`. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 06 '16 at 20:23
  • @AndrewThompson Yes, that was the problem. Thank you for the fast response I really appreciate it. And thank you for the additional information you provided. – Tyler Obier Nov 06 '16 at 20:25
  • You'll also want a `JButton` to perform your conversion action, by the way, once you get there – OneCricketeer Nov 06 '16 at 20:26
  • Glad you got it sorted. :) Note also the other tips added in edits to that comment. – Andrew Thompson Nov 06 '16 at 20:26
  • @AndrewThompson Yes i fixing my code right now. Once again thank you. – Tyler Obier Nov 06 '16 at 20:27
  • @cricket_007 *"You'll also want a `JButton`"* Note that we can add an `ActionListener` to a `JTextField`. From the perspective of a user, I'd typically prefer that (hitting 'enter' once a value is ready to be evaluated) than being forced to navigate to a `JButton` to get the action happening. Alternately, you might have a single listener attached to both text filed and button. – Andrew Thompson Nov 06 '16 at 20:29
  • @cricket_007 *"Yeah, `KeyAdapter` .."* Several times ***no!*** I said an `ActionListener` because I **meant** an `ActionListener`. When added to a text field, it will fire when the user hits the enter key. And if you were adding any form of listener for keys other than enter, it should be a `DocumentListener`, not a `KeyListener` or `KeyAdapter` - which is a much lower level listener better suited to AWT & 2001 (before Swing). – Andrew Thompson Nov 06 '16 at 20:35
  • @cricket_007 I can accept my answer in 2 days – Tyler Obier Nov 06 '16 at 20:45

1 Answers1

0
public static void main(String[] args){

    JFrame frame = new JFrame("Temperature Converter");
    Temperature temp = new Temperature();

    JLabel temperature_label;
    JTextField farenheit_temperature;

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);

    frame.setLayout(new FlowLayout());

    temperature_label = new JLabel("Farenheit Converter");
    temperature_label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 18));
    frame.getContentPane().add(temperature_label);

    farenheit_temperature = new JTextField("Enter Farenheit Temperature", 30);
    frame.getContentPane().add(farenheit_temperature);

    frame.setVisible(true);
}
Tyler Obier
  • 378
  • 1
  • 2
  • 15
  • missing there code line frame.pack(with some short description about), before code line frame.setVisible(true);, then will be goog answer ... – mKorbel Nov 07 '16 at 09:00