Java anonymous objects 🃏

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ธ.ค. 2024
  • Java anonymous objects tutorial
    #java #anonymous objects

ความคิดเห็น • 54

  • @BroCodez
    @BroCodez  4 ปีที่แล้ว +14

    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class Main {

    public static void main(String[] args) {

    JFrame frame = new JFrame();
    ArrayList deck = new ArrayList();

    /*
    ImageIcon AC = new ImageIcon("src\\cards\\1.png");
    JLabel AClabel = new JLabel(AC);
    deck.add(AClabel);

    ImageIcon TwoC = new ImageIcon("src\\cards\\2.png");
    JLabel TwoClabel = new JLabel(TwoC);
    deck.add(TwoClabel);

    ... repeat for all 52 cards :(
    */

    for(int i =1;i

    • @joyceasante8292
      @joyceasante8292 ปีที่แล้ว

      "Hard"way to add objects
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;
      public class Main
      {
      public static void main (String[]args)
      {
      JFrame frame = new JFrame ();
      ArrayList < JLabel > deck = new ArrayList < JLabel > ();
      ImageIcon AC = new ImageIcon ("src\\cards\\1.png");
      JLabel AClabel = new JLabel (AC);
      deck.add (AClabel);
      ImageIcon twoC = new ImageIcon ("src\\cards\\2.png");
      JLabel twoClabel = new JLabel (twoC);
      deck.add (twoClabel);
      frame.add (deck.get (0));
      frame.add (deck.get (1));
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.setSize (380, 380);
      frame.setLayout (new FlowLayout ());
      frame.setVisible (true);
      }
      }
      ______________________________
      SIMPLE WAY
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;
      public class Main
      {
      public static void main (String[]args)
      {
      JFrame frame = new JFrame ();
      ArrayList < JLabel > deck = new ArrayList < JLabel > ();
      for (int i = 1; i

  • @angelcastineira2561
    @angelcastineira2561 4 ปีที่แล้ว +12

    creating the array is so clever! at first i thought it was a little redundant since you could this and save one step
    frame.add(new JLabel(new ImageIcon("src\\cards\\"+i+".png")));
    but then I realized that creating and filling the array, you can use the array positions as "names" for the objects so you can correct a specific Jlabel later.
    beautiful!

  • @DNA912
    @DNA912 4 ปีที่แล้ว +1

    Not what I was looking for. But something I never thought of doing and glad to know now.

  • @yenegew-tech
    @yenegew-tech 2 ปีที่แล้ว

    No words to tell How much u'r helping me

  • @ChrisMusicRSA
    @ChrisMusicRSA 3 ปีที่แล้ว +2

    This may just be me... but I feel like this is like watching a really entertaining gaming video. It might be the colors, idk.
    Thx for the great explanation! :P

  • @girl6994
    @girl6994 4 ปีที่แล้ว +1

    Line NO.10. We don’t need to type two times JLabel in if you we are using JDK 8 or above.

  • @owenagnel4139
    @owenagnel4139 4 ปีที่แล้ว +2

    Great vid as usual!

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +1

      thanks man!

  • @khiemdo5527
    @khiemdo5527 2 ปีที่แล้ว

    Another great video. Thanks a lot. Happy New Year and wishing you all the best.

  • @usamanadeem145
    @usamanadeem145 3 ปีที่แล้ว

    Great

  • @percivalgebashe4376
    @percivalgebashe4376 ปีที่แล้ว

    Nice

  • @tharinduperera8758
    @tharinduperera8758 2 ปีที่แล้ว

    i think using this for swing is not the best idea as naming the object has flexibilty to use them for styling and other action and mouse listners etc.. but while programing this can be used here and there to remove the redundancy and unnecessory codes.

  • @kemann3815
    @kemann3815 3 ปีที่แล้ว

    Best of the best

  • @wolanus
    @wolanus 4 ปีที่แล้ว

    great tutorial

  • @mindreader860
    @mindreader860 3 ปีที่แล้ว

    Great♥

  • @Dygit
    @Dygit 4 ปีที่แล้ว +1

    Very cool video :) Now that I think about it, I probably could have been more efficient by using this method in one of my prior projects

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +6

      Get used to that. As you gain new skills, you find more shortcuts to things you have coded previously lol

  • @davecone2063
    @davecone2063 3 ปีที่แล้ว

    Coolies! Thank you for the lesson.

  • @anthonydeluca9832
    @anthonydeluca9832 ปีที่แล้ว

    This is also likely more efficient in memory as you don't have a object in multiple places in memory I.E.
    Bunny bunny = new Bunny(); --- adds object to memory
    Pet pet = new Pet(bunny); --- creates another object in memory and stores the location of bunny as well (I think?)
    vs
    Pet pet = new Pet(new Bunny()); -- the Bunny no longer exists afterwards and doesn't need a pointer to it so all the info is in pet and is still accessible
    PLEASE DO NOT TAKE THIS AS PROFESSIONAL ADVISE -- this is my personal interpretation of the benefits and I could be WRONG, if so please make me aware. Either way it still saves lines of code and effort.

  • @eugenezuev7349
    @eugenezuev7349 6 หลายเดือนก่อน

    helpful, thanks

  • @cll2598
    @cll2598 ปีที่แล้ว

    Legend as always

  • @tasneemayham974
    @tasneemayham974 ปีที่แล้ว

    This is to defeat the EVIL TH-cam ALGORITHM.
    GO, BRO!!

  • @Electronic__Project
    @Electronic__Project 2 ปีที่แล้ว +1

    Where can i find pictures Like u have BRO ?

  • @developerjunior446
    @developerjunior446 3 ปีที่แล้ว +1

    Can you make video your whole day as Programmer ?

  • @mahdib9361
    @mahdib9361 4 ปีที่แล้ว +2

    jAVAFX TUTURIAL PLEASE

  • @nawfalnjm5699
    @nawfalnjm5699 3 ปีที่แล้ว

    omg . Just 3 lines of code , thanks !

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 ปีที่แล้ว

    thanks bro

  • @daniyalahsen5707
    @daniyalahsen5707 4 ปีที่แล้ว

    AOA, Man you are under-rated. You really should have more subs than you already have (by the quality of your work). Also, let's say I want to download your videos on MP4 quality, how may I so that? (Legally)

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +1

      You're comment ended up in my spam folder lol. Thank you. However I am not sure how to download TH-cam videos

    • @daniyalahsen5707
      @daniyalahsen5707 4 ปีที่แล้ว

      @@BroCodez wow Google sure knows what is spam and what is not. Wondering where all the 200 IQ algorithm experts went. Also AOA stands for Assalam O Alaikum which is Arabic for may you live in peace (just if you were wondering). AOA again.

  • @sidof8065
    @sidof8065 4 ปีที่แล้ว

    good tuto

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      thanks for watching Sidof

  • @KentHervey
    @KentHervey 4 ปีที่แล้ว

    Thanks

  • @nihadyaqublu2359
    @nihadyaqublu2359 2 ปีที่แล้ว

    this is genius

  • @kingkock1
    @kingkock1 2 ปีที่แล้ว

    We're cooking with petrol now!!

  • @MrJIsBack
    @MrJIsBack 3 ปีที่แล้ว

    But isn't it considered bad practice to just skip naming variables to make readable code?
    I've been forcing myself to do so thinking it was a better idea, but I didn't know not naming objects was actually considered to be a valid method...

  • @desmiles1567
    @desmiles1567 3 ปีที่แล้ว

    🤩

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

  • @vuchinh9989
    @vuchinh9989 3 ปีที่แล้ว

    Cool!

  • @thiagobrittosouza
    @thiagobrittosouza 3 ปีที่แล้ว

    thanks again bro

  • @noah77
    @noah77 4 ปีที่แล้ว +1

    Can I know where are you from??
    (Not intending to be bad)
    For me, I am from 🇬🇧🇬🇧

    • @noah77
      @noah77 4 ปีที่แล้ว +1

      @@BroCodez nice to meet you bud:)

    • @noah77
      @noah77 4 ปีที่แล้ว +2

      @@BroCodez love your java tutorials:)

  • @manuelgonzalezpalafox2627
    @manuelgonzalezpalafox2627 2 ปีที่แล้ว

    Ly bro 7

  • @pesterlig
    @pesterlig 4 ปีที่แล้ว

    Danke

  • @zari_723
    @zari_723 2 ปีที่แล้ว

    hi :)

  • @chzzh6634
    @chzzh6634 3 ปีที่แล้ว

    I'm so lazy to continue. HELP :(

  • @girl6994
    @girl6994 4 ปีที่แล้ว +1

    I don’t know how to download these pictures so I can’t type same project with you.

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      I made them myself. This is more of a demonstration, but any pictures or images could work

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      holy crap, that would be awesome. You could write a sub-program for each card for each card's effects. I'm now imagining all the work that went into a game like Magic The Gathering Online

    • @chzzh6634
      @chzzh6634 3 ปีที่แล้ว

      @@VXFluff ayy bro. u made good music. naisu

  • @ashish8-v4g
    @ashish8-v4g 25 วันที่ผ่านมา

    import java.util.Scanner;
    class AE extends Exception {
    AE(String m) {
    super(m);
    }
    }
    class Main {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int age;
    age = s.nextInt();
    try {
    if (age < 18) {
    throw new AE("You are not 18");
    } else {
    System.out.println("You are 18+");
    }
    } catch (AE e) {
    System.out.println("An error occurred: " + e.getMessage());
    }
    }
    }
    other way around

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 ปีที่แล้ว

    Thanks