JDialog not setting preferred size correctly?

I really don't understand what is happening here. I have a program that I would like to use a JDialog in, but I wanted a separate class for it so I extended the class for a JDialog, went to set the preferred size of it, but it doesn't appear to be setting it correctly. I make a button to "cancel" the operation the JDialog will do that is supposed to be in the bottom right of the JDialog, but it doesn't go there. It instead is outside of it, and only shows up once I resize the JDialog.

I don't have the slightest clue what's going on, or if I'm missing something stupid, but any help is appreciated.

The problem is happening because the preferred size of a dialog includes the title bar of the window, as well as any other window decorations. However, the location within the window is set from the upper left corner of the content pane. Because of that difference, most window managers will have your button end up a little too far right, and a lot too far down.

The correct solution is to use actual layout managers, which will correctly position your components. Here's an example which puts the button where you want:

If you're insistent on using a null layout, you can change your code to use getContentPane().setPreferredSize(new Dimension(300, 200)) instead, which will let your frame add the necessary space for the window decorations. However, I would strongly advise you not to do this. Using a null layout is considered a bad practice.