0

I'm newbie in Java and I have problems with my main menu for my game. I've followed informations from internet but, I still can't fix it. The errors are in my main form, public class Screen extends JFrame implements ActionListener { and Start fram2 = new Start(); Please help me

Here's my codes : -- Main Form

    package screen;
//import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.awt.event.InputEvent;
//import java.awt.event.KeyEvent;
import javafx.event.ActionEvent;

 public class Screen extends JFrame implements ActionListener {

    public Screen() {
    JFrame f = new JFrame("Menu Demo");
    f.setSize(800, 600);

    f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();

    JMenu jmFile = new JMenu("File");
    JMenuItem jmiOpen = new JMenuItem("Create Player");
    JMenuItem jmiClose = new JMenuItem("Close");

    JMenuItem jmiExit = new JMenuItem("Exit");
    JPanel p = new JPanel();
    //Container c = getContentPane();

     JButton start = new JButton("START!");
     JButton instructions = new JButton ("INSTRUCTIONS");
     JButton close = new JButton("CLOSE!");

      jmFile.setMnemonic(KeyEvent.VK_F);
      jmiOpen.setMnemonic(KeyEvent.VK_O);
      jmiClose.setMnemonic(KeyEvent.VK_S);
      jmiExit.setMnemonic(KeyEvent.VK_X);

      jmiOpen.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_O,
        InputEvent.CTRL_DOWN_MASK
        ));

      jmiClose.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_S,
        InputEvent.CTRL_DOWN_MASK
        ));

      jmiExit.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_X,
        InputEvent.CTRL_DOWN_MASK
        ));


    jmFile.add(jmiOpen);
    jmFile.add(jmiClose);

    jmFile.addSeparator();
    jmFile.add(jmiExit);
    jmb.add(jmFile);
    p.add(start);
      p.add(instructions);
      instructions.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == instructions){
        setVisible(false);
        //Start fram2 = new Start();  
       Start fram2 = new Start();
        dispose();
    }
    }
});

      p.add(close);  
      f.add(p,"South");

      start .addActionListener((ActionListener) this);
      instructions.addActionListener((ActionListener) this);
      close .addActionListener((ActionListener) this);

   // JMenu jmOptions = new JMenu("Options");
   // JMenu a = new JMenu("A");
   // JMenuItem b = new JMenuItem("B");
   // JMenuItem c = new JMenuItem("C");
    JMenuItem d = new JMenuItem("D");
   // a.add(b);
    //a.add(c);
   // a.add(d);
   // jmOptions.add(a);

    JMenu e = new JMenu("E");
    e.add(new JMenuItem("F"));
    e.add(new JMenuItem("G"));


    JMenu jmHelp = new JMenu("Help");
    JMenuItem jmiAbout = new JMenuItem("About");
    jmHelp.add(jmiAbout);
    jmb.add(jmHelp);

    jmiOpen.addActionListener((ActionListener) this);
    jmiClose.addActionListener((ActionListener) this);

    jmiExit.addActionListener((ActionListener) this);
    d.addActionListener((ActionListener) this);
    jmiAbout.addActionListener((ActionListener) this);

    f.setJMenuBar(jmb);
    f.setVisible(true);

    JLabel img = new JLabel(new ImageIcon("D:/SpaceShip.jpg"));
         f.add(img,"North");

    p.setLayout(new FlowLayout());
    }

    public static void main(String[] args) {
        new Screen();
    }

    public void actionPerformed(ActionEvent arg0) { // try to use actionPerformed(java.awt.event.ActionEvent) but still has error
        // Do something here

    }

}

--2nd class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 class Start extends JFrame{
   JFrame fram2 = new JFrame("About");  

   public  Start(){

setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel jl1 = new JLabel ("    ABOUT THE DEVELOPER   ");
JLabel jl2 = new JLabel ("3 Mahasiswa Sistem Informasi dari Universitas Tarumanagara");
JLabel jl3 = new JLabel ("Mereka adalah : ");
JLabel jl4 = new JLabel (" \t1. Meiliana Setiady, 825160021");
JLabel jl5 = new JLabel (" \t2. James Natasalim, 825160023");
JLabel jl6 = new JLabel (" \t3. Maya Retnosari, 825160030");
JLabel jl7 = new JLabel (" Selamat menikmati permainan dari kami ^__^");

JPanel p2 = new JPanel();
p2.add(jl1);
p2.add(jl2);
p2.add(jl3);
p2.add(jl4);
p2.add(jl5);
p2.add(jl6);
p2.add(jl7);
getContentPane().add(p2);
//fram2.add(p2);

//fram2.setSize(800,600);
setVisible(true);
}

}

still has error in 1st form packages : (import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent;) and in the row 'instructions.addActionListener(new ActionListener()'

The red line said : java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class ActionListener location: class java.awt.event at screen.Screen.(Screen.java:10) Exception in thread "main" Java returned: 1 BUILD FAILED (total time: 1 second)

Did I miss something?

  • yes,you miss to post your error – John Joe Dec 11 '17 at 14:32
  • Umm.. I've post my error and those are on top. Only row 'public class Screen extends JFrame implements ActionListener {' and row 'Start fram2 = new Start();' has red signs –  Dec 11 '17 at 14:36
  • what the red line said ? – John Joe Dec 11 '17 at 14:39
  • java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: Uncompilable source code - screen.Screen is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener at screen.Screen.(Screen.java:17) Exception in thread "main" Java returned: 1 BUILD FAILED (total time: 1 second) –  Dec 11 '17 at 14:45

1 Answers1

1

Screen is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener at screen.Screen.

You have to implement actionPerformed(java.awt.event.ActionEvent) method

 @Override
    public void actionPerformed(ActionEvent arg0) {
        // Do something here

    }

In your second class,

JLabel jl1 = new JLabel(" \tABOUT THE DEVELOPER\t", 30);

You cannot have an int. It has to be one of the following constants

  • List item
  • LEFT (2)

  • CENTER (0)

  • RIGHT (4)

  • LEADING (10)

  • TRAILING (11)**

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • Uhh thank you the main program is show, but the instructions button still not working. Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: screen.Screen.Start.setVisible at screen.Screen$1.actionPerformed(Screen.java:75) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at –  Dec 11 '17 at 15:00
  • You can refer https://stackoverflow.com/questions/31078640/java-exception-in-thread-awt-eventqueue-0-java-lang-illegalargumentexception – John Joe Dec 11 '17 at 15:11
  • Okay, I've followed your way and It still doesn't work. But, I've tried to configure my 2nd class, I removed the public class and the row 'public class Screen extends JFrame implements ActionListener {' is not error but, there are still several errors on 1st form in package. import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; also the row 'instructions.addActionListener(new ActionListener() ' is still error. I tried to implement actionPerformed(java.awt.event.ActionEvent) method as you said before, but It still has error –  Dec 11 '17 at 15:32
  • You just need to remove the 30 and it should works. `JLabel jl1 = new JLabel(" \tABOUT THE DEVELOPER\t");` – John Joe Dec 11 '17 at 15:34
  • Yeah, I think so but, now the packages are error, (import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent;) . Also the 'instructions.addActionListener(new ActionListener()' still error. I don't know why . Umm sorry for asking you a lot –  Dec 11 '17 at 15:38
  • Okay, I've updated my post. Please check at my source codes –  Dec 11 '17 at 15:48
  • remove `import javafx.event.ActionEvent;` – John Joe Dec 11 '17 at 15:57
  • Okay, I've removed it but now the packages in 1st main still has errors. The errors are still same ( import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent;) also, the row 'instructions.addActionListener(new ActionListener() ' still has an error, and I got additional errors. 'public void actionPerformed(ActionEvent e) {' and 'public void actionPerformed(ActionEvent arg0) { // try to use actionPerformed(java.awt.event.ActionEvent) but still has error // Do something here }' –  Dec 11 '17 at 16:05
  • but you said the main program is shown just now, only instruction button not working ? – John Joe Dec 11 '17 at 16:09
  • and where your second class package? You miss to declare ? – John Joe Dec 11 '17 at 16:10
  • A few minutes ago, but after I ran my program, it changed. I don't know too why It can be happens. Umm did you try to ran on your netbeans? –  Dec 11 '17 at 16:12
  • yes,it works fine after removed `import javafx.event.ActionEvent;` – John Joe Dec 11 '17 at 16:13
  • Really? But, why mine is still not working.. o.O I think, I didnt miss anything from you said before –  Dec 11 '17 at 16:16
  • Sorry, would you like to put your codes here? I wanna compare with my own, maybe I miss something (?) If you don't mind –  Dec 11 '17 at 16:17