0

Hey this is my second post so don't get mad at me but I'm having issues with the JPanel in java. I am trying to set the size and the location but it won't work, I have tried repaint(); but that does not work. Any help?

here's my code:

package test.test.test;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextField;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame  {

  JPanel colorPanel = new JPanel();

  public Display(){
    super("JPanel test");
    setLayout(new FlowLayout());
    add(colorPanel);
    colorPanel.setBackground(Color.CYAN);
    colorPanel.setSize(300, 300);
    repaint();
  } 
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2580555
  • 329
  • 2
  • 4
  • 13
  • 2
    This doesn't look like it actually compiles. Have you tried compiling it? When you say that it doesn't work, what do you mean exactly? What is happening and what do you want to happen? – JeroenWarmerdam Aug 07 '13 at 14:23
  • 2
    don't even think of setting a components size/location manually - that's the exclusive task of a suitable LayoutManager – kleopatra Aug 07 '13 at 14:57

2 Answers2

2

For the benefit of anyone reading this question later, here's a short, self contained, correct example of defining a JPanel with a background color.

Almost all the time, you should let the Swing component layout manager determine the size of Swing components. In this case, we define the preferred size of the JPanel because the JPanel does not contain any other Swing components.

The default layout manager of a JFrame is BorderLayout. The JPanel is positioned in the center of the BorderLayout.

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimplePanel implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("JPanel Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel colorPanel = new JPanel();
        colorPanel.setBackground(Color.CYAN);
        colorPanel.setPreferredSize(new Dimension(300, 300));

        frame.add(colorPanel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimplePanel());
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

When using Flowlayout, you should set the prefered size (of the component you add to your panel) instead of size, because the layoutmanager will handle the size an location for you.

public class Test extends JFrame  {

  JPanel colorPanel = new JPanel();

  public Display(){
    super("JPanel test");
    getContentPane().setLayout(new FlowLayout());
    colorPanel = new JPanel
    colorPanel.setPreferedSize(new Dimension(300,300));
    colorPanel.setBackground(Color.CYAN);
    getContentPane().add(colorPanel);
    pack();
    repaint();
  } 
}

and don't forget to set your JFrame visible and the size (of use pack()) ;)

Joris W
  • 517
  • 3
  • 16
  • no, you [should **not** call setXXSize](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519), ever. – kleopatra Aug 07 '13 at 14:52