Change Background Color of Jpanel in Java
#1
-
- New D.I.C Head
Reputation: 0
- Posts: 37
- Joined: 08-November 11
I Can't change JPanel Background colour.
Posted 27 November 2011 - 07:50 AM
I'm just started out learning about Swing and i'm trying to write a program with a simple button which will change the background colour of the panel.
here is what i have so far
class MyWindowListener extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { System.out.println("Closing window!"); System.exit(0); } } class MyButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("test"); panel.setBackground(Color.yellow); } } public class FredGui5 { public static void main(String[] args) { JFrame frame = new JFrame("Fred plays with buttons...again!"); frame.setVisible(true); frame.setSize(400, 400); frame.addWindowListener(new MyWindowListener()); JPanel panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.NORTH); GridBagConstraints c = new GridBagConstraints(); JButton button = new JButton("Push me for yellow"); c.gridx = 0; c.gridy = 1; button.addActionListener(new MyButtonListener()); panel.add(button, c); frame.add(panel); } }
in the actionPerformed method of the buttonlistener class there is an error on the "p.setBackground(Color.yellow);" line. It says that it cannot find variable panel in the mybuttonlistener class. How do i alter the code so panel can be seen by that class?
Is This A Good Question/Topic? 0
#2 GregBrannon
Reputation: 2250
- Posts: 5,340
- Joined: 10-September 10
Re: I Can't change JPanel Background colour.
Posted 27 November 2011 - 09:18 AM
"panel" is local to your main() method, so is not visible to your MyButtonListener class. How might you make "panel" visible to your other classes?
#3 nexus490
-
- New D.I.C Head
Reputation: 0
- Posts: 37
- Joined: 08-November 11
Re: I Can't change JPanel Background colour.
Posted 27 November 2011 - 10:01 AM
I can't declare it public can? I've also tried moving the "JPanel panel = new JPanel();" line to outside of the main and into the buttonlistenerclass but that causes other errors.
#4 GregBrannon
Reputation: 2250
- Posts: 5,340
- Joined: 10-September 10
Re: I Can't change JPanel Background colour.
Posted 27 November 2011 - 10:16 AM
I suggest you try something like:
public class FredGui5 { // declare panel to be a class variable here public static JPanel panel; public static void main(String[] args) { JFrame frame = new JFrame("Fred plays with buttons...again!"); // . . . panel = new JPanel(); // . . . } static class MyButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("test"); panel.setBackground(Color.yellow); } } }
As your approach to coding GUIs evolves beyond running everything from the static main() method, your construction of a small app like this will change so that little, if anything has to be static.
This post has been edited by GregBrannon: 27 November 2011 - 10:17 AM
#5 nexus490
-
- New D.I.C Head
Reputation: 0
- Posts: 37
- Joined: 08-November 11
Re: I Can't change JPanel Background colour.
Posted 27 November 2011 - 11:30 AM
The code is now as follows:
public class FredGui5 { public static JPanel panel; public static void main(String[] args) { JFrame frame = new JFrame("Fred plays with buttons...again!"); frame.setVisible(true); frame.setSize(400, 400); frame.addWindowListener(new MyWindowListener()); panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.NORTH); GridBagConstraints c = new GridBagConstraints(); JButton button = new JButton("Push me for yellow"); c.gridx = 0; c.gridy = 1; button.addActionListener(new MyButtonListener()); panel.add(button, c); frame.add(panel); } static class MyWindowListener extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { System.out.println("Closing window!"); System.exit(0); } } static class MyButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JPanel panel = new JPanel(); System.out.println("test"); panel.setBackground(Color.yellow); } } }
Only problem is when i press the button the test String is output but the colour won't change. Any ideas?
#6 nexus490
-
- New D.I.C Head
Reputation: 0
- Posts: 37
- Joined: 08-November 11
Re: I Can't change JPanel Background colour.
Posted 27 November 2011 - 12:14 PM
Its ok, no need to reply i found my mistake. For some reason i was creating a panel object twice.
Change Background Color of Jpanel in Java
Source: https://www.dreamincode.net/forums/topic/257359-i-cant-change-jpanel-background-colour/
0 Response to "Change Background Color of Jpanel in Java"
Post a Comment