+ 2

I want to make jave code (two buttons that changes the background) but i have errors can someone help

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class colorbutton extends JFrame implements Actionlistener { private JButton red = new JButton("red"); private JButton blue = new JButton("blue"); public colorbutton() { getContentpane().setLayout(new FlowLayout()); getcontentpane().add(red); getcontentpane().add(blue); red.ActionListener(this); blue.ActionListener(this); } public void actionperformed(ActionEvent k) { if (k.getsource()== red) { getcontentpane().setBackground(color.RED); } else if (k.getsource()== blue) { getcontentpane().setBackground(color.BLUE); } } public static void main (string[] args) { colorbutton frame = new colorbutton(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setsize(300,200); } }

15th Aug 2023, 11:43 AM
ABDULLAH ESSAM
ABDULLAH ESSAM - avatar
6 Respostas
+ 4
This code doesn't compile. You have typos and some methods that you're trying to call don't exist. Have you tried using an IDE like IntelliJ or Eclipse to help you see these errors?
15th Aug 2023, 12:27 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 4
Well thx guys i corrected every error and it's working wow
15th Aug 2023, 1:18 PM
ABDULLAH ESSAM
ABDULLAH ESSAM - avatar
+ 3
As Marcos Chamosa Rodríguez said, the Sololearn compiler won't work with this code. You'll need another.
15th Aug 2023, 12:45 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
That's awesome, well done! Sorry I couldn't be of much help.
15th Aug 2023, 8:43 PM
Ausgrindtube
Ausgrindtube - avatar
0
Ausgrindtube I'm doing it on jcreator and i still have errors most of them is cannot find symbol
15th Aug 2023, 12:54 PM
ABDULLAH ESSAM
ABDULLAH ESSAM - avatar
0
Thank you Ausgrindtube
16th Aug 2023, 1:41 PM
ABDULLAH ESSAM
ABDULLAH ESSAM - avatar