Results 1 to 7 of 7
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default adding text fields newb question

    Hello I am 3 weeks into my programming and i am trying to build a simple calculator that can add 2 seperate text fields together and print result in the third text field. here is what i have so far. i will post the gui code and then backend code (did i use that term correctly?) that i wrote. as far as the backend code goes i used an example from the book i am teaching myself with that goes to a lottomadness game and i have tried to convert the code to suit my purpose but i am not sure that i went about it the correct way (especially since it wont compile the main file ).

    Java Code:
    package com.blue2525989;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class BrickCalculator extends JFrame {
        BrickCalculatorMain mathTwo = new BrickCalculatorMain(this);
             
        //add row1
        JPanel top = new JPanel();
        JLabel brickCalc = new JLabel("The Brick Calculator!", JLabel.CENTER);
        JLabel space = new JLabel("\n");
        //addsecondtoprow
        JPanel topTwo = new JPanel();
          JLabel madeBy = new JLabel("made by Jason Bierbrauer", JLabel.CENTER);
        //add row2
        JPanel inputPanel = new JPanel();
        JLabel brickNum = new JLabel("Input the amount of bricks per course:");
        JTextField numOfBrick = new JTextField(5);
        JLabel courseLeft = new JLabel("Enter the amount of courses left:");
        JTextField numOfCourses = new JTextField(5);
        //add row3
        JPanel answer = new JPanel();
        JLabel answerIs = new JLabel("You need:");
        JTextField answerText = new JTextField(5);
        JLabel bricks = new JLabel("bricks.");
        //add row4
        JPanel buttons = new JPanel();
        JButton compute = new JButton("Compute");
        JButton reset = new JButton("Reset-er");
        
    
    public BrickCalculator() {
        super("Brick Calculator");
        
        setSize(450,550);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout layout = new GridLayout(5,1,0,0);
        setLayout(layout);
        
        compute.addActionListener(mathTwo);
        reset.addActionListener(mathTwo);
        
        FlowLayout layout1 = new FlowLayout (FlowLayout.CENTER,
                    0,0);
        top.setLayout(layout1);
        top.add(brickCalc);
        top.add(space);
        add(top);  
        
        FlowLayout layout2 = new FlowLayout (FlowLayout.CENTER,
                    10,10);
        inputPanel.add(brickNum);
        inputPanel.add(numOfBrick);
        inputPanel.add(courseLeft);
        inputPanel.add(numOfCourses);
        add(inputPanel);
        
        FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,
        10,10);
        answer.add(answerIs);
        answer.add(answerText);
        answer.add(bricks);
        add(answer);
        
        FlowLayout layout4 = new FlowLayout (FlowLayout.CENTER,
        10,10);
        buttons.add(compute);
        buttons.add(reset);
        add(buttons);
        
         topTwo.add(madeBy);
         add(topTwo);
        
        setVisible(true);
    }
    
    private static void setLookAndFeel(){
        try{
            UIManager.setLookAndFeel(
                    "com.sun.java.swin.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception exc){
            //ignore error
        }
    }
    
    public static void main(String[] args){
        BrickCalculator.setLookAndFeel();
        BrickCalculator frame = new BrickCalculator();
    }
    }
    HERE IS THE CODE FOR THE BACKEND

    Java Code:
    package com.blue2525989;
    
    import java.math.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class BrickCalculatorMain implements ActionListener, Runnable{
        
    BrickCalculator gui;
    Thread math;
    
    public BrickCalculatorMain (BrickCalculator in) {
        gui = in;
    }
    
    
    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Compute")){
    
        }
        if (command.equals("Reset-er")){
            clearAllFields();
        }
    }
    
    void startMath(){
        math = new Thread(this);
        math.start();
    }
    
    void clearAllFields(){
        gui.numOfBrick.setText("0");
        gui.courseLeft.setText("0");
        
    }
    }
    any help or suggestions would be much appreciated thanks, Jason
    Last edited by Tolls; 09-30-2016 at 09:23 AM. Reason: Add code tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: adding text fields newb question

    "especially since it wont compile the main file"

    You need to show us the compilation issues then.

    Also, when posting code please use code tags.
    If you edit (or quote) your post you'll see what I've done.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: adding text fields newb question

    Oh sorry. Well the second file wont compile because it is not complete i believe. I am for sure missing lines of code. I need some direction to the proper tutorials so i can figure out what i need to do.

  4. #4
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: adding text fields newb question

    Oh sorry. Well the second file wont compile because it is not complete i believe. I am for sure missing lines of code. I need some direction to the proper tutorials so i can figure out what i need to do. And that does look much nice with the code tag. I will figure out how to do that.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: adding text fields newb question

    Yes, but what exactly is wrong with it?
    What are the errors?

    You shouldn't expect people to compile things to find out the errors themselves.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: adding text fields newb question

    Just to give you a start point: Since the BrickCalculator class is not abstract you have to create an instance of that class first and then execute the setLookAndFeel() method for that instance. Adjust your main() method accordingly.
    Last edited by benji2505; 09-30-2016 at 03:11 PM.

  7. #7
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: adding text fields newb question

    Thankyou i will try that when i get home tonight

Similar Threads

  1. Basic Newb question
    By Dark Knight in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-07-2014, 09:31 AM
  2. Android Java NewB Question...
    By dmagician in forum Android
    Replies: 0
    Last Post: 06-25-2012, 11:32 AM
  3. Replies: 2
    Last Post: 05-20-2012, 04:29 PM
  4. A newb question
    By JavaJunkie2341 in forum New To Java
    Replies: 2
    Last Post: 03-30-2012, 07:00 PM
  5. [SOLVED] Probably a tooootal newb question... but
    By proeliumfessus in forum New To Java
    Replies: 6
    Last Post: 04-05-2009, 07:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •