Results 1 to 6 of 6

Thread: Pls help!!!

  1. #1
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Question Pls help!!!

    I wrote these codes that i can select a number. this number defines number of rows and columns of gridlayout and number of buttons in panel. it works. but when selecting new number, the old one doesnt disappear and the new grid appears back side. please help. i have to do this for my homework

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class lightsout extends JFrame{
    		private String[] number = {"3", "4", "5", "6", "7", "8", "9", "10"};
    		private JComboBox jcb = new JComboBox(number);
    		
    	    JPanel p2 = new JPanel();
    		private JPanel p1;
    		
    		JLabel jlb = new JLabel("Enter a number:");
    	
        	
        	
     public static void main(String[]args){
        	
        	lightsout frame = new lightsout();
    
        	frame.setTitle("Lights Out");
        	frame.setSize(500,300);
        	frame.setLocationRelativeTo(null);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
        	
        	}
        public lightsout() {
        	setDisplay(0);
        	p2.setLayout(new BorderLayout(25,25));
        		
        	p2.add(jlb, BorderLayout.WEST);
        	p2.add(jcb, BorderLayout.CENTER);
        	
        	add(p2, BorderLayout.NORTH);
        	jcb.addItemListener(new ItemListener() {
    
          public void itemStateChanged(ItemEvent e) {
    
            setDisplay(jcb.getSelectedIndex());
    
          }
    
        });
    
        }
         public void setDisplay(int index) {
    		
    		
    		p1 = new JPanel();
    		int n;
       		n = Integer.parseInt(number[index]);	
       		p1.setLayout(new GridLayout(n,n,0,0));
        		for (int i = 1; i <= n*n; i++) {
        			p1.add(new JButton());
        			}
    		add(p1, BorderLayout.CENTER);
      }
       
        
    }
    Last edited by Fubarable; 04-29-2011 at 01:38 AM. Reason: Moderator Action: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    In the setDisplay method, I would first remove p1 from the contentPane if p1 is not null, then create a new p1 as you're doing, add it to the contentPane, revalidate the contentPane (you'll need to cast it to a JPane first) and then repaint the contentPane.

    Also, I added code tags added to your post above to help make it readable.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck

  3. #3
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Sorry for code tags. i'm new in the forum.
    I understand what you say but i dont know how i write it. if you write i will be very glad.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by remus19 View Post
    Sorry for code tags. i'm new in the forum.
    I understand what you say but i dont know how i write it. if you write i will be very glad.
    If you try it first and then show us your attempt, I would be even gladder. :)

  5. #5
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    thanks for suggestion. i made it :) i have to write Lights Out game. Now i going t add game rules. if i will have a problem, i write here. thanks again..

  6. #6
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class lightsout extends JFrame{
    		
    		private JTextField jtf = new JTextField(15);
    		private JPanel p3 = new JPanel();
    	    JPanel p2 = new JPanel();
    		private JPanel p1;
    		private Object[] matrix;
    		JLabel jlb = new JLabel("Enter a number:");
    	
        	
        	
     public static void main(String[]args){
        	
        	lightsout frame = new lightsout();
    
        	frame.setTitle("Lights Out");
        	frame.setSize(800,500);
        	frame.setLocationRelativeTo(null);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
        	
        	}
        public lightsout() {
        	add(p3, BorderLayout.CENTER);
        	p3.setLayout(new BorderLayout(5,5));
        	setDisplay("2");
        	
        	p2.setLayout(new BorderLayout(25,25));
        		
        	p2.add(jlb, BorderLayout.WEST);
        	p2.add(jtf, BorderLayout.CENTER);
        	
        	p3.add(p2, BorderLayout.NORTH);
        	
        	jtf.addActionListener(new ActionListener() {
    
          public void actionPerformed(ActionEvent e) {
    
            setDisplay(jtf.getText());
    
          }
    
        });
    
        }
         public void setDisplay(String index) {
    		
    		if(p1 != null){
    			p3.remove(p1);
    			matrix = null;
    		}
    		
    		p1 = new JPanel();
    		int n;
       		n = Integer.parseInt(index);
       		matrix = new Object[n*n];	
       		p1.setLayout(new GridLayout(n,n,0,0));
        	for(int i = 0; i < matrix.length; i++){
    			matrix[i] = new MyButton(Integer.toString(i));
    			}
    		for(int i = 0; i < matrix.length; i++){
    			p1.add((MyButton)matrix[i]);
    			}
        	
        	
        	
        		/*for (int i = 1; i <= n*n; i++) {
        			p1.add(new MyButton(Integer.toString(i)));
        			}*/
    		p3.add(p1, BorderLayout.CENTER);
    		p3.revalidate();
    		p3.repaint();
    		
      }
        
    
    class MyButton extends JButton{
    	private ImageIcon blcIcon = new ImageIcon("black.jpg");
    	private ImageIcon prsIcon = new ImageIcon("pressed.jpg");
    	private ImageIcon whtIcon = new ImageIcon("white.jpg");
    	public MyButton(String s){
    		super.setText(s);
    		setHorizontalTextPosition(CENTER);
    		setForeground(Color.WHITE);
    		setIcon(blcIcon);
    		setPressedIcon(prsIcon);
    		addActionListener(new ActionListener() {
    
          public void actionPerformed(ActionEvent e) {
          	
          	
          	toogle();
          	
    
          }
    
        });
    		
    	}
    	private void toogle(){
    		
    		if(getIcon() == blcIcon){
          		setIcon(whtIcon);
          		setForeground(Color.BLACK);
          	}
          	else if(getIcon() == whtIcon){
          		setIcon(blcIcon);
          		setForeground(Color.WHITE);
          	}
    	}
    	
    }
    }
    i can change background of selected buton but i couldn't change its neighbour buttons. i cant use array.

Posting Permissions

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