It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

functional interface in java

page: 1
0

log in

join
share:

posted on Apr, 12 2023 @ 07:25 AM
link   
In Java, I have a difficult task to complete. I must respond to mouse clicks with the code supplied as a lambda expression (print 'ok' to console). The issue is that I cannot alter the Main class; instead, I must construct a class/interface to receive lambda expressions. I attempted to follow Lambda expression instructions, but I kept getting an error about utilising a non-functional interface. Main:


import javax.swing.*;

public class Main [

public static void main(String[] args) [
SwingUtilities.invokeLater( ()-> [
JFrame f = new JFrame();
JButton b = new JButton("Mouse Click");
b.addMouseListener ( (MousePressListener) e -> System.out.println("ok") );
f.add(b);
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
]
);
]
]


Could you individuals with more experience give me any pointers on how to approach this problem? I started by constructing a class based on the scaler docs, but I got lost (I made this class and used 'implements,' thus type MousePressListener is also type MouseListener, right?)


public class MousePressListener implements MouseListener [

@Override
public void mouseClicked(MouseEvent e) [
// TODO Auto-generated method stub

]

@Override
public void mouseEntered(MouseEvent e) [
// TODO Auto-generated method stub

]

@Override
public void mouseExited(MouseEvent e) [
// TODO Auto-generated method stub

]

@Override
public void mousePressed(MouseEvent e) [
// TODO Auto-generated method stub

]

@Override
public void mouseReleased(MouseEvent e) [
// TODO Auto-generated method stub

]

]



posted on Apr, 12 2023 @ 09:00 AM
link   
Have you tried 'include main' at the top in your class?



posted on Apr, 12 2023 @ 09:00 AM
link   
JAVA is not a language I know too well but the folks over at stackoverflow will be able to help you.

It reads like you are struggling with the concept of OOP at whole, you might want to take a look at that again and make sure you understand.

I guess this is what the teacher wants you to do



posted on Apr, 17 2023 @ 12:48 AM
link   
a reply to: kwakakev

i dont think so also thank you for your reply
edit on 17-4-2023 by Mobo01 because: (no reason given)



posted on Apr, 17 2023 @ 12:50 AM
link   
a reply to: TDDAgain

yeah i asked there but did not recieved a right answer yet
Thanks tho



posted on Jun, 4 2023 @ 05:37 AM
link   
 


off-topic post removed to prevent thread-drift


 



new topics

top topics
 
0

log in

join