-2
package Kinematics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

public class Display extends JFrame implements Runnable{

private static final long serialVersionUID = 938633141149262027L;

static JButton b2 = new JButton("v/t");
static JButton b1 = new JButton("d/t");
static JFrame frame = new JFrame("Kinematics Conversions");
static JFrame frame2 = new JFrame("Kinematics Conversions");



public static void main(String[] args){
    frame.setVisible(true);
    JPanel p = new JPanel();
    frame.setPreferredSize(new Dimension(500, 500));
    b1.setBounds(5, 5, 5, 5);
    b1.addActionListener(ActionListener -> {
        public void actionPerformed(ActionEvent e){ //says actionPerformed cannot have void return type
            if(b2.getModel().isPressed()) { //however the oracle site says otherwise
                frame.setVisible(false);
                frame2.setVisible(true);
            }
        }
    });
    b2.setBounds(50, 50, 5, 5);
    p.add(b1);
    p.add(b2);
    frame.add(p);
    frame.pack();
}

@Override
public void run() {
    if(b2.getModel().isPressed()) {
        frame.setVisible(false);
        frame2.setVisible(true);
    }
}

}

Ok, so I have the correct syntax.. i think. I went onto the oracle website to look at the syntax and the proper return type however ECLIPSE OXYGEN 2.0 says that actionPerformed cannot have a return type of void. Why is that?

Ty Kutcher
  • 25
  • 3
  • I am quite sure your syntax is wrong. You either want `.addActionListener(new ActionListener{...` (annonymous class) or `.addActionListener((ActionEvent e) -> { ...` (add the body of `actionPerformed(...)` here). – Turing85 Jun 15 '18 at 19:49

1 Answers1

2

You have to make up your mind.

You can either use an anonymous inner class (as outlined here, there is an example for action listeners right there).

Or you use java8 style lambda expressions.

Your current code is just a mix of both ideas. Invalid syntax, coming out of the idea of putting together two completely different approaches.

GhostCat
  • 137,827
  • 25
  • 176
  • 248