I have 2 questions. How do I make a background color for my JFrame and how can I make a block that can move around.
I was playing around with buttons and I want to make a background color for my Jframe, and so I tried .setBackground() and .getContentPane().setBackground(), but neither seem to work
Also I want to make it so that there is a small ball initialized in the top left corner of the JFrame which can be moved around with the arrow keys and will trigger a new window when it hits one of the buttons. I know how to make collision code, I just don't know how to make the ball and have it move in association with WASD or arrow keys.
This is what I have so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
public class idk
{
public static void main (String args[])
{
JFrame frame = new JFrame("Button test 1");
frame.setSize(1680, 1000);
//the next line won't do anything
frame.setBackground(Color.RED);
JPanel panel = new JPanel();
JButton buttonOne = new JButton("Button" + "" + "1");
buttonOne.setBounds( 100, 100 ,200,100);
buttonOne.setBackground(Color.BLACK);
buttonOne.setForeground(Color.WHITE);
JButton buttonTwo = new JButton("Button" + "" + "2");
buttonTwo.setBounds( 740, 100 ,200,100);
buttonTwo.setBackground(Color.BLACK);
buttonTwo.setForeground(Color.WHITE);
JButton buttonThree = new JButton("Button" + "" + "3");
buttonThree.setBounds( 1380, 100 ,200,100);
buttonThree.setBackground(Color.BLACK);
buttonThree.setForeground(Color.WHITE);
JButton buttonFour = new JButton("Button" + "" + "4");
buttonFour.setBounds( 100, 450 ,200,100);
buttonFour.setBackground(Color.BLACK);
buttonFour.setForeground(Color.WHITE);
JButton buttonFive = new JButton("Button" + "" + "5");
buttonFive.setBounds( 740, 450 ,200,100);
buttonFive.setBackground(Color.BLACK);
buttonFive.setForeground(Color.WHITE);
JButton buttonSix = new JButton("Button" + "" + "6");
buttonSix.setBounds( 1380, 450 ,200,100);
buttonSix.setBackground(Color.BLACK);
buttonSix.setForeground(Color.WHITE);
JButton buttonSeven = new JButton("Button" + "" + "7");
buttonSeven.setBounds( 100, 800 ,200,100);
buttonSeven.setBackground(Color.BLACK);
buttonSeven.setForeground(Color.WHITE);
JButton buttonEight = new JButton("Button" + "" + "8");
buttonEight.setBounds( 740, 800 ,200,100);
buttonEight.setBackground(Color.BLACK);
buttonEight.setForeground(Color.WHITE);
JButton buttonNine = new JButton("Button" + "" + "9");
buttonNine.setBounds( 1380, 800 ,200,100);
buttonNine.setBackground(Color.BLACK);
buttonNine.setForeground(Color.WHITE);
panel.setLayout(null);
panel.add(buttonOne);
panel.add(buttonTwo);
panel.add(buttonThree);
panel.add(buttonFour);
panel.add(buttonFive);
panel.add(buttonSix);
panel.add(buttonSeven);
panel.add(buttonEight);
panel.add(buttonNine);
frame.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}