#help with project action listener

153 messages · Page 1 of 1 (latest)

sinful estuary
#

i need help with action listners in my java project if someone could help me do it it would be great ,basically i have to make a gui that where a user selects the arrow that is dsiplayed on the screen i have made the gui but just the action listneers need to be done it has to be done in java ,.basically works like this The four buttons show arrows point up, down, left and right respectively.
• When the user presses the “Play” button, an arrow should be shown in the middle, as shown in Figure 2. The
user should click on the corresponding arrow.
• If the user does click on the correct arrow, then their score should be incremented by 1. If the user clicks on
the incorrect arrow, then their score should be reset to 0.
• Once the user has clicked on an arrow, correct or incorrect, a new middle arrow should be randomly chosen
– this is now the arrow the user clicks on.
• If the user clicks on the “Reset” button, the score should be reset to 0 and the central image should be reset
to a blank image

jolly otterBOT
#

Hey, @sinful estuary!
Please remember to /close this post once your question has been answered!

dusky berry
#

u did something?

#

@sinful estuary

sinful estuary
dusky berry
#

its ok heere

sinful estuary
#

alright so the gui looks like this

dusky berry
#

just dont totally ask like whole project do something we can say problem

sinful estuary
#

oh

#

no no i just need help with the action listners

#

i am struggling to do em

dusky berry
#

oh

#

can u put code

sinful estuary
#

i made the gui but my action listner isnt working

#

gimme 2 mins ill show you it

dusky berry
#

maybe u frogot to add actiaon list

#

listener

dusky berry
sinful estuary
# dusky berry remake the image of the arrows and dont keep background white use transparent co...

play.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JOptionPane.showMessageDialog(null, "You pressed a button!");
            Random rd = new Random();
            int i = rd.nextInt(20);
              if(i%4 == 0)
                  
                  
                  arrow.setIcon(RightIcon);


                  else if(i%4 == 1)

                  arrow.setIcon(LeftIcon);

                  else if(i%4 == 2) 

                  arrow.setIcon(UpIcon);

                  else

                  arrow.setIcon(DownIcon);
        }
    });
jolly otterBOT
#
play.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JOptionPane.showMessageDialog(null, "You pressed a button!");
                Random rd = new Random();
                int i = rd.nextInt(20);
                  if(i%4 == 0)
                      
                      
                      arrow.setIcon(RightIcon);


                      else if(i%4 == 1)

                      arrow.setIcon(LeftIcon);

                      else if(i%4 == 2) 

                      arrow.setIcon(UpIcon);

                      else

                      arrow.setIcon(DownIcon);
            }
        });
sinful estuary
#

this is the action listner for the play button when i press the play button i want the centre white box to display a random arrow

dusky berry
#

can u share full code

sinful estuary
dusky berry
#

i think so u forgot to add listener to the button

#

no one will

sinful estuary
#

ok ok

dusky berry
sinful estuary
#

gime a sec

dusky berry
#

u have waisted alot of code lol

#

u use ide?

sinful estuary
#

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import javax.swing.JFrame;

import java.util.*;

public class Main {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Random rd = new Random();
    JFrame frame = new JFrame();
    
    
      Icon UpIcon = new ImageIcon("Up.png");
      Icon LeftIcon = new ImageIcon("Left.png");
      Icon DownIcon = new ImageIcon("Down.png");
      Icon RightIcon = new ImageIcon("Right.png");
      Icon blank = new ImageIcon("Blank.png");
    
    
    JButton down = new JButton(new ImageIcon("Down.png")); 
    JButton left = new JButton(new ImageIcon("Left.png")); 
    JButton right = new JButton(new ImageIcon("Right.png")); 
    JButton up = new JButton(new ImageIcon("Up.png")); 
    JButton play = new JButton("Play");
    JButton reset = new JButton("Reset");
    

    //labels 
     JLabel score = new JLabel("Score:0");

      JLabel arrow = new JLabel(blank);
    

    //panels
    JPanel p1 = new JPanel();
    p1.setBounds(0, 0, 500, 70);
    JPanel p2 = new JPanel();
    p2.setBounds(0, 70, 500, 70);
    JPanel p3 = new JPanel();
    p3.setBounds(0, 140, 500, 70);
    
    //play reset
    JPanel p4 = new JPanel();
    p4.setBounds(0, 210, 500, 50);
dusky berry
#

just paste full text it will take as file

sinful estuary
#

where do i get the file from ?

dusky berry
#

just paste here text then option will come then remove the message

sinful estuary
dusky berry
#

currently how many lines of code?

dusky berry
sinful estuary
dusky berry
#

just fill the boundary with transparent color

sinful estuary
dusky berry
dusky berry
sinful estuary
dusky berry
#

yes

#

but ok

#

if u want u can do it will look good

sinful estuary
#

i dont wnan loose marks he doesnt care about it really

#

he mainly wants functionanlity

#

but this action listner wonr work

dusky berry
dusky berry
sinful estuary
#

it says that when i press play

dusky berry
#

u should always put exception lol

#

its compilation error

sinful estuary
#

wdum

dusky berry
#

e

#

ahhh discord bug

sinful estuary
#

java is so confusing to me im a noob

dusky berry
#

but idc

sinful estuary
#

so how do i fix it ?

dusky berry
#

whenever exception come try to understand first line
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:

sinful estuary
#

yeah

#

so what i do to fix ;-;

#

this compilation error

dusky berry
#

its not compilation sorry

#

compilation is meannig like syntax error and stuff

#

syntax error means like when u didnt closed bracket and more

sinful estuary
#

ohh

dusky berry
#

can u try frame.repaint

#

in last line of action listener'

sinful estuary
#

ok

sinful estuary
dusky berry
#

frame.repaint(); 😦

sinful estuary
#

still no work tho

dusky berry
#

can u give the images

sinful estuary
#

oki

#

sent the down arrow twuce

dusky berry
sinful estuary
#

no clue but its all working tho

dusky berry
#

can u put ss eclipse

sinful estuary
dusky berry
#

here

#

of eclipse

sinful estuary
#

ok

sinful estuary
dusky berry
#

bruh

#

ahh

#

idk what

sinful estuary
#

;c

dusky berry
#

import javax.*;

#

remove thhis

#

why u add this?

sinful estuary
#

i removed it now

#

still same

dusky berry
#

how it is possible its working for me

#

u are tapping play button right

sinful estuary
#

its showing

#

a image

#

when u tap play button ?

dusky berry
#

yes

sinful estuary
dusky berry
sinful estuary
#

it gives an errow whn i tap play

dusky berry
#

how

#

JLabel arrow = new JLabel(blank);
-> final JLabel arrow = new JLabel(blank);

#

try

sinful estuary
dusky berry
#

comment this
// JOptionPane.showMessageDialog(null, "You pressed a button!");

dusky berry
#

do one thing

#

@sinful estuary

#

go widnows then preferences

sinful estuary
#

im here

#

in prefrences

dusky berry
sinful estuary
dusky berry
#

its an eclipse problem

sinful estuary
#

done

#

so now what do i do

sinful estuary
#

im trying netbeans but it aint wroking

#

it aint showing the images nothing

dusky berry
#

go to file explorer then

sinful estuary
dusky berry
#

just put all picture in addingment

#

assigmnent

#

no need file explorter

sinful estuary
#

LOL

#

ty man

#

eclipse is just shit ig

dusky berry
sinful estuary
#

do you know how i can make my netbeasn look like eclipse wiht the colors and stuff

dusky berry
#

go to preferences somehting

sinful estuary
#

ok

sinful estuary
dusky berry