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
#help with project action listener
153 messages · Page 1 of 1 (latest)
Hey, @sinful estuary!
Please remember to /close this post once your question has been answered!
can we go dms ?
its ok heere
just dont totally ask like whole project do something we can say problem
remake the image of the arrows and dont keep background white use transparent color
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);
}
});
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);
}
});
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
can u share full code
can i send it in dms i dont want people copying my code xD
ok ok
its ook just put here idk why someone would
yeah i use ecliplse
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);
put as file
just paste full text it will take as file
where do i get the file from ?
just paste here text then option will come then remove the message
currently how many lines of code?
paste full xd
u have piskel or any other simple photo editing?
just fill the boundary with transparent color
i dont the photos i used there where what was given to us
do ctrl shift f
lol the photos look bad
the ones of the arrows ?
i dont wnan loose marks he doesnt care about it really
he mainly wants functionanlity
but this action listner wonr work
do this in eclipse
is it throwing any exception or soemhtig?
it says that when i press play
wdum
java is so confusing to me im a noob
but idc
so how do i fix it ?
whenever exception come try to understand first line
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
its not compilation sorry
compilation is meannig like syntax error and stuff
syntax error means like when u didnt closed bracket and more
ohh
ok
doesnt work
frame.repaint(); 😦
can u give the images
why the red lines coming?
no clue but its all working tho
can u put ss eclipse
put a screenshot in ecliplse ?
ok
;c
to import all imports one time
i removed it now
still same
wait
its showing
a image
when u tap play button ?
yes
wtf
when u tap play it shows a random pic right?
no
it gives an errow whn i tap play
how
JLabel arrow = new JLabel(blank);
-> final JLabel arrow = new JLabel(blank);
try
no work
comment this
// JOptionPane.showMessageDialog(null, "You pressed a button!");
sorry was testing nebeans
im here
in prefrences
undo the comment and the final adding
ok
its an eclipse problem
in netbeans where would i add the images
im trying netbeans but it aint wroking
it aint showing the images nothing
go to file explorer then
wanan get married :>
LOL
ty man
eclipse is just shit ig
do you know how i can make my netbeasn look like eclipse wiht the colors and stuff
go to preferences somehting
ok
hey u still awake ?
i never used netbeen u can always search in google u get easy ways