Results 1 to 3 of 3
  1. #1
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Gridlayout with buttons and labels

    I am trying to turn the dot com bust program into a gui app, so have made an initial program to show a grid. It usually gives the grid one would expect, but sometimes it jumbles everything up or doesn't work at all. Is there a better way of writing this code when we are combining different jcomponents?

    Java Code:
    import java.awt.*;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.GroupLayout.Alignment;
    
    public class Grid extends JFrame{
    	
    	ArrayList<JButton> buttonList = new ArrayList<JButton>();
    	ArrayList<JLabel> labelList = new ArrayList<JLabel>();
    	JFrame f;
    	JPanel p;
    	
    	public static void main(String[] arg)
    	{
    		Grid g = new Grid();
    		g.go();
    	}
    	
    	public void go()
    	{
    		setGUI();
    		populateGrid();
    	}
    	
    	public void setGUI()
    	{
    		f = new JFrame("Dot Com Bust!");
    		GridLayout grid = new GridLayout(8,8);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		p = new JPanel(grid);
    		p.setBackground(Color.white);
    		f.getContentPane().add(BorderLayout.EAST, p);
    		f.setSize(600,400);
    		f.setVisible(true);
    	}
    	
    	public void populateGrid()
    	{
    		int labels = 0;
    		int buttons = 0;
    		
    		for (int i=0;i<49;i++){			
    			buttonList.add(new JButton("X"));
    		}
    		
    		for (int i=0;i<15;i++){		
    			String[] letters = {"A","B","C","D","E","F","G"};
    			if (i<8){
    				labelList.add(new JLabel(Integer.toString(7-i),JLabel.CENTER));
    			}else{
    				labelList.add(new JLabel(letters[i-8],JLabel.CENTER));
    			}
    		}
    		
    		for (int i=0;i<64;i++){
    			if(i%8==0 || i>56){
    				p.add(labelList.get(labels++));
    				System.out.println("Labels "+labels);
    			}else{
    				Font font = new Font("Serif", Font.BOLD, 20);
    				buttonList.get(buttons).setFont(font);
    				p.add(buttonList.get(buttons++));
    				System.out.println("Buttons "+buttons);
    			}
    		}
    	}
    }
    It works but it looks clunky and a bit haphazard. Am I just way off the mark?

  2. #2
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Re: Gridlayout with buttons and labels

    Anyone?

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

    Default Re: Gridlayout with buttons and labels

    I don't see anything obviously wrong with your code. Is the current code not working well for you?

    Note that I generally try to avoid calling "setSize()" on anything except in certain special situations such as when adding components to a JLayeredPane. I'll usually try to let the components find their preferred size by calling pack() on the top window before setting it visible.

Similar Threads

  1. Replies: 3
    Last Post: 08-15-2011, 01:56 PM
  2. Printing labels to A4
    By eggy524 in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 04:53 PM
  3. Drawing labels/buttons on an image
    By Eric Coulthard in forum Java 2D
    Replies: 0
    Last Post: 05-25-2010, 10:11 PM
  4. size och swt labels
    By larsk in forum SWT / JFace
    Replies: 0
    Last Post: 10-04-2009, 12:01 PM
  5. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:26 PM

Posting Permissions

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