0

Thanks so much for bothering to look at this n.n;
I've been kind of confused in this class, mainly because I have no idea what the teacher's saying; her English isn't that great, and she has no in-class coding exercises so we're learning entirely from her crappy powerpoints. I'd have been better off just learning directly from the source material....
But I digress. I resisted the allure to just copy off of random websites, mainly because I actually want to learn, and my code is giving me errors attributed to files that aren't even there. I'm kind of wondering what's going on here....
Here's my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
public class GUI1 extends JFrame {
    private JLabel resultLabel, firstLabel, message;
    private JTextField fText;
    private JPanel p1, p3, p4, p5, p6;
    private JButton okButton;

    public GUI1(){

        super("First GUI Application");

        p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));
        firstLabel = new JLabel("Farenheit: ");
        fText = new JTextField(10);
        p1.add(firstLabel);
        p1.add(fText);


                        p3 = new JPanel();
                        p3.setLayout(new FlowLayout(FlowLayout.LEFT));
                        message = new JLabel("Converted temperature:");
                        resultLabel = new JLabel("Converted temperature to go here");
                        p3.add(message);
                        p3.add(resultLabel);

                p4 = new JPanel();
                p4.setLayout(new FlowLayout(FlowLayout.LEFT));
                p4.add(p1,BorderLayout.NORTH);
                p4.add(p3,BorderLayout.SOUTH);

                p5 = new JPanel();
                okButton = new JButton("Convert");
                p5.setLayout(new FlowLayout());
                p5.add(okButton);
                okButton.addActionListener(
                      new ActionListener() {
public void actionPerformed(ActionEvent e) {
         e.getActionCommand();{
         String f = firstLabel.getText();
         float fi = Float.parseFloat(f);
         int c = (int) Math.round((5.0 / 9.0) * (fi - 32));
resultLabel.setText(String.format("%6.2f",c));
      }
   }});
                p6 = new JPanel();
                p6.setLayout(new BorderLayout());
                p6.add(p4,BorderLayout.CENTER);
                p6.add(p5,BorderLayout.SOUTH);
                add(p6);


    }
public static void main (String[]args){
            GUI1 frame = new GUI1();

            frame.setSize(350,140);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}}
//import needed packages
import java.awt.*;
import javax.swing.*;
public class AlignGrid extends JFrame{
    private JButton okJButton;
    private JButton cancelJButton;
    private JButton helpJButton;
    private JTextField xJTextField, yJTextField;
    private JCheckBox snapJCheckBox, showJCheckBox;
    private JLabel xJLabel, yJLabel;
    private JPanel checkJPanel, buttonJPanel, fieldJPanel1, fieldJPanel2, fieldJPanel;
    public AlignGrid(){
       super ("Align");
       //build checkbox panel
       snapJCheckBox = new JCheckBox("Snap to Grid");
       showJCheckBox = new JCheckBox("Show Grid");
       checkJPanel = new JPanel();
       checkJPanel.setLayout(new GridLayout(2,1));
       //add "snap" checkbox
       checkJPanel.add(snapJCheckBox);
       //add "show" checkbox
       checkJPanel.add(showJCheckBox);
       //build panel1
       xJLabel = new JLabel("X: ");
       xJTextField = new JTextField("8",3);
       fieldJPanel = new JPanel();
       //use flow layout
       fieldJPanel.setLayout(new FlowLayout());
       fieldJPanel.add(xJLabel);
       fieldJPanel.add(xJTextField);
       //build panel2
       yJLabel = new JLabel("Y: ");
       //set text field width to 8*3
       yJTextField = new JTextField("8", 3);
       fieldJPanel2 = new JPanel();
       //use flow layout
       fieldJPanel2.setLayout(new FlowLayout());
       //add yJLabel
       fieldJPanel2.add(yJLabel);
       fieldJPanel2.add(yJTextField);
       //build field panel
       fieldJPanel = new JPanel();
       //use Border layout
       fieldJPanel.setLayout(new BorderLayout());
       fieldJPanel.add (fieldJPanel1,BorderLayout.NORTH);
       fieldJPanel.add (fieldJPanel2, BorderLayout.SOUTH);
       //build button panel
     okJButton = new JButton("OK");
     cancelJButton = new JButton("Cancel");
     helpJButton = new JButton("Help");
     buttonJPanel = new JPanel();
       buttonJPanel.setLayout(new GridLayout(3,1,10,5));
       buttonJPanel.add (okJButton,cancelJButton);
       //use flow layout and align center. Add components.
       setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
       add(checkJPanel);
       add(fieldJPanel);
       add(buttonJPanel);
    }
public static void main (String[] args){
      AlignGrid alignGrid = new AlignGrid();
        alignGrid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //set size of frame
        alignGrid.setSize(300,140);
        //display frame
        alignGrid.setVisible(true);
    }
    //end class
}
 //end constructor

The first assignment was to make a thermometer program with GUI. The second was to recreate a GUI, which should be accurate: if not, I'll scan the book and post it here.

um....
if someone wants a copy of bioshock on steam I'll gift it to you if you help me xD I have an extra....

Thanks in advance!
~Will

PS: here's the exact errors.

for the first program:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Farenheit:"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
        at java.lang.Float.parseFloat(Float.java:422)
        at GUI1$1.actionPerformed(GUI1.java:47)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Farenheit:"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
        at java.lang.Float.parseFloat(Float.java:422)
        at GUI1$1.actionPerformed(GUI1.java:47)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Farenheit:"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
        at java.lang.Float.parseFloat(Float.java:422)
        at GUI1$1.actionPerformed(GUI1.java:47)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Java Result: 1

for the second program:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:927)
at AlignGrid.<init>(AlignGrid.java:48)
at AlignGrid.main(AlignGrid.java:64)
Java Result: 1

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Ezzaral
0
String f = firstLabel.getText();
float fi = Float.parseFloat(f);

That is the source of your first error. You are trying to parse "Fahrenheit: " as a float. It is a String. And we don't get rewards for helping people here. Besides which, giving away free copies of BioShock is undoubtedly illegal, so I wouldn't be surprised if one of the moderators here edits that out of your post and gives you an infraction.

Edited by BestJewSinceJC: n/a

0
String f = firstLabel.getText();
float fi = Float.parseFloat(f);

That is the source of your first error. You are trying to parse "Fahrenheit: " as a float. It is a String. And we don't get rewards for helping people here. Besides which, giving away free copies of BioShock is undoubtedly illegal, so I wouldn't be surprised if one of the moderators here edits that out of your post and gives you an infraction.

Thanks for the help! Surprised I didn't already catch that.

Not seeing how it's illegal to give Steam gifts, as it's impossible to have pirated keys on Steam, but I'll remove it if it's against the rules.

0

Second one's finished; found the error.

I'm still getting errors in the first, however.... any help would be appreciated, thanks!

Edited by LucarioWill: n/a

0

Sorry, I meant to say the first one is fine and the second is still getting errors. What am I doing wrong? Help!!! :(

0

On line 45

fieldJPanel.add (fieldJPanel1,BorderLayout.NORTH);

you haven't initialized 'fieldJPanel1'. Looks like you mis-typed it up above and set up 'fieldJPanel' twice.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.