-1

Sorry for not putting specific code in previous post. I realized I was doing something wrong, but now am having another problem. I am trying to place a JPanel at a specific place on a JFrame after clicking a button but nothing is happening, here is my code for my Actionperformed method.

public void actionPerformed(ActionEvent e) 
    {


          JPanel pa=new JPanel();
          frame.getContentPane().setLayout(null);
          pa.setBackground(Color.yellow);
          pa.setLocation(50,150);
          pa.setSize(150,100);
          pa.setBounds(50,150,150,100);
          pa.setOpaque(true);
          frame.add(pa);


    }                        
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Please add some source code as concrete examples so others can help. Currenly your question is like "something went wrong and I dont know why" – Jens Baitinger Jul 07 '17 at 19:10
  • If you post your code I will be glad to help. –  Jul 07 '17 at 19:10
  • @Tommy I added some code in my post. I am pretty new to coding, so any help would be great. – Nikhil Srikumar Jul 07 '17 at 20:27
  • @NikhilSrikumar Thank you for sharing your code. I will see what I can do. Where are you trying to put the panel? –  Jul 07 '17 at 20:30
  • at the coordinates (50,150) – Nikhil Srikumar Jul 07 '17 at 20:51
  • 1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 08 '17 at 06:50
  • .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 08 '17 at 06:52

1 Answers1

0

There are many ways you can do this.

  1. Use setLocation(x, y) and setSize(length, width)

  2. Use setBounds(x, y, length, width)

  3. If 1 and 2 don't work, look into layout Managers:

I highly recommend that you take a look at the Layout Managers JavaDoc. below are links for the each one, and the ones I would recommend for your case have a asterisk(*) next to them.

  • BorderLayout* is for general directions(NORTH, SOUTH, EAST, WEST)

  • BoxLayout puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.

  • CardLayout lets you implement an area that contains different components at different times, often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays.

  • FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row.

  • GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.

  • GridLayout* simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

  • GroupLayout is a layout manager that was developed for use by GUI builder tools, but it can also be used manually. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. Consequently, however, each component needs to be defined twice in the layout.

  • SpringLayout is a flexible layout manager designed for use by GUI builders. It lets you specify precise relationships between the edges of components under its control. For example, you might define that the left edge of one component is a certain distance (which can be dynamically calculated) from the right edge of a second component. SpringLayout lays out the children of its associated container according to a set of constraints.

Disclaimer: Most of these are direct quotes for the Oracle JavaDoc regarding Layout Managers.