Reputation: 584
In my code below, the resulting output has each row of JLabels squished together in the center. I would like them to spread out. The main method just creates an instance of the GUI and all necessary packages have been imported.
// Main class with JFrame and ActionListener enabled
public class GUI_with_2D_Arrays extends JFrame {
// Class variables
private static JLabel[][] labels = new JLabel[6][6];
private static JPanel[][] panels = new JPanel[6][6];
private static char[][] letters = new char[6][6];
private static Random random = new Random();
// Constructor
public GUI_with_2D_Arrays() {
// Initialize panels
for (JPanel[] row : panels)
Arrays.fill(row, new JPanel());
// Initialize chars with randomly generated letters
String consonants = "BCDFGHJKLMNPQRSTVWXYZ", vowels = "AEIOU";
int index;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
index = random.nextInt(consonants.length());
letters[i][j] = consonants.charAt(index);
}
for (int i = 0; i < 6; i++) {
index = random.nextInt(vowels.length());
letters[i][5] = vowels.charAt(index);
letters[5][i] = vowels.charAt(index);
}
shuffle(letters);
/*
* Assign chars to labels, sets font of labels, adds labels to panels, sets
* layout of panels and adds panels to frame
*/
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels[i].length; j++) {
labels[i][j] = new JLabel(Character.toString(letters[i][j]));
labels[i][j].setFont(new Font("Comic Sans MS", Font.PLAIN, 30));
panels[i][j].setLayout(new FlowLayout());
panels[i][j].add(labels[i][j]);
getContentPane().add(panels[i][j]);
}
}
setSize(840, 840);
setLayout(new GridLayout(6, 6));
setLocationRelativeTo(null);
setVisible(true);
}
}
How do I get java to display the JLabels separately and evenly across the screen?
Thanks for any help in advance.
Upvotes: 0
Views: 135
Reputation: 324157
How do I get java to display the JLabels separately and evenly across the screen?
You should only use a single panel with a GridLayout
. Then you should add the labels directly to this panel. So the basic logic would be:
JPanel labelPanel = new JPanel( new GridLayout(0, 5) );
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels[i].length; j++) {
JLabel label = new JLabel(Character.toString(letters[i][j]));
label.setFont(new Font("Comic Sans MS", Font.PLAIN, 30));
labelPanel.add( label );
}
}
add(labelPanel. BorderLayout.CENTER);
Also, get rid of the static
keyword on all your variables. You should not be using it.
If you have code like:
JFrame one = new GUI_with_2D_Arrays();
JFrame two = new GUI_with_2D_Arrays();
You have two instances of a JFrame
. However, because the variables are "static", each instance shares the same variables. This means that both frames would share the same panels and labels, which is not what you want (and also not possible since components can have only one parent). You would want each instance of the frame to have its own components.
The static
keyword should only be used for variables where the data is not unique and doesn't change. For example in your class you could have used:
private static String consonants = "BCDFGHJKLMNPQRSTVWXYZ";
since this is just a literal string that can be used by any instance of the GUI_with_2D_Arrays
class.
Upvotes: 1