Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java GUI

Options
  • 29-07-2016 6:26pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi I'm making a simple Java GUI for fun, have been watching tutorials on YouTube. They all seem to use a constrictor. Why is this? I have a template of mine made out and I have it all in the same class? Can someone else clear up my confusion. Thank you


Comments

  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method:
    • A constructor doesn’t have a return type.
    • The name of the constructor must be the same as the name of the class.
    • Unlike methods, constructors are not considered members of a class.
    • A constructor is called automatically when a new instance of an object is created.
    • A constructor allows you to provide initial values for class fields when you create the object.

    More info here


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Thanks I know how to make one but I don't understand the point I can easily put my whole program into one class with no constructor but all YouTube tutorials use them


  • Registered Users Posts: 1,461 ✭✭✭Anesthetize


    Thanks I know how to make one but I don't understand the point I can easily put my whole program into one class with no constructor but all YouTube tutorials use them
    Object-oriented programming will make your head explode :)

    Before attempting to make a Java GUI perhaps it's better to get the fundamentals of Java classes down first. Learn how class attributes, constructors and methods work.


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    Thanks I know how to make one but I don't understand the point I can easily put my whole program into one class with no constructor but all YouTube tutorials use them

    They are a fundamental of OO programming. If you don't undserstand the point of them then its worth doing a fundamentals course first. Something like this is free and although targeted towards android covers principals of OO programming


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    I have a template of mine made out and I have it all in the same class?
    A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created.
    Thanks I know how to make one but I don't understand the point I can easily put my whole program into one class

    Something tells me that not everyone has the same understanding of the term "constructor"!?
    Did you really mean constructor chocolate boy123? Or perhaps you meant the examples were using Controllers? Maybe provide a link to one of the examples and it can be better explained.


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I have now my GUI finished ... I just need to format the answers to 2 decimal points. I know how to do this normally but how can I do it to a JTextBox?


  • Registered Users Posts: 262 ✭✭guylikeme


    Format them, use the toString() method on the object and use the JTextBox's append or add method for it. Job done


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Have everything finished now thank ye!


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Now that you have that under your belt you could do the offical Java Tutorials there quite good and teaching the fundamentals and make a great starting point.


Advertisement