I'm new to coding, and I've been trying to turn all my JLabels to red, but for some reason, it just doesn't work. Some of the squares still stay the same color. This is what my code looks like:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Random;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Build {
JPanel[][] rod = new JPanel[11][11];
JLabel[][] label = new JLabel[11][11];
Border border = BorderFactory.createLineBorder(Color.BLACK);
JLabel[][] store = new JLabel[11][11];
JFrame frame = new JFrame("Start");
public static void main(String[] args) {
new Build();
}
public Build() {
frame.getContentPane();
frame.setSize(330, 330);
frame.setLayout(new GridLayout(11, 11));
Random rand = new Random();
Dimension dimension = new Dimension(30, 30);
for (int row = 0; row < label.length; row++) {
for (int col = 0; col < label[row].length; col++) {
JLabel myLabel = new JLabel();
myLabel = new JLabel();
myLabel.setOpaque(true);
myLabel.setBackground(Color.WHITE);
if (row % 2 == 0) {
myLabel.setBackground(Color.BLACK);
}
myLabel.setPreferredSize(dimension);
myLabel.setBorder(border);
frame.add(myLabel);
label[row][col] = myLabel;
}
int ran = rand.nextInt(11);
label[row][ran].setBackground(Color.WHITE);
store = label.clone();
}
frame.setVisible(true);
Scanner scan = new Scanner(System.in);
int e = scan.nextInt();
if (e == 1) {
store();
}
}
public void store() {
for (int i = 10; i > 0; i--) {
for (int j = 0; j < label[i].length; j++) {
while (label[i][j] == null) {
System.out.println("wow");
}
label[i][j].setBackground(Color.RED);
label[i][j].setOpaque(true);
}
}
}
}
My intention is for me to have every row from 1-10 be copied from a previous row (like row 0 becomes row 1), which is why I had a store array set up, but I'm struggling to see why this code doesn't turn every square from 1-10 red.
DATA: (before running store()): Works normal.
(After running store()): The first row hasn't changed, so that's good, but it's not working well for the rest.
I reworked your code to illustrate some basic Swing principles.
Here's the GUI I came up with. I made the random cells blue instead of white so you could see that I did, in fact, make one cell in each row a different color. I separated the cells by 2 pixels so you can see each white cell more clearly.
Here's the GUI after you left-click the button.
As you can see, all the cells are now red.
I removed the Scanner
console code and replaced it with a GUI JButton
.
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Netbeans section.
The first thing you do, in the main method, is call the SwingUtilities
invokeLater
method. This method ensures that you create and execute the Swing components on the Event Dispatch Thread.
I created a JFrame
and two JPanels
. One JPanel
holds the cells, while the other JPanel
holds the JButton
. Yes, you can add Swing components directly to a JFrame
, but it is not wise to do so. Always create a JPanel
to hold your Swing components and place the JPanel
on the JFrame
.
The JFrame
has a default BorderLayout
.
I separated the creation of the JFrame
and the two JPanels
into separate methods. this makes the code much easier to read and understand.
I created an ActionListener
for the JButton
. This is how, In Swing, you wait for a user action.
This code should be a better base for what you're trying to accomplish.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class Build implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Build());
}
private JLabel[][] label;
private Random random;
public Build() {
this.random = new Random();
this.label = new JLabel[11][11];
}
@Override
public void run() {
JFrame frame = new JFrame("Start");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.add(createButtonPanel(), BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 11, 2, 2));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Dimension dimension = new Dimension(30, 30);
Border border = BorderFactory.createLineBorder(Color.BLACK);
for (int row = 0; row < label.length; row++) {
for (int col = 0; col < label[row].length; col++) {
label[row][col] = new JLabel();
label[row][col].setBorder(border);
label[row][col].setOpaque(true);
label[row][col].setPreferredSize(dimension);
if (row % 2 == 0) {
label[row][col].setBackground(Color.BLACK);
} else {
label[row][col].setBackground(Color.WHITE);
}
panel.add(label[row][col]);
}
int ran = random.nextInt(label[row].length);
label[row][ran].setBackground(Color.BLUE);
}
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton button = new JButton("Click me");
button.addActionListener(new ButtonListener());
panel.add(button);
return panel;
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
for (int i = label.length - 1; i >= 0; i--) {
for (int j = 0; j < label[i].length; j++) {
label[i][j].setBackground(Color.RED);
}
}
}
}
}