0

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);
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • *"I have 2 questions."* SO start 2 threads. SO is a Q&A Site, not a help desk. And while on the subject of questions, don't forget to add a question mark (?) to the end of them. – Andrew Thompson Feb 09 '15 at 00:47

1 Answers1

1
  • Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
  • You have a JPanel which is sitting on top of the frame, blocking whatever is behind it. In this case, consider changing the background color of the panel instead
  • In order to "move" a block about the screen, you will probably need to delve into custom painting, see Painting in AWT and Swing, Performing Custom Painting and 2D Graphics for more details
  • For key notification, you will get the best results from using the key bindings API, which has a number of benefits over the older KeyListener API. See How to Use Key Bindings for more details
  • Your buttons are going to be some what usless without some functionality behind them, see How to Write an Action Listeners, How to Use Buttons, Check Boxes, and Radio Buttons and How to Use Actions for more details

Something like How to change images based on keystrokes and I am trying to make ball gradually move for example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366