Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: For some reason, I am getting a grey background rather than the 'CYAN' I have coded.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default For some reason, I am getting a grey background rather than the 'CYAN' I have coded.

    However, it shows a CYAN border around the button on the frame. Does anyone know why?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
    public class Front_ChristmasCard extends JFrame implements ActionListener
    {
     
       static String Guestname;
       private boolean cardOpen=false;
       private JButton cardOpenButton = new JButton("Open Card");
     
     
        public Front_ChristmasCard()
        {
           Guestname= JOptionPane.showInputDialog("Enter your name to see your card:");
     
     
           //sets title above window
           setTitle("Marry Christmas " + Guestname);
     
           //     sets layouts, adds buttons and sets background.
           setLayout(new FlowLayout());
           add(cardOpenButton);
           getContentPane().setBackground(Color.CYAN);
     
           //adds listener
           cardOpenButton.addActionListener(this);
     
           //sets size of window
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setSize(700,900);
           setLocation(750,750);
           setVisible(true);
        }
     
     
       public void paint(Graphics g)
       {
          g.drawString("Merry Christmas " + Guestname + "",300, 800);
          g.setColor(Color.white);
          g.fillRect(130,700,460,45);
          g.setColor(Color.red);
          g.fillRect(150,655,420,45);
          g.fillRect(170,615,380,45);
          g.fillRect(190,575,340,45);
          g.fillRect(210,535,300,45);
          g.fillRect(230,495,260,45);
          g.fillRect(250,455,220,45);
          g.fillRect(270,415,180,45);
          g.fillRect(290,375,140,45);
          g.fillRect(310,335,100,45);
          g.fillRect(340,295,40,45);
          g.setColor(Color.white);
          g.fillOval(320,255,140,100);
     
     
     
       }
     
        public void actionPerformed(ActionEvent event)
        {
           if(event.getSource() == cardOpenButton)
           {
              cardOpen = true;
              Inside_ChristmasCard gui= new Inside_ChristmasCard();
     
     
     
           }
     
     
     
     
        }
     
     
       public static void main(String[] args)
       {
           Front_ChristmasCard gui=new Front_ChristmasCard();
       }//main
    }//class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: For some reason, I am getting a grey background rather than the 'CYAN' I have coded.

    Problems I see:
    Overriding the JFrame's paint method instead of adding a JPanel and overriding its paintComponent() method.
    Assuming 750,750 is on the screen.
    Not calling super method in paint
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: For some reason, I am getting a grey background rather than the 'CYAN' I have coded.

    I am sorry, but I couldn't understand your answer. Is there any chance you could explain those points more 'amateurally'

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: For some reason, I am getting a grey background rather than the 'CYAN' I have coded.

    Define a class that extends the JPanel class.
    Override that class's paintComponent() method
    Move the code that is in your paint() method to the paintComponent() method
    Create an instance of the new class and add it to the JFrame.

    See the tutorial for custom how to do custom painting:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. hard-coded text field looks different in LInux/Mac
    By iamhealed in forum AWT / Java Swing
    Replies: 1
    Last Post: September 9th, 2012, 09:45 AM
  2. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  3. Replies: 3
    Last Post: February 20th, 2012, 10:11 AM
  4. Reason for swing component overlapping?
    By Furious5k in forum AWT / Java Swing
    Replies: 1
    Last Post: November 6th, 2011, 08:40 AM
  5. hmm for some reason the char.At isnt working
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2010, 04:27 AM