By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Color does not stay. Help!!

P: 14
Hello. Here is my code .. I followed the books instructions on how to make by box and now I am tring to jazz it up. However the set.background color does not stay. It blinks and then goes back to the original. Here is the code . what am I doing wrong.



Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. public class EmployeeUX
  4. extends JFrame {
  5. public EmployeeUX() throws HeadlessException {
  6. super("Employee Swing Interface by John H. Howard");
  7. JLabel lastName = new JLabel("Last Name:");
  8. JTextField lastNameText = new JTextField("Hope");
  9. JLabel fastName = new JLabel("Fast Name:");
  10. JTextField fastNameText = new JTextField("Bob");
  11.  
  12. this.add(lastName);
  13. this.add(lastNameText);
  14.  
  15. Container content = this.getContentPane();
  16. LayoutManager layout = new GridLayout(2,2);
  17. content.setLayout(layout);
  18. content.add(fastName);
  19. content.add(fastNameText);
  20.  
  21. content.add(lastName);
  22. content.add(lastNameText);
  23.  
  24. this.setLocation(1, 1);
  25.  
  26. this.setSize(480, 480);
  27. this.setBackground(Color.red) ;
  28. this.setVisible(true);
  29. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  30. }
  31.  
  32. public static void main(String[] args) {
  33. EmployeeUX employeeux = new EmployeeUX();
  34. }
  35. }
  36.  
Nov 30 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Hello. Here is my code .. I followed the books instructions on how to make by box and now I am tring to jazz it up. However the set.background color does not stay. It blinks and then goes back to the original. Here is the code . what am I doing wrong.



import java.awt.*;
import javax.swing.*;
public class EmployeeUX
extends JFrame {
public EmployeeUX() throws HeadlessException {
super("Employee Swing Interface by John H. Howard");
JLabel lastName = new JLabel("Last Name:");
JTextField lastNameText = new JTextField("Hope");
JLabel fastName = new JLabel("Fast Name:");
JTextField fastNameText = new JTextField("Bob");

this.add(lastName);
this.add(lastNameText);

Container content = this.getContentPane();
LayoutManager layout = new GridLayout(2,2);
content.setLayout(layout);
content.add(fastName);
content.add(fastNameText);

content.add(lastName);
content.add(lastNameText);

this.setLocation(1, 1);

this.setSize(480, 480);
this.setBackground(Color.red) ;
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
EmployeeUX employeeux = new EmployeeUX();
}
}
Where do you want the color to appear? You set it as the background color for the frame. Then you added components onto that frame so the background color no longer appears but is blocked by the components which have their own colors.
Nov 30 '06 #2

P: 14
I wanted it to stay the background color no matter what components I add.When I run the code the background color just blinks and then goes away.

It even did that when I had nothing but the box with the title..

thanks
John
Dec 1 '06 #3

10K+
P: 13,264
I wanted it to stay the background color no matter what components I add.When I run the code the background color just blinks and then goes away.

It even did that when I had nothing but the box with the title..

thanks
John
It is supposed to do that. Your current layout fills the whole frame with components added to the frame. You should also not add components directly onto the frame but should add them to a panel first.

Here is how to do it. Notice I have set the color on the contentpane


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4. public class EmployeeUX
  5. extends JFrame {
  6. public EmployeeUX() throws HeadlessException {
  7. super("Employee Swing Interface by John H. Howard");
  8. JLabel lastName = new JLabel("Last Name:");
  9. JTextField lastNameText = new JTextField("Hope");
  10. JLabel fastName = new JLabel("Fast Name:");
  11. JTextField fastNameText = new JTextField("Bob");
  12. JPanel panel = new JPanel();
  13. panel.add(lastName);
  14. panel.add(lastNameText); 
  15.  
  16. Container content = getContentPane();
  17. content.setBackground(Color.red) ;
  18. LayoutManager layout = new GridLayout(2,2);
  19. content.setLayout(layout);
  20. //content.add(fastName);
  21. //content.add(fastNameText);
  22. content.add(panel);
  23. //content.add(lastNameText);
  24. this.setLocation(1, 1);
  25. this.setSize(480, 480);
  26. this.setVisible(true);
  27. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  28. }
  29. public static void main(String[] args) {
  30. EmployeeUX employeeux = new EmployeeUX();
  31. }
  32. }
  33.  
  34.  
Dec 1 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.