#I want to make 3 menu buttons.

1 messages ยท Page 1 of 1 (latest)

digital totem
#

Should I make 3 classes that all inherit from JButton?

shell monolithBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

digital totem
#

wtf

#

ok bot

#

also I want to darken the button when it's hovered over or does it do that automatically

#

would I have to use a shader

#

I'm guessing not

shell monolithBOT
#

Changed the category to JavaFX|Swing.

#

<@&987246487241105418> please have a look, thanks.

digital totem
#

thanks

#

I forgot that it was swing

#

also I didn't realize you could override methods while instantiating something

#

that's interesting

#

oh when it's an interface

#

I see

#

how do I close the question guys

glacial leaf
#

details for that here:

shell monolithBOT
#

An interface defines a set of method signatures, as contract. It can greatly increase code modularity. Often, interfaces are property-driven.

interface CanWalk {
  void walkLeft();
  void walkRight();
}

There is no method body. So if a class implements CanWalk, he makes the promise to offer those methods:

class Player implements CanWalk {
  int x;

  @Override
  void walkLeft() {
    x--;
  }

  @Override
  void walkRight() {
    x++;
  }
}

Someone could now demand a CanWalk instance and use the methods:

class Mover {
  static void moveAround(CanWalk canWalk) {
    for (int i = 0; i < 10; i++) {
      canWalk.walkRight();
    }
    canWalk.walkLeft();
    canWalk.walkRight();
  }
}

Note that the moveAround accepts everything that can walk.

Mover.moveAround(player);

We could also give it a Dog, as long as it implements CanWalk.

You have two options to create instances of interfaces:

  1. Create a class that implements the interface, like Player
  2. Use an anonymous class:
CanWalk canWalk = new CanWalk() {
  @Override
  void walkLeft() {
    System.out.println("Walking left");
  }

  @Override
  void walkRight() {
    System.out.println("Walking right");
  }
};

An interface that only offers one method is called a functional interface:

@FunctionalInterface
interface IntOperation {
  int operate(int a, int b);
}

You have two additional options to create instances of it:
3. Lambda expression

IntOperation operation = (a, b) -> a * b;
System.out.println(operation.operate(5, 3)); // Prints 15
  1. Method reference
// Method in MathUtil
static int multiply(int a, int b) {
  return a * b;
}

// Use it as
IntOperation operation = MathUtil::multiply;
System.out.println(operation.operate(5, 3)); // Prints 15
glacial leaf
digital totem
#

?

#

it's a mouselistener

#

that's not anonymous is it?

#

it's an interface I thought

glacial leaf
#

yeah but when u write:

#
Foo foo = new Foo() {
  @Override
  ...
};
#

thats an anonymous class then

digital totem
#

ohh ok

#

cool

glacial leaf
#

similar to writing a new class:

class MyFoo implements Foo {
  @Override
  ...
}

and then using that:

Foo foo = new MyFoo();
#

just that it happens behind the curtain, hence anonymous

digital totem
#

well that seems easier

#

thanks

#

so it's not about what the actual class is defined as but more about how you write it

#

how you instantiate it rather

glacial leaf
#

anonymous classes are just a shorthand for making ur own class in ur own file. in fact, the compiler does replace it with that

digital totem
#

makes sense

#

thanks bro

glacial leaf
#

๐Ÿ‘Œ

digital totem
#

will definitely keep in mind in the future