Results 1 to 3 of 3
- 03-02-2012, 01:43 PM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
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?
It works but it looks clunky and a bit haphazard. Am I just way off the mark?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); } } } }
- 03-03-2012, 02:17 PM #2
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Re: Gridlayout with buttons and labels
Anyone?
-
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
-
How can I to print (printer) a JPanel and its componets (buttons and labels)?
By mad72584 in forum New To JavaReplies: 3Last Post: 08-15-2011, 01:56 PM -
Printing labels to A4
By eggy524 in forum New To JavaReplies: 3Last Post: 09-23-2010, 04:53 PM -
Drawing labels/buttons on an image
By Eric Coulthard in forum Java 2DReplies: 0Last Post: 05-25-2010, 10:11 PM -
size och swt labels
By larsk in forum SWT / JFaceReplies: 0Last Post: 10-04-2009, 12:01 PM -
Aligning Labels
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks