#I want to make 3 menu buttons.
1 messages ยท Page 1 of 1 (latest)
<@&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>.
No, you don't need to create 3 separate classes for each menu button. You can simply create 3 instances of JButton and customize them individually as needed. This will make your code more concise and easier to maintain.
Useful links:
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
Changed the category to JavaFX|Swing.
<@&987246487241105418> please have a look, thanks.
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
sounds like ur doing an anonymous class
details for that here:
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:
- Create a class that
implementsthe interface, likePlayer - 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
- 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
u can use /help-thread close ๐
?
it's a mouselistener
that's not anonymous is it?
it's an interface I thought
yeah but when u write:
Foo foo = new Foo() {
@Override
...
};
thats an anonymous class then
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
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
anonymous classes are just a shorthand for making ur own class in ur own file. in fact, the compiler does replace it with that
๐
will definitely keep in mind in the future