Results 1 to 2 of 2
  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Background color of label not changing?

    So I have to make the game, last stone wins. So far, it works perfectly, but unfortunately, two of the labels is not changing color to yellow, or even gray. The problem is in scoreboard i believe. Picture of program: CelticsRule - Home first picture. the labels one and two arent turning gray or yellow.
    Driver:
    Java Code:
       import javax.swing.JFrame;
       public class Driver11
       {
          public static void main(String[] args)
          {
             JFrame frame = new JFrame("Last Stone Wins");
             frame.setSize(350,325);
             frame.setLocation(350,325);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setContentPane(new Panel11());
             frame.setVisible(true);
          }
       }
    Panel:
    Java Code:
       import javax.swing.*;
       import java.awt.*;
       import java.awt.event.*;
       public class Panel11 extends JPanel
       {
          private Display11 display;
          private Scoreboard11 scoreboard;
          private int total, run;
          public Panel11()
          {
             total = 12;
             run = 1;
             setLayout(new BorderLayout());
          
             scoreboard = new Scoreboard11();
             add(scoreboard, BorderLayout.NORTH);
          
             display = new Display11();
             add(display, BorderLayout.CENTER);
          
             JPanel panel = new JPanel();
             panel.setLayout(new FlowLayout());
             add(panel, BorderLayout.SOUTH);
             JButton button1 = new JButton("One");
             button1.addActionListener(new Listener1());
             panel.add(button1);
             JButton button2 = new JButton("Two");
             button2.addActionListener(new Listener2());
             panel.add(button2);
             JButton button3 = new JButton("Three");
             button3.addActionListener(new Listener3());
             panel.add(button3);
          }
          private class Listener1 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                run++;
                scoreboard.turn(run);
                total = total - 1;
                if(total <= 0)
                {
                   scoreboard.score(run);
                   total = 12;
                	
                }
             
                display.pickUp(total);
             }
          }
          private class Listener2 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                run++;
                scoreboard.turn(run);
                total = total - 2;
                if(total <= 0)
                {
                   scoreboard.score(run);
                   total = 12;
                }
             
             
                display.pickUp(total);
             }
          }
          private class Listener3 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                run++;
                scoreboard.turn(run);
                total = total - 3;
                if(total <= 0)
                {
                   scoreboard.score(run);
                   total = 12;
                }
             
                display.pickUp(total);
             }
          }
       }
    Scoreboard:
    Java Code:
       //Name______________________________ Date_____________
       import javax.swing.*;
       import java.awt.*;
       public class Scoreboard11 extends JPanel
       {
          private JLabel label1, label2, label3, oneLabel, twoLabel;
          private int count1, count2;
          public Scoreboard11()
          {
             setLayout(new GridLayout(1, 5));
             count1 = count2 = 0;
          
             // add(new JLabel("One: ", SwingConstants.RIGHT));
             oneLabel=new JLabel("One: ");
             oneLabel.setHorizontalAlignment(SwingConstants.RIGHT);
             oneLabel.setBackground(Color.GRAY.brighter());
             add(oneLabel);
             label1 = new JLabel("0");
             label1.setHorizontalAlignment(SwingConstants.LEFT);
             label1.setBackground(Color.yellow);
          
             add(label1);
             label2 = new JLabel("Nim");
             label2.setHorizontalAlignment(SwingConstants.CENTER);
             label2.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 20));
             add(label2);
             // add(new JLabel("Two: ", SwingConstants.RIGHT));
             twoLabel=new JLabel("Two: ");
             twoLabel.setHorizontalAlignment(SwingConstants.RIGHT);
             twoLabel.setBackground(Color.GRAY.brighter());
             add(twoLabel);
             label3 = new JLabel("0");
             label3.setHorizontalAlignment(SwingConstants.LEFT);
             label3.setBackground(Color.GRAY.brighter());
             add(label3);
             label1.setOpaque(true);
             label3.setOpaque(true);
          }
          public void turn(int r)
          {
             if(r%2==1)
             {
                label1.setBackground(Color.yellow);
                label3.setBackground(Color.GRAY.brighter());
                oneLabel.setBackground(Color.yellow);
                twoLabel.setBackground(Color.GRAY.brighter());
             }
             if(r%2==0)
             {	
                twoLabel.setBackground(Color.yellow);
                oneLabel.setBackground(Color.GRAY.brighter());
                label3.setBackground(Color.yellow);
                label1.setBackground(Color.GRAY.brighter());
             }
          
          }
          public void score(int s)
          {
             if(s%2==0)
             {
                count1++;
                label1.setText(""+count1);
             
             }
             if(s%2==1)
             {
                count2++;
                label3.setText(""+count2);
             }
          
          }
       }
    Display:
    Java Code:
    	//Name______________________________ Date_____________
       import javax.swing.*;
       import java.awt.*;
       public class Display11 extends JPanel
       {
          private JLabel label;
          public Display11()
          {
             setLayout(new GridLayout(1, 1));
          
             label = new JLabel("");
             label.setFont(new Font("Serif", Font.BOLD, 100));
             label.setHorizontalAlignment(SwingConstants.CENTER);
             add(label);
             ImageIcon twelve = new ImageIcon("stones12.jpg");
             label.setIcon(twelve);
          }
          public void pickUp(int n)
          {
          
             switch(n)
             {
                case 1: ImageIcon one = new ImageIcon("stones1.jpg");
                   label.setIcon(one);
                   break;
                case 2: ImageIcon two = new ImageIcon("stones2.jpg");
                   label.setIcon(two);
                   break;
                case 3: ImageIcon three = new ImageIcon("stones3.jpg");
                   label.setIcon(three);
                   break;
                case 4: ImageIcon four = new ImageIcon("stones4.jpg");
                   label.setIcon(four);
                   break;
                case 5: ImageIcon five = new ImageIcon("stones5.jpg");
                   label.setIcon(five);
                   break;
                case 6: ImageIcon six = new ImageIcon("stones6.jpg");
                   label.setIcon(six);
                   break;
                case 7: ImageIcon seven = new ImageIcon("stones7.jpg");
                   label.setIcon(seven);
                   break;
                case 8: ImageIcon eight = new ImageIcon("stones8.jpg");
                   label.setIcon(eight);
                   break;
                case 9: ImageIcon nine = new ImageIcon("stones9.jpg");
                   label.setIcon(nine);
                   break;
                case 10: ImageIcon ten = new ImageIcon("stones10.jpg");
                   label.setIcon(ten);
                   break;
                case 11: ImageIcon eleven = new ImageIcon("stones11.jpg");
                   label.setIcon(eleven);
                   break;
                case 12: ImageIcon twelve = new ImageIcon("stones12.jpg");
                   label.setIcon(twelve);
                   break;
                default:
                   break;
             
             }
          
          }
       }

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Background color of label not changing?

    oneLabel.setOpaque(true);
    twoLabel.setOpaque(true);

    JComponent (Java Platform SE 6)

Similar Threads

  1. Using scrollBars to change the color of a label
    By aortell24 in forum New To Java
    Replies: 4
    Last Post: 06-07-2012, 03:46 AM
  2. Replies: 5
    Last Post: 12-11-2011, 06:42 PM
  3. changing image of a label
    By Coold0wn in forum New To Java
    Replies: 4
    Last Post: 09-06-2011, 06:46 AM
  4. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 05:54 AM
  5. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •